Problem 118: PASIC

Description

PASIC is a simple programming language, as its name suggests, it is a cross between PAScal and baSIC. The entire language is defined by the following BNF specification.

In this version of BNF,

PASIC Syntax

      program == "PROGRAM" block

      block == "BEGIN" statementlist "END"

      statementlist == statement { ";" statementlist }

      statement == block | assign | output | conditional | loop

      assign == variable ":=" expression

      expression == variable | constant | "(" expression operator expression ")"

      operator == "+" | "-" | "*" | "/" | "=" | "<" | ">"

      output == "PRINT" outputitemlist

      outputitemlist == outputitem { "," outputitemlist }

      outputitem == expression | string | "NEWLINE"

      conditional == "IF" expression "THEN" statement

      loop == "WHILE" expression "DO" statement

      variable: see lexical rules

      constant: see lexical rules

      string: see lexical rules

Lexical Rules

So, a program in PASIC consists of the word "program" followed by a block. A block consists of the words "begin" and "end" surrounding a statement-list. A statement-list always consists of at least one statement; after that one statement there may be a semicolon followed by a further statement-list. This has the overall effect of saying that a statement-list consists of any (non-zero) number of statements separated by semicolons. Each statement is either another block, and assignment statement, an output statement, a conditional statement, or a loop statement.

The Problem

You must write a program that reads a PASIC program as input, and runs it. The output from your program should be exactly the output that the PASIC program would produce if it were run.

You may safely assume that the input will be a single, perfectly correct, PASIC program, immediately followed by the end-of-file. The syntax and lexical rules will be followed exactly. Execution of the program will never result in a division by zero, and no numeric results or intermediate results will be less than 2-31 or greater than or equal to 231. The program will not contain any infinite loops. You will ceratinly not be required to perform any "optimisations" on the PASIC program to get the desired results.

Semantics

Input Format

The input will be a single perfectly correct PASIC program. It may occupy more than one line. No line will contain more than 100 characters.

Output Format

The output should be exactly the output that would be produced by running the PASIC program. Only print statements produce any output. Print an extra newline after the PASIC program terminates, so that all output lines will be properly terminated even if the PASIC program afils to print a newline, and there will be an extra blank line at the end if the PASIC program does print a final newline.

Sample Input 1

program begin
  x:=12;
  y:=34;
  z:=((x*2)+y);
  print z    
end

Sample Output 1

58

Sample Input 2

PROGRAM
  BEGIN
    n:=7;
    f:=1;
    n1:=n;
    WHILE (n1>0) DO
      BEGIN
        f:=(f*n1);
        n1:=(n1-1)
      END;
    PRINT n, '! = ', f, NEWLINE
  END

Sample Output 2

7! = 5040

Sample Input 3

program begin
  i:=1;
  while (i<10) do begin
    j:=1;
    while (j<10) do begin
      p:=(i*j);
      if (p<10) then print ' ';
      print ' ', p;
      j:=(j+1) end;
    print newline;
    i:=(i+1) end end

Sample Output 3

  1  2  3  4  5  6  7  8  9
  2  4  6  8 10 12 14 16 18
  3  6  9 12 15 18 21 24 27
  4  8 12 16 20 24 28 32 36
  5 10 15 20 25 30 35 40 45
  6 12 18 24 30 36 42 48 54
  7 14 21 28 35 42 49 56 63
  8 16 24 32 40 48 56 64 72
  9 18 27 36 45 54 63 72 81

End of Problem