Imp2 Interpreter

The Grammar

program → 'program' ';' var-decl proc-decl-list cmpd-stmt '.'
proc-decl-list → proc | proc-decl proc-decl-list | empty
proc-decl → 'procedure' IDENT '(' formal-list ')' ';' var-decl cmpd-stmt ';'
var-decl → 'var' ident-list ';' | empty
formal-list → ident-list | empty
ident-list → IDENT | IDENT ',' ident-list
stmt-list → stmt | stmt ';' stmt-list
stmt → assign-stmt | write-stmt | writeln-stmt | if-stmt | if-else-stmt |
while-stmt | cmpd-stmt | empty-stmt | proc-call-stmt
assign-stmt → IDENT ':=' expr
write-stmt → 'write' '(' expr ')'
writeln-stmt → 'writeln' '(' ')'
if-stmt → 'if' expr 'then' stmt
if-stmt → 'if' expr 'then' stmt 'else' stmt
while-stmt → 'while' expr 'do' stmt
cmpd-stmt → 'begin' stmt-list 'end'
empty-stmt →  empty
proc-call-stmt → IDENT '(' expr-list ')'
expr-list → expr | expr ',' expr-list | empty
expr → or-expr
or-expr → and-expr | or-expr 'or' and-expr
and-expr → rel-expr | and-expr 'and' rel-expr
rel-expr → add-expr | add-expr '<' add-expr | add-expr '<=' add-expr | add-expr '=' add-expr |
add-expr '<>' add-expr | add-expr '>=' add-expr | add-expr '>'
add-expr → mul-expr | add-expr + mul-expr | add-exp - mul-expr
mul-expr → unary-expr | mul-expr '*' unary-expr | mul-expr 'div' unary-expr | mul-expr '%' unary-expr
unary-expr → primary-expr | '+' primary-expr | '-' primary-expr | 'not' primary-expr
primary-expr → INT-CONST | IDENT | 'true' | 'false' | '(' expr ')'
empty → 

imp.rkt

lexer.rkt

tree.rkt

parser.rkt

exec.rkt

eval.rkt

error.rkt