SimuVeda/ Computer Science

Computer Science · Simulator 067

Compiler Pipeline

Type an arithmetic expression such as 2 * (3 + x) ^ 2. Watch a tiny compiler split it into tokens, parse it into a syntax tree that respects precedence, generate stack-machine instructions, and run them step by step.

CollegeCollege compilers

Experiments 0 of 0 Challenge not tried

How the model works

  1. Lexer: scans characters into tokens: numbers, names, operators and parentheses. It rejects unknown characters.
  2. Parser: a recursive-descent parser for the grammar
    expr → term (('+'|'−') term)*, term → unary (('*'|'/') unary)*, unary → '−' unary | power, power → atom ('^' unary)?, atom → number | name | '(' expr ')'.
    The grammar encodes precedence (^ above unary minus above * / above + −), so −2^2 = −4, and ^ is right-associative.
  3. Code generation: a post-order walk of the tree emits instructions for a stack machine (PUSH, LOAD, ADD, MUL, POW, NEG…).
  4. Execution: the virtual machine pops operands and pushes results. The final stack value is the answer.

Variables x and y take the values set in the controls.

Deliberately restricted language: numbers, x, y, + − * / ^ and parentheses only. No function calls and no assignment. Nothing is evaluated with the browser's own eval.

Guided experiments

  1. Precedence. 2 + 3 * 4. Prediction: the tree puts * below +, so the result is 14, not 20.
  2. Right-associative power. 2 ^ 3 ^ 2. Prediction: parsed as 2 ^ (3 ^ 2) = 512, not (2 ^ 3) ^ 2 = 64.
  3. Syntax error. 3 * (4 + . Prediction: the parser reports what it expected and where, and no code is generated.

Challenge

How many instructions does the stack machine run for (x + 1) * (x − 1)? What is the deepest the stack gets?

Reveal the explanation

LOAD x, PUSH 1, ADD, LOAD x, PUSH 1, SUB, MUL: 7 instructions. The stack peaks at 3 values (the first sum, then x and 1 for the second bracket). Step through it to watch the stack.

References

  • Aho, Lam, Sethi & Ullman, Compilers: Principles, Techniques, and Tools, ch. 2.
  • Nystrom, Crafting Interpreters (free online), ch. 4–6 and 14–17.

Related simulators

Worked example

Expression: x * (y + 3) − 2 with x = 4 and y = 2.

Tokens: x, *, (, y, +, 3, ), −, 2: nine tokens.

Syntax tree: the root is −, because it has the lowest precedence outside brackets. Its left child is * with children x and (y + 3); its right child is 2.

Stack code (post-order walk of the tree): LOAD x, LOAD y, PUSH 3, ADD, MUL, PUSH 2, SUB. Seven instructions; the stack is deepest, 3 values (4, 2, 3), just before ADD.

Run it: ADD gives 5, MUL gives 20, SUB gives 18. Type the expression with x = 4 and y = 2 and step through the code.

Common questions

What are the main stages of a compiler?

Lexing splits the text into tokens, parsing builds a syntax tree from the grammar, semantic checks verify types and names, and code generation turns the tree into instructions, usually with optimisation in between.

How does a parser handle operator precedence?

The grammar has a level for each precedence, so * and / are grouped before + and −. A recursive-descent parser calls one function per level, which puts higher-precedence operators lower in the tree.

What is a stack machine?

A machine whose instructions take their inputs from the top of a stack and push the result back. The Java virtual machine, .NET and WebAssembly work this way.

Keyboard shortcuts

Space
Play or pause
S
Step
R
Reset
F
Full screen
?
Show this list