Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. (*
  2.    adapted to Oberon-07 by 0CodErr, KolibriOS team
  3.                                                    *)
  4. (* Example program from Programming In Modula-2, N. Wirth., pg. 56, *)
  5. (*  - no WINDOWS in this example *)
  6.  
  7. (* this program translates a small language into postfix form
  8.  *  the language is
  9.  *
  10.  *           expression = term { [ "+" | "-" ] term }
  11.  *
  12.  *           term = factor { [ "*" | "/" ] factor }
  13.  *
  14.  *           factor = letter | "(" expression ")"
  15.  *
  16.  *           letter  =  "a" | 'b" | … | "z"
  17.  *
  18.  *   try as input
  19.  *      a+b
  20.  *      a*b+c
  21.  *      a+b*c
  22.  *      a*(b/(c-d))
  23.  *)
  24.  
  25. MODULE postfix;
  26.  
  27. IMPORT In, Out, Console;
  28.  
  29.  
  30. CONST
  31.     OUT_LINE_SIZE = 80;
  32.     IN_LINE_SIZE = 80;
  33.  
  34.  
  35. VAR
  36.     ch       : CHAR;
  37.     i, index : INTEGER;
  38.     out_line : ARRAY OUT_LINE_SIZE OF CHAR;
  39.     in_line  : ARRAY IN_LINE_SIZE OF CHAR;
  40.     cur_ch   : INTEGER;
  41.  
  42.  
  43. PROCEDURE NextChar(): CHAR;
  44. BEGIN
  45.     INC(cur_ch)
  46.     RETURN in_line[cur_ch - 1]
  47. END NextChar;
  48.  
  49.  
  50. PROCEDURE expression;
  51. VAR
  52.     addop :CHAR;
  53.  
  54.  
  55.     PROCEDURE term;
  56.     VAR
  57.         mulop :CHAR;
  58.  
  59.  
  60.         PROCEDURE factor;
  61.         BEGIN (* factor *)
  62.             IF ch = "(" THEN
  63.                 ch := NextChar();
  64.                 expression;
  65.                 WHILE ch # ")" DO
  66.                     ch := NextChar()
  67.                 END (* WHILE *)
  68.             ELSE
  69.                 WHILE (ch < "a") OR (ch > "z") DO
  70.                     ch := NextChar()
  71.                 END; (* WHILE *)
  72.                 out_line[index] := ch;
  73.                 index := index + 1
  74.             END; (* IF *)
  75.             ch := NextChar()
  76.         END factor;
  77.  
  78.  
  79.     BEGIN (* term *)
  80.         factor;
  81.         WHILE (ch = "*") OR (ch = "/") DO
  82.             mulop := ch;
  83.             ch := NextChar();
  84.             factor;
  85.             out_line[index] := mulop;
  86.             index := index + 1
  87.         END (* WHILE *)
  88.     END term;
  89.  
  90.  
  91. BEGIN (* expression *)
  92.     term;
  93.     WHILE (ch = "+") OR (ch = "-") DO
  94.         addop := ch;
  95.         ch := NextChar();
  96.         term;
  97.         out_line[index] := addop;
  98.         index := index + 1
  99.     END (* WHILE *)
  100. END expression;
  101.  
  102.  
  103. BEGIN (* Postfix *)
  104.     Console.open;
  105.  
  106.     index := 1; cur_ch := 0;
  107.     Out.String("Enter expression:");
  108.     In.String(in_line);
  109.     ch := NextChar();
  110.     WHILE ch > " " DO
  111.         expression;
  112.         FOR i := 1 TO index - 1 DO
  113.             Out.Char(out_line[i])
  114.         END; (* FOR *)
  115.         Out.Ln;
  116.         index := 1; cur_ch := 0;
  117.         Out.String("Enter expression:");
  118.         In.String(in_line);
  119.         ch := NextChar()
  120.     END; (* WHILE *)
  121.  
  122.     Console.exit(TRUE)
  123. END postfix.