• Falsches Dokument? Kostenlos tauschen
  • Geschrieben von Student*innen, die bestanden haben
  • Sofort verfügbar nach Zahlung
  • Online lesen oder als PDF
Verkaufen
Wo studierst du
Deine Sprache
Document preview thumbnail
Vorschau 2 aus 5 Seiten
Study guide

Compiler Design: Lexing, Parsing and Code Generation

Document preview thumbnail
Vorschau 2 aus 5 Seiten

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.

Inhaltsvorschau

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

Dokument Information

Studium
Unbekannt
Hochgeladen auf
3. dezember 2026
Anzahl der Seiten
5
geschrieben in
2026/2027
Typ
Study guide
$7.98

Falsches Dokument? Kostenlos tauschen Innerhalb von 14 Tagen nach dem Kauf und vor dem Herunterladen kannst du ein anderes Dokument wählen. Du kannst den Betrag einfach neu ausgeben.
Geschrieben von Student*innen, die bestanden haben
Sofort verfügbar nach Zahlung
Online lesen oder als PDF

Verkauft
0
Anhänger
0
Sachen
225
Zuletzt verkauft
-



Warum sich Studierende für Stuvia entscheiden

on Mitstudent*innen erstellt, durch Bewertungen verifiziert

Geschrieben von Student*innen, die bestanden haben und bewertet von anderen, die diese Studiendokumente verwendet haben.

Nicht zufrieden? Wähle ein anderes Dokument

Kein Problem! Du kannst direkt ein anderes Dokument wählen, das besser zu dem passt, was du suchst.

Bezahle wie du möchtest, fange sofort an zu lernen

Kein Abonnement, keine Verpflichtungen. Bezahle wie gewohnt per Kreditkarte oder Sofort und lade dein PDF-Dokument sofort herunter.

Student with book image

“Gekauft, heruntergeladen und bestanden. So einfach kann es sein.”

Alisha Student

Arbeitest du an deiner Quellenangabe?

Erstelle korrekte Quellenangaben in APA, MLA und Harvard mit unserem kostenlosen Zitiergenerator.

Arbeitest du an deiner Quellenangabe?

Häufig gestellte Fragen