Profetas wrote:
>
> I have a simple menu program that loops and ask the user to type
> a command, but after the first command is typed the readln doesn't
> read it properly. That doesn't affect much, but it is driving me
> mad. has anybode had the same problem?
>
> I am using freepascal BTW.
>
> Thanks (the code is below)
.... snip code ...
With minor editing and correction of your code, some annotated, the
following works. Note that it works on ALL versions of Pascal, and
uses nothing that is not available in ISO7185 (Standard) Pascal.
It is properly indented to show the level of control. Read the
comments I have added.
PROGRAM LexDemo(input, output);
(* You are using these files, so mention them *)
{PROCEDURE Menu();}
PROCEDURE menu; (* Pascal is not C, no parens *)
BEGIN
WRITELN(' Logical calculator by Lucas S Silvs ');
WRITELN('+------------------------------------------+');
WRITELN('+ Main Menu +');
WRITELN('+------------------------------------------+');
WRITELN('| D -=> Display menu |');
WRITELN('| L -=> Lex keyboard input |');
WRITELN('| P -=> Parser the tokens into parse tree |');
WRITELN('| E -=> Ealuate the parser tree |');
WRITELN('| B -=> Bind an identifier to a value |');
WRITELN('| S -=> Show the values of all identifiers |');
WRITELN('| Q -=> Quit |');
WRITELN('| Print (m)enu |');
WRITELN('| (l)ex token from standard input |');
WRITELN('| lex token(s) from standard input |');
WRITELN('| (r)ead string |');
WRITELN('| lex tokens from s(t)ring |');
WRITELN('+------------------------------------------+');
END {Menu};
(* 1-----------1 *) (* Separators are handy for human use *)
{PROCEDURE Main_Loop();}
PROCEDURE MainLoop; (* Pascal is not C *)
VAR
Option, Tokens : CHAR; (* Tokens was undefined *)
Commands : SET OF char; (* to guard case statement *)
(* 2-----------2 *)
(* dummies for parsing *)
PROCEDURE yylex; BEGIN writeln('yylex'); END;
PROCEDURE yyparser(Token : char);
BEGIN writeln('yyparse'); END;
(* 2-----------2 *)
BEGIN (* mainloop *)
Option := 'd';
Commands := ['L', 'P', 'l', 's', 'r', 'd'];
Tokens := ' '; (* initialize it *)
WHILE Option <> 'q' DO BEGIN
(* Portable to all flavors guard of case *)
IF NOT (option IN commands) THEN BEGIN
writeln('Unknown option');
menu;
END
ELSE
CASE Option OF
(* Remove those empty parens, Pascal is not C *)
'L' : yylex;
'P' : yyparser(Tokens);
'l' : Menu;
's' : Menu;
'r' : Menu;
'd' : Menu;
END; (* CASE, ELSE *)
WRITE('Type your command: ==> ');
READLN(Option);
END (* WHILE *)
END {Main_Loop};
(* 1-----------1 *)
BEGIN
MainLoop; (* No parens, Pascal is not C *)
END. {LexDemo}
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


|