Here you have to design an interpreter for a subset of the While language. It is a simple imperative language which only supports integer literals.
We will use similar grammar which its authors1,2,3 have used. Below is the description of grammar that we will use.
- x, y ∈ Var (variables)
n ∈ Num (numerals/integers)
opa ∈ Opa (arithmetic operators)
opa ::=+
|-
|*
|/
opb ∈ Opb (boolean operators)
opb ::=and
|or
opr ∈ Opr (relational operators)
opr ::=>
|<
a ∈ AExp (arithmetic expressions)
a ::= x | n | a1 opa a2 | ( a )b ∈ BExp (boolean expressions)
b ::= true | false | b1 opb b2 | a1 opr a2 | ( b )S ∈ Stmt (statements)
S ::= x := a | S1 ; S2 | if b then { S1 } else { S2 } | while b do { S }
Here all operators are left associative. Their precedence order is as follows.
- Arithmetic Operators: (
*
,/
) > (+
,-
) > (>
,<
) - Boolean Operators:
and
>or
You can safely assume that all variables have integer type and are initialized properly. All variables name will consist of only lowercase letter ('a'-'z') and it's length will not exceed 10.
Note that ";
" is more like of a sequencing operator. It is used to concatenate two statements. That's why there will be no ";
" at the end of block of statements.
All divisions are integers divisions, that is, a/b = floor(a/b)
. Intermediate values of any variable will always be in range [0, 2*1018].
All test cases are valid programs. All of them will execute no more than 106 operations. All operators and operand will be separated by at least one white space.
Input
Input will be the multiline While program. You have to read it to the end of file.
Output
At the end of program, you have to print each variable's name and its value, in different lines, sorted by the lexicographical order of name.
Sample Input #00
fact := 1 ;
val := 10000 ;
cur := val ;
mod := 1000000007 ;
while ( cur > 1 )
do
{
fact := fact * cur ;
fact := fact - fact / mod * mod ;
cur := cur - 1
} ;
cur := 0
Sample Output #00
cur 0
fact 531950728
mod 1000000007
val 10000
Sample Input #01
a := 10 ;
b := 100 ;
if ( a < b ) then
{
min := a ;
max := b
}
else {
min := b ;
max := a
}
Sample Output #01
a 10
b 100
max 100
min 10
Explanation
Sample Case #00: This programs calculate the factorial of a number. Here it calculate the value of 10000! % (10^9+7)
using while statement. Using the property a % b == a - (a/b)*b
we calcuated the modulo of solution.
Sample Case #01: This program finds the maximum and minimum of a
and b
using if else statement.