Recent

Author Topic: program NAME(input,output); // old book example, another...  (Read 6970 times)

juanirias

  • Full Member
  • ***
  • Posts: 103
Hi!

I have some old book with this example:

Code: Pascal  [Select][+][-]
  1. program accept(input,output);
  2. (* this is a recursive program to recognize a string of the form:     *)
  3. (* w'c'm$ where w is some string, 'c' is the character c, and m is    *)
  4. (* the reverse of w note that w may not contain the letter c or a $   *)
  5. function match: boolean;
  6. var
  7.   ch1   :       char;           { first char of a symmetric pair                        }
  8.   ch2   :       char;           { last char of a symmetric pair                         }
  9.   t     :       boolean;        { holds value returned by recursive call        }
  10. begin
  11.   if not eof then
  12.     begin
  13.       read(ch1);
  14.       if ch1 <> 'c' then
  15.         begin
  16.           t := match;   { this is a recursive call              }
  17.           if t and not eof then
  18.             begin
  19.               read(ch2);
  20.               match := ( ch1 = ch2 )
  21.             end
  22.             else match := false
  23.         end
  24.         else match := true { found a c }
  25.     end
  26.     else match := false
  27. end;  { of function match }
  28.  
  29.  
  30.  
  31. begin  { program accept }      
  32.  
  33.  
  34.   if match then
  35.     begin
  36.       if input = '$' then writeln(' legal string')
  37.       else writeln(' no dollar sign')
  38.     end
  39.   else writeln(' ilegal string');
  40. end.
  41.  

The ERROR:

accept.pas(36,16) Error: Operator is not overloaded: "Text" = "Char"

Some advice for the "input" ?

regards, JUAN
« Last Edit: June 28, 2016, 05:48:39 am by juanirias »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: program NAME(input,output); // old book example, another...
« Reply #1 on: June 28, 2016, 06:35:40 am »
you should at least change line 16 from
Code: Pascal  [Select][+][-]
  1.           t := match;   { this is a recursive call              }
into
Code: Pascal  [Select][+][-]
  1.           t := match();   { this is a recursive call              }
Otherwise it will not do a recursive call.

Input is the input channel, thus typed as a textfile.

edit: Seems that the last char is not read, so do it manually:
Code: Pascal  [Select][+][-]
  1. program accept(input,output);
  2.  
  3. (* this is a recursive program to recognize a string of the form:     *)
  4. (* w'c'm$ where w is some string, 'c' is the character c, and m is    *)
  5. (* the reverse of w note that w may not contain the letter c or a $   *)
  6. function match: boolean;
  7. var
  8.   ch1   :       char;           { first char of a symmetric pair                        }
  9.   ch2   :       char;           { last char of a symmetric pair                         }
  10.   t     :       boolean;        { holds value returned by recursive call        }
  11. begin
  12.   if not eof then
  13.     begin
  14.       read(ch1);
  15.       if ch1 <> 'c' then
  16.         begin
  17.           t := match();   { this is a recursive call              }
  18.           if t and not eof then
  19.             begin
  20.               read(ch2);
  21.               match := ( ch1 = ch2 )
  22.             end
  23.             else match := false
  24.         end
  25.         else match := true { found a c }
  26.     end
  27.     else match := false
  28. end;  { of function match }
  29.  
  30.  
  31. var
  32.   LastChar: Char;  
  33.  
  34. begin  { program accept }      
  35.   if match then
  36.   begin
  37.     if not(eof) then
  38.     begin
  39.       read(LastChar);
  40.  
  41.       if lastchar = '$' then writeln(' legal string')
  42.       else writeln(' no dollar sign')
  43.     end
  44.     else writeln(' ilegal string, missing dollar sign');
  45.   end
  46.   else writeln(' ilegal string');
  47. end.
  48.  
« Last Edit: June 28, 2016, 06:48:11 am by molly »

juanirias

  • Full Member
  • ***
  • Posts: 103
Re: program NAME(input,output); // old book example, another...
« Reply #2 on: June 28, 2016, 06:50:18 am »
Thanks!

On the book before the equal sign is like an arrow with upper direction 

Code: Pascal  [Select][+][-]
  1. input ↑ = '$'
« Last Edit: June 28, 2016, 06:56:06 am by juanirias »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: program NAME(input,output); // old book example, another...
« Reply #3 on: June 28, 2016, 06:53:27 am »
Yeah, i just finished posting a working version just before you posted your post  :D

Quote
I the book before the equal sign is like an arrow with upper direction
I have not the faintest idea what that means or is suppose to do. You simply can't check input like you would treat it as a string/char.

mischi

  • Full Member
  • ***
  • Posts: 191
Re: program NAME(input,output); // old book example, another...
« Reply #4 on: June 28, 2016, 07:08:52 am »
<code>input^ = '$'</code> is old-school ANSI pascal. input^ has the next character, before the next read. So, you can do a check of the next character, before you actually read it. You have to compile it with the option -Miso and probably the SVN version from trunk. I think that it is not working in plain 3.0.0. On OS X, the version 3.0.0.1 from fink compiles it and the program seems to work.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: program NAME(input,output); // old book example, another...
« Reply #5 on: June 28, 2016, 07:25:00 am »
Aaah, the "^" character, ok.

Thank you very much for the explanation mischi.

In that case we can accomplish something similar with a bit of magic (note: black magic, which could fail under certain circumstances).

for the main program part:
Code: Pascal  [Select][+][-]
  1. begin  { program accept }      
  2.   if match then
  3.   begin
  4.     if TextRec(input).buffer[TextRec(input).bufpos] = '$' then writeln(' legal string')
  5.     else writeln(' no dollar sign')
  6.   end
  7.   else writeln(' ilegal string');
  8. end.
  9.  

mischi

  • Full Member
  • ***
  • Posts: 191
Re: program NAME(input,output); // old book example, another...
« Reply #6 on: June 28, 2016, 11:48:58 am »
Maybe someone can check fpc 3.0.0. There is a chance that it works in ISO mode (fpc -Miso Filename). At least the upcoming fpc 3.0.2 will handle it correctly.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: program NAME(input,output); // old book example, another...
« Reply #7 on: June 28, 2016, 12:07:44 pm »
Sorry mischi, i was not aware you wanted to know.

adding {$MODE ISO} in the code, adjusting main to:
Code: [Select]
begin  { program accept }       
  if match then
  begin
    if input^ = '$' then writeln(' legal string')
    else writeln(' no dollar sign')
  end
  else writeln(' ilegal string');
end.
works with FPC 3.0.0 as it gave me the proper results
« Last Edit: June 28, 2016, 12:09:41 pm by molly »

mischi

  • Full Member
  • ***
  • Posts: 191
Re: program NAME(input,output); // old book example, another...
« Reply #8 on: June 28, 2016, 02:04:45 pm »
Good to know, that fpc 3.0.0 already does the trick. Your hint with {$MODE ISO} as alternative should be valuable. Thank you. Now it is up to Juanirias to verify at least on of our suggestions.

juanirias

  • Full Member
  • ***
  • Posts: 103
Re: program NAME(input,output); // old book example, another...
« Reply #9 on: June 28, 2016, 05:34:56 pm »
Thanks two both!

One note: the match function; the program work with or without ()

work using:

Code: Pascal  [Select][+][-]
  1. t := match;     { this is a recursive call              }

OR

Code: Pascal  [Select][+][-]
  1. t := match();   { this is a recursive call              }

 

TinyPortal © 2005-2018