- Practice
- Functional Programming
- Interpreter and Compilers
- While Language

# While Language

# While Language

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 authors^{1,2,3} have used. Below is the description of grammar that we will use.

- x, y ∈
**Var**(variables) n ∈

**Num**(numerals/integers)op

_{a}∈**Op**(arithmetic operators)_{a}

op_{a}::=`+`

|`-`

|`*`

|`/`

op

_{b}∈**Op**(boolean operators)_{b}

op_{b}::=`and`

|`or`

op

_{r}∈**Op**(relational operators)_{r}

op_{r}::=`>`

|`<`

a ∈

**AExp**(arithmetic expressions)

a ::= x | n | a_{1}op_{a}a_{2}| ( a )b ∈

**BExp**(boolean expressions)

b ::=**true**|**false**| b_{1}op_{b}b_{2}| a_{1}op_{r}a_{2}| ( b )S ∈

**Stmt**(statements)

S ::= x := a | S_{1}**;**S_{2}|**if**b**then {**S_{1}**} else {**S_{2}**}**|**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*10^{18}].

All test cases are *valid* programs. All of them will execute no more than 10^{6} 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.