Introduction
Compilers and interpreters start with the same steps: break text into tokens, understand precedence and evaluate. This project builds a complete pipeline for arithmetic expressions — a compact introduction to compiler design.
Problem statement
let x = …), the functions sqrt, abs, min, max and pow and the constant pi. Respect precedence and associativity (^ is right-associative), show the RPN form on request, and report errors such as unmatched parentheses, unknown variables, missing operands and division by zero with useful messages.Objectives
- Write a lexer that produces typed tokens with positions
- Implement Dijkstra's shunting-yard algorithm
- Evaluate postfix expressions with a stack
- Distinguish unary and binary minus
Features
- Correct precedence and associativity
- Unary minus
- Variables and functions with arity checks
:rpnto show postfix form,:varsto list variables- Column-accurate error messages
Concepts demonstrated
- Lexical analysis
- Operator precedence parsing
- Stack machines
- Error handling
System architecture
lexer.c turns the input into Tokens (number, identifier, operator, parentheses, comma). eval.c converts the token list to RPN with an operator stack, using the previous token to decide whether - is unary, and then evaluates the RPN with a value stack, applying functions by arity. main.c handles let, commands and printing.
Folder structure
Complete source code
Makefile
What each file does
- src/lexer.h
- Token types and the tokenizer API.
- src/lexer.c
- Character-level scanning into tokens.
- src/eval.h
- The evaluator API and variable functions.
- src/eval.c
- Variables, shunting-yard conversion, RPN evaluation and function calls.
- src/main.c
- The REPL,
letassignments and commands. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
2 + 3 * 4 (2 + 3) * 4 2 ^ 3 ^ 2 -3 ^ 2 10 % 4 + 7 / 2 let r = 2.5 pi * r ^ 2 :rpn (1 + 2) * -3 max(4, sqrt(81)) + min(2, abs(-7)) let total = r * 4 :vars 1 / (3 - 3) 2 * (3 + 4 x + 1 4 + * 2 let 9x = 1 :quit
Testing
- Diff with
tests/expected-output.txt. - Check precedence: 2 ^ 3 ^ 2 = 512 (right-associative) and -3 ^ 2 = -9.
- Feed malformed input such as
4 + * 2and2 * (3 + 4and read the error messages.
Common errors
-3 ^ 2 gives 9
Unary minus must have lower precedence than ^ (as in mathematics), so the power is applied first.
Function results wrong when nested
Push function names on the operator stack and pop them to the output when their closing parenthesis is reached.
Possible improvements
- Comparison and logical operators
- User-defined functions
- Integer and fraction modes
Additional challenges
- 1Build an abstract syntax tree instead of RPN and print it.
- 2Add implicit multiplication (
2pi,3(4+5)). - 3Compile expressions to a tiny bytecode and run it in a loop.
Built and tested it yourself? Mark it to update your dashboard.