prog -> stmt.
stmt -> IF expr THEN stmt ELSE stmt
| WHILE expr DO stmt
| INPUT ID
| ID ASSIGN expr
| WRITE 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
"input" => INPUT
"else" => ELSE
"begin" => BEGIN
"end" => END
"write" => WRITE
{alpha}[{digit}{alpha}]* => ID (identifier)
{digit}+ => NUM (positive integer)
"+" => ADD
":=" => ASSIGN
"-" => SUB
"*" => MUL
"/" => DIV
"(" => LPAR
")" => RPAR
";"=> SEMICOLON
/* This program calculates the
factorial of the number which
is input */
begin
input x; % input a number ..
y := 1;
while x do
begin
y := y * x;
x := x - 1;
end;
write y;
end
beginy:= 23;end |
begin if y then x:= 10end |
cPUSH 23 |
rPUSH yL1: cPUSH 1L2: rPUSH z |
# ... aliases for Robin's stack machine in csh.
# 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 = $< " '