From characters to a useful representation
Follow a tiny expression through tokens, a syntax tree, and semantic checks.
What you’ll learn
- Distinguish lexing from parsing
- Read a simple syntax tree
- Explain why valid syntax can still be invalid meaning
Begin with the language you intend to accept
A compiler or interpreter needs a definition of its input language. Start with a deliberately small expression language containing integer literals, addition, multiplication, and parentheses. Write several accepted examples and several rejected examples before implementing anything. These examples expose decisions about precedence, associativity, whitespace, and errors. A small, explicit language gives you a reliable place to learn the pipeline without mixing in every feature of a production programming language.
Turn characters into tokens
A lexer groups characters into meaningful units such as a number, an identifier, or an operator. For the text 2 + 3 * 4, a useful token stream contains a number, a plus operator, another number, a multiplication operator, and the last number. Tokenization should also track locations well enough to explain a later failure. Recognizing a sequence of digits is different from deciding how that number participates in an expression.
Build a tree that preserves structure
A parser organizes tokens according to grammar rules. If multiplication has higher precedence than addition, the syntax tree for this example places the multiplication beneath the addition: the right operand of plus is the result of multiplying three and four. Parentheses can request a different grouping. A tree makes this relationship explicit and lets later stages work with structure rather than repeatedly interpreting the original characters.
Add
├── Number(2)
└── Multiply
├── Number(3)
└── Number(4)Check meaning beyond punctuation
A parser may accept the shape of a function call while another stage rejects an unknown function name or the wrong argument types. These are different categories of error and should lead to different explanations. A useful diagnostic names the relevant construct and source location, then tells the reader what expectation was violated. Do not continue producing arbitrary results after an error unless the recovery strategy is intentional and tested.
For the malformed input 2 + * 4, report that an operand was expected after plus and point to the unexpected multiplication token. This is a parsing problem before any arithmetic can be evaluated.
Evaluate before optimizing
A tree-walking evaluator is often a good first execution model for a small language. It provides a baseline for checking a later bytecode or native-code implementation. Test nested expressions, boundary values, malformed inputs, and precedence explicitly. Only then introduce transformations, making sure each preserves the meaning promised by the language. Optimization is easier to assess when a simple implementation can still explain the expected result for a small example.
Try it yourself
With multiplication taking precedence, draw or describe the tree for 2 + 3 * 4 and compute the result.
Show a possible solution
The root is addition with left child 2 and right child multiplication of 3 and 4. Evaluate the multiplication to 12, then add 2 to obtain 14.
Check your understanding
Try 11 questions across compilers and language implementation. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.