Interpreter
Time limit1sMemory limit256 MB
Run small integer programs with arithmetic, comparisons, if/else branches, while loops, and print statements.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, String, Stack
- Solved
- No attempts yet
Problem
Write an interpreter for the small programming language described below.
Data types
The language has a single data type, a 32-bit signed integer.
Operators and statements that read a value as a boolean (!, &&, ||, if, while) treat 0 as false and every other value as true.
Operators that produce a boolean (!, <, <=, >, >=, ==, !=, &&, ||) return 0 for false and 1 for true.
Variables
A variable name is a single lowercase letter from a to z. Every variable is initialized to 0 at the start of each program.
Whitespace
Each simple statement (set or print) and each part of a compound statement (if, else, end if, while, end while) sits on its own line. There are no blank lines.
Any amount of whitespace (a space or a tab) may appear before or after a token (an operator, a variable name, a keyword, a constant), but never inside a token. Whitespace is guaranteed only between two adjacent alphanumeric tokens.
Statements
A statement is one of the following.
If with else
if expression
statements
else
statements
end if
If expression is true (non-zero), run the statements in the first block. Otherwise run the statements in the second block. A block may be empty.
If without else
if expression
statements
end if
If expression is true (non-zero), run the statements in the block. Otherwise skip the block.
While
while expression
statements
end while
Evaluate expression. If it is true (non-zero), run the statements in the block and go back to the start of the while statement to evaluate expression again. Otherwise skip the block.
Assignment
set name = expression
Set the variable named name to the result of expression.
Output
print expression
Write the result of expression to standard output on its own line.
Expressions
An expression is a variable name, an integer constant (a sequence of digits whose value is between 0 and inclusive), or one of the operations in the table below.
Some operators bind more tightly than others. 1+2*3 is the same as 1+(2*3), while 1*2+3 is the same as (1*2)+3.
Inside one precedence level, binary operators group left to right and unary operators group right to left. 1+2-3 is the same as (1+2)-3, and !-x is the same as !(-x).
Division / truncates the quotient toward zero, and the remainder % takes the sign of the left operand. So -7/2 is -3 and -7%2 is -1.
Every final and intermediate value fits in a 32-bit signed integer, so no overflow check is needed. Division by 0 never happens, for either / or %.
Input
The input holds one or more programs. Each program begins with a line holding the number of lines () in that program, and the next lines hold the program itself. No line is longer than 100 characters. A line holding 0 ends the input.
Output
Print only the values produced by the print statements of the programs, one per line, in the order the statements run.