Design a mini compiler/interpreter
Applied IntuitionSoftware Engineer · OnsiteLast updated
This question evaluates understanding of language design and implementation skills, including lexical analysis, parsing into an AST, expression evaluation with operator precedence and unary operators, variable assignment semantics, and runtime error reporting.
Problem
Design and implement a mini compiler / interpreter for a small expression language.
You are given a program as a string (possibly multiple lines). The language supports:
- Integer literals, e.g.
42 - Variables and assignment, e.g.
x = 3 + 4 - Arithmetic:
+ - * /with standard precedence and parentheses - Unary minus, e.g.
-x - A
print(expr)statement that outputs an integer
Task
Build a pipeline that:
- Tokenizes the input (lexer)
- Parses tokens into an AST (parser)
- Evaluates the AST (interpreter) and produces the printed outputs in order
Clarifications
- Integer division truncates toward zero.
- All variables are global.
- Invalid syntax should raise a clear error.
Example
Program:
x = 10
print(x + 2 * 3)
print(-(x - 7))
Output:
16
-3
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.