Peter's calculator broke down last week. All he has left is a computer with no calculator app, plus paper and pencil — too tedious for an engineer. As one of Peter's friends, you have been asked to write him a calculator program. After talking with him, you learn the following:
The input strictly follows this syntax, given in EBNF:
file = line { line } .
line = [ assignment | print | reset ] .
assignment = var ":=" expression.
print = "PRINT" var.
reset = "RESET".
expression = term { addop term }.
term = factor { mulop factor }.
factor = "(" expression ")" | var | number.
addop = "+" | "-".
mulop = "*".
In Extended Backus-Naur Form (EBNF), A = B C means the construct A consists of a B followed by a C. A = B | C means A is a B or, alternatively, a C. A = [ B ] means A is either a B or nothing, and A = { B } means A is the concatenation of any number of Bs (including none).
The production var is the name of a variable: a letter followed by up to 49 more letters or digits. Letters may be uppercase or lowercase. The production number is an integer. The exact syntax of these productions is:
var = letter { letter | digit }.
number = [ "-" ] digit { digit }.
letter = "A" | "B" | ... | "Z" | "a" | "b" | ... | "z".
digit = "0" | "1" | ... | "8" | "9".
Any number of spaces may appear between the parts of a construct, but never inside a variable name or an integer. <EOF> denotes the end of the input, and <CR> denotes the newline character. Every line of the input is shorter than 200 characters. The case of letters matters for both variables and keywords.
A variable's value is undefined when:
Write a program that implements Peter's calculator. It must store every variable definition and, for each PRINT statement, evaluate the named variable using the most recent definitions. When it encounters a RESET statement, it must delete all stored variables so that every variable becomes undefined again.
The input contains calculations that follow the syntax above. Each line is either an assignment to a variable, a PRINT statement, a RESET statement, or empty.
For each PRINT statement in the input, output one line containing the numeric value of the named variable, or the word UNDEF if the variable is undefined.