2f54c7de00
git-svn-id: svn://kolibrios.org@8097 a494cfbc-eb01-0410-851d-a64ba20cac60
123 lines
2.5 KiB
Plaintext
123 lines
2.5 KiB
Plaintext
(*
|
|
adapted to Oberon-07 by 0CodErr, KolibriOS team
|
|
*)
|
|
(* Example program from Programming In Modula-2, N. Wirth., pg. 56, *)
|
|
(* - no WINDOWS in this example *)
|
|
|
|
(* this program translates a small language into postfix form
|
|
* the language is
|
|
*
|
|
* expression = term { [ "+" | "-" ] term }
|
|
*
|
|
* term = factor { [ "*" | "/" ] factor }
|
|
*
|
|
* factor = letter | "(" expression ")"
|
|
*
|
|
* letter = "a" | 'b" | … | "z"
|
|
*
|
|
* try as input
|
|
* a+b
|
|
* a*b+c
|
|
* a+b*c
|
|
* a*(b/(c-d))
|
|
*)
|
|
|
|
MODULE postfix;
|
|
|
|
IMPORT In, Out, Console;
|
|
|
|
|
|
CONST
|
|
OUT_LINE_SIZE = 80;
|
|
IN_LINE_SIZE = 80;
|
|
|
|
|
|
VAR
|
|
ch : CHAR;
|
|
i, index : INTEGER;
|
|
out_line : ARRAY OUT_LINE_SIZE OF CHAR;
|
|
in_line : ARRAY IN_LINE_SIZE OF CHAR;
|
|
cur_ch : INTEGER;
|
|
|
|
|
|
PROCEDURE NextChar(): CHAR;
|
|
BEGIN
|
|
INC(cur_ch)
|
|
RETURN in_line[cur_ch - 1]
|
|
END NextChar;
|
|
|
|
|
|
PROCEDURE expression;
|
|
VAR
|
|
addop :CHAR;
|
|
|
|
|
|
PROCEDURE term;
|
|
VAR
|
|
mulop :CHAR;
|
|
|
|
|
|
PROCEDURE factor;
|
|
BEGIN (* factor *)
|
|
IF ch = "(" THEN
|
|
ch := NextChar();
|
|
expression;
|
|
WHILE ch # ")" DO
|
|
ch := NextChar()
|
|
END (* WHILE *)
|
|
ELSE
|
|
WHILE (ch < "a") OR (ch > "z") DO
|
|
ch := NextChar()
|
|
END; (* WHILE *)
|
|
out_line[index] := ch;
|
|
index := index + 1
|
|
END; (* IF *)
|
|
ch := NextChar()
|
|
END factor;
|
|
|
|
|
|
BEGIN (* term *)
|
|
factor;
|
|
WHILE (ch = "*") OR (ch = "/") DO
|
|
mulop := ch;
|
|
ch := NextChar();
|
|
factor;
|
|
out_line[index] := mulop;
|
|
index := index + 1
|
|
END (* WHILE *)
|
|
END term;
|
|
|
|
|
|
BEGIN (* expression *)
|
|
term;
|
|
WHILE (ch = "+") OR (ch = "-") DO
|
|
addop := ch;
|
|
ch := NextChar();
|
|
term;
|
|
out_line[index] := addop;
|
|
index := index + 1
|
|
END (* WHILE *)
|
|
END expression;
|
|
|
|
|
|
BEGIN (* Postfix *)
|
|
Console.open;
|
|
|
|
index := 1; cur_ch := 0;
|
|
Out.String("Enter expression:");
|
|
In.String(in_line);
|
|
ch := NextChar();
|
|
WHILE ch > " " DO
|
|
expression;
|
|
FOR i := 1 TO index - 1 DO
|
|
Out.Char(out_line[i])
|
|
END; (* FOR *)
|
|
Out.Ln;
|
|
index := 1; cur_ch := 0;
|
|
Out.String("Enter expression:");
|
|
In.String(in_line);
|
|
ch := NextChar()
|
|
END; (* WHILE *)
|
|
|
|
Console.exit(TRUE)
|
|
END postfix. |