The following defines a simple language in which a program consists of assignment statements, and each variable is assumed to be of integer type. For simplicity, only operators that produce integer values are included. An assignment that starts with the keyword 'let' defines a single-assignment variable, where the expression on the right-hand side may contain constants or previously defined single-assignment variables only.
Write an interpreter for this language in a programming language of your choice. Given a program, your interpreter should be able to do the following:
Program:
Assignment*
Assignment:
Identifier = Exp;
let Identifier = Exp;
Exp:
Exp + Term | Exp - Term | Term
Term:
Term * Fact | Fact
Fact:
( Exp ) | - Fact | + Fact | Literal | Identifier
Identifier:
Letter [Letter | Digit]*
Letter:
a|...|z|A|...|Z|_
Literal:
0 | NonZeroDigit Digit*
NonZeroDigit:
1|...|9
Digit:
0|1|...|9
x = 001;
error
x_2 = 0;
x_2 = 0
x = 0
y = x;
z = ---(x+y);
error
let x = 1;
y = 2;
z = ---(x+y)*(x+-y);
x = 1
y = 2
z = 3
let x = 1;
y = 2;
let z = x+ y;
error, normal variables in let expression