What I learned writing a language in C
For CSE341 at GTÜ, a friend and I built ANS, a small language for chemistry. You declare elements, compounds and reactions, and it checks that a reaction is balanced before anything runs.
We wrote it in plain C11 with no dependencies: a hand-written lexer, a recursive-descent parser, a type checker and a tree-walking interpreter.
Three things stuck with me:
- Write the grammar first. Once the EBNF was complete, the parser almost wrote itself. Every time we skipped this step, we paid for it later.
- Catch errors before running. A two-pass type checker meant that an unbalanced reaction failed at check time, with a clear message, instead of producing a wrong number.
- Domain tokens are worth it. Treating
H2OandkJ/molas their own token types made the rest of the pipeline much simpler.
It's a small language, but it changed how I read every other one.