Professional Documents
Culture Documents
CONSTRUCTION
Lecture 2
TWO-PASS COMPILER
errors
2
TWO-PASS COMPILER
3
FRONT END
4
THE FRONT-END
source tokens IR
scanner parser
code
errors
Modules
• Scanner
• Parser 5
SCANNER
source tokens IR
scanner parser
code
errors
6
SCANNER
8
SCANNER
9
PARSER
source tokens IR
scanner parser
code
errors
10
PARSER
11
CONTEXT-FREE GRAMMARS
• Context-free syntax is specified
with a grammar G=(S,N,T,P)
• S is the start symbol
• N is a set of non-terminal symbols
• T is set of terminal symbols or words
• P is a set of productions or rewrite rules
12
CONTEXT-FREE GRAMMARS
14
CONTEXT-FREE GRAMMARS
15
DERIVATION
Production Result
1. goal → expr
goal
2. expr → expr op term expr
3. |
term expr op term
4. term → number expr op y
5. |
id expr – y
6.
7.
op →+
|
expr op term – y
- expr op 2 – y
expr + 2 – y
term + 2 – y
x+2–y 16
THE FRONT END
17
PARSE
Production Result
goal
1 expr
2 expr op term
5 expr op y
7 expr – y
2 expr op term – y
4 expr op 2 – y
6 expr + 2 – y
3 term + 2 – y
5 x+2–y 18
HOMEWORK
What is Parse trees?
How can we use Parse trees to generate CFGS?
Consult some examples to generates strings using Parse trees.