Katkat

Design a mini compiler/interpreter

Applied IntuitionSoftware Engineer · Onsite

Last 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:

  1. Tokenizes the input (lexer)
  2. Parses tokens into an AST (parser)
  3. 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