prog -> stmt.
stmt -> IF expr THEN stmt ELSE stmt
| WHILE expr DO stmt
| DO stmt UNTIL expr | READ ID
| ID ASSIGN expr
| PRINT expr
| BEGIN stmtlist END.
stmtlist -> stmtlist stmt SEMICOLON
|.
expr -> expr addop term
| term.
addop -> ADD
| SUB.
term -> term mulop factor
| factor.
mulop -> MUL
| DIV.
factor -> LPAR expr RPAR
| ID
| NUM
| SUB NUM.
"if" => IF
"then" => THEN
"while" => WHILE
"do" => DO
"until" => UNTIL
"read" => READ
"else" => ELSE
"begin" => BEGIN
"end" => END
"print" => PRINT
{alpha}[{digit}{alpha}]* => ID (identifier)
{digit}+ => NUM (positive integer)
"+" => ADD
"-" => SUB
"*" => MUL
"/" => DIV
"(" => LPAR
")" => RPAR
";"=> SEMICOLON
/* This program calculates the factorial of the number input */
begin % input a number ..
read x;
y:= 1;
while x do
begin
endy:= y * x;end;
x:= x - 1;
print y;
beginy:= 23;end |
begin if y then x:= 10end |
cPUSH 23 |
rPUSH yL1: cPUSH 1L2: rPUSH z |
# ... aliases for Robin's stack machine.
# This file should be "sourced" prior to executing
# stack machine files.
set stack = ""
alias cPUSH 'set stack = (\!:1 $stack)'
alias rPUSH 'set stack = ($\!:1 $stack)'
alias sPUSH '@ stack[1] = $stack[1] + 1 ; set stack[1] = $stack[$stack[1]]'
alias LOAD 'eval "set \!:1 = \$stack[1] ; shift stack"'
alias OP2 'eval "@ stack[2] = \$stack[2] \!:1 \$stack[1]"; shift stack'
alias cJUMP 'set tos = $stack[1]; shift stack; if ($tos == 0) goto \!:1'
alias JUMP goto
alias PRINT 'echo $stack[1]; shift stack'
alias READ 'eval "set \!:1 = $< " '