• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Start selling Create your account
Document preview thumbnail
Preview 2 out of 5 pages
Study guide

Compiler Design: Lexing, Parsing and Code Generation

Document preview thumbnail
Preview 2 out of 5 pages

A study guide to compiler design: lexical analysis with regular expressions and finite automata, parsing with LL and LR techniques including FIRST and FOLLOW sets and ambiguity, semantic analysis and symbol tables, intermediate representations including three-address code and SSA, optimisation with dataflow analysis, and code generation with register allocation. Contains two comparative tables, three worked examples, answer technique, five model answers, a checkpoint bank and a glossary.

Content preview

Compiler Design: Lexing, Parsing and Code Generation
From source text to machine instructions, one translation phase at a time
Computer Science | Undergraduate (Level 5) | December 2026


WHAT IS INSIDE

1. Core Concepts and Theoretical Frameworks
2. Lexical analysis and finite automata
3. Parsing: LL, LR and ambiguity
4. Semantic analysis and intermediate representation
5. Optimisation and code generation
6. Step-by-Step Worked Examples
7. Comparative Summary Table
8. Common Exam Pitfalls and Answer Technique
9. Practice Exam Questions with Model Answers
10. Glossary of Exam-Ready Definitions

1. Core Concepts and Theoretical Frameworks
Compiler design is examined as a pipeline in which each phase has a formal model and a clear contract with
the next. The table is the revision skeleton: for each phase, know the input, the output and the algorithm.

Phase Input Output Formal tool
Lexical analysis Character stream Token stream Regular expressions and finite
automata
Syntax analysis Token stream Parse tree Context-free grammars, LL
and LR tables
Semantic analysis Parse tree Annotated tree Attribute grammars, type
rules
Intermediate code Annotated tree Three-address code or SSA Linear IR
Optimisation Intermediate code Equivalent, faster code Dataflow analysis, graph
algorithms
Code generation Intermediate code Assembly Instruction selection, register
allocation
Linking and loading Object files Executable image Relocation, symbol resolution

Two ideas unify the subject. The first is that regular languages are enough for the lexical level and
context-free grammars for the syntactic level, which is why the two phases use different tools. The second
is that optimisation must preserve meaning; every transformation is justified by an equivalence, and the
proof of that equivalence is the hard part.

2. Lexical analysis and finite automata
The lexer converts a character stream into tokens such as identifiers, keywords, numbers and operators,
discarding whitespace and comments. Each token class is described by a regular expression, and the set of
expressions is compiled into a deterministic finite automaton. The automaton is built by Thompson's
construction into a non-deterministic automaton and then converted to a deterministic one by the subset
construction.




Compiler Design: Lexing, Parsing and Code Generation Page 1 of 5

,Compiler Design: Lexing, Parsing and Code Generation


Longest match and rule priority resolve ambiguity: the lexer takes the longest string that forms a token, and if
two rules match the same length, the rule listed first wins. This is why a keyword must be listed before the
identifier rule, and why a language with a reserved word cannot use it as a variable name without an escape.
Lexical errors are reported with a position and a recovery action. The lexer should never crash on malformed
input; it should skip to the next plausible token boundary and continue, because a single error should not
produce a cascade of spurious diagnostics. The classic tools are lex and flex, which generate the automaton
from a specification.

Construct Recognises Determinism Use
Regular expression Regular languages Declarative Token specification
NFA Regular languages Non-deterministic Thompson construction
DFA Regular languages Deterministic Table-driven lexer
Subset construction Converts NFA to DFA Algorithm Automaton generation
DFA minimisation Reduces states Algorithm Table compression


3. Parsing: LL, LR and ambiguity
A context-free grammar defines the syntax with productions. A derivation is a sequence of replacements from
the start symbol; a leftmost derivation always replaces the leftmost non-terminal. A parse tree records the
structure, and the language of the grammar is the set of strings with a derivation from the start symbol.
Top-down parsing predicts a production from the next token. An LL(1) parser uses a table indexed by
non-terminal and lookahead, built from the FIRST and FOLLOW sets. It cannot handle left recursion, which
must be eliminated, and it cannot handle a grammar where two productions for the same non-terminal start
with strings that can derive the same terminal, which is a FIRST/FIRST conflict.
Bottom-up parsing shifts tokens onto a stack and reduces when the top of the stack matches the right side of a
production. LR parsers are stronger than LL parsers and handle more grammars, including left recursion, but
the table is larger and the construction is more complex. SLR, LALR and canonical LR differ in how
precisely they compute the lookahead sets, with LALR being the practical compromise used by yacc and
bison.
• FIRST of a symbol is the set of terminals that can begin a string derived from it.
• FOLLOW of a non-terminal is the set of terminals that can appear immediately after it.
• Left recursion must be removed for LL parsing.
• Left factoring removes FIRST/FIRST conflicts.
• LR parsing handles a superset of LL grammars.
• Ambiguous grammars need precedence declarations or a rewrite.

4. Semantic analysis and intermediate representation
Semantic analysis checks what the grammar cannot express: that a variable is declared, that types match, that
a function is called with the right number of arguments, and that a break appears inside a loop. The symbol
table maps names to types and locations, and it is usually a hash table with a stack of scopes to handle
shadowing.
A syntax-directed translation attaches actions to productions and computes attributes. Synthesised attributes
flow up the tree, as in the type of an expression; inherited attributes flow down, as in the expected type of a
declaration. The type system is specified by rules, and a type checker is an implementation of those rules.
The intermediate representation is a machine-independent form that the optimiser and the back end share.
Three-address code gives each instruction at most one operator, with temporaries for intermediate values, and

Compiler Design: Lexing, Parsing and Code Generation Page 2 of 5

Document information

Study
Unknown
Uploaded on
December 3, 2026
Number of pages
5
Written in
2026/2027
Type
Study guide
£6.35

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
225
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions