Compilers -- Practice Problems
Compilers — Practice Problems
10 MCQ questions covering lexical analysis, parsing (LL and LR), abstract syntax trees, intermediate representations, optimization, and code generation. Select an option to check your answer.
Intuition
A translator for translators: A compiler is like a multi-stage translation pipeline, it first breaks your code into words (lexing), then checks the grammar (parsing), then rewrites it for clarity (optimisation), and finally converts it to machine language. Each stage catches different errors.
Why it matters: Compilers turn human-readable code into efficient machine instructions. Understanding them explains why some code is faster than others, how language features work under the hood, and why certain errors only show up at runtime.
The key insight: Compiler optimisations must preserve what your program does, not how it does it, they can rearrange, combine, and eliminate code, but the observable behaviour must stay the same.
Lexical Analysis and Parsing
Intermediate Representations, Optimization, and Code Generation
Additional Compiler Questions
Common Mistakes
Confusing lexical analysis with parsing: Lexical analysis groups characters into tokens. Parsing groups tokens into a syntax tree. They’re different compilation phases.
Assuming all grammars can be parsed deterministically: Some grammars require backtracking or lookahead beyond one token. LL(1) and LR(1) are restricted grammar classes.
Forgetting that compiler optimisations must preserve program semantics: Optimisations can change HOW the program runs but not WHAT it computes. Don’t assume all transformations are valid.
Cross-References
- Theory of Computation: Automata and formal languages underlying compiler design.
- Algorithms: Graph colouring algorithms used in register allocation.