Peter's Calculator

No attempts yetTime limit1sMemory limit128 MB

Problem

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:

  • Peter works only with integers. The operations he needs are addition, subtraction, and multiplication.
  • He wants to use an arbitrary number of variables whose names are at most 50 characters long.
  • His main way of computing is to type in formulas and assign them to variables. Some formulas are complex expressions that may refer to variables not yet defined; others consist of a single number. Peter then asks for the value of some variable, i.e. he evaluates its formula.
  • Peter also wants to redefine some variables and then re-evaluate the formulas that depend on them.

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:

  • it has not been defined yet, or it refers to a variable that has not been defined yet; or
  • its definition contains a cycle.

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.

Input

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.

Output

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.