当前位置:文档之家› 编译原理复习

编译原理复习

复习
Source code
Scanner
Compiling process
Tokens Parser Syntax Tree
Semantic analyzer Annotated Tree Source code optimizer
Literal table
Symbol table
Error handler Auxiliary components that interact with some phases
NFA Regular expressions Minimiz
DFA
Lexical Specification
Table-driven Implementation of DFA
Parser
• • • • CFG: production Derivation, leftmost and rightmost parse tree, abstract syntax tree Top-down and bottom-up
5 digit val=4 base=8 4
3
Code generation
• •
– –
Intermediate code generation TAC
Attribute grammar for generating three-address code Code Generation for If- and While- Statements
Schematic Form of LR parser
Input a1 … ai … an $
Stack
Sm Xm Sm-1 Xm1 … S0 $
LR Parsing Program
Output
action
goto
Parsing Table
A LR(0) DFA
E→T+E· S→E E → T; E→T+E T → int T → ( E)
Parser
• LR parser
– Shift and reduce – Parsing table: action and goto – State
• Item • LR(0) DFA: Closure Operation and Goto Operation
– SLR(1) parser
• Only reduce A → ω if the next token t is in FOLLOW(A).
Intermediate code Code generator Target code
Target code optimizer
Target code
Scanner
• Regular expression + automaton • NFA,DFAs • Convert NFAs to DFAs.
– Predictive – lookahead
Parser
• Top-down:
– LL(1) grammar – FirstSet and FollowSet
• nullable nontermianls
– Left Factor and Left Recursion
LL(1) Grammar

– –
A grammar is LL(1) grammar if the following conditions are satisfied:
For each production A → α1|α2|…|αn , for all i and j, 1≤i, j ≤ n, i≠j , First(αi) ∩First(αj) = Φ For each nonterminal A such that First(A) contains ε, First(A) ∩Follow(A) = Φ.
E → T ·; ② E→T·+E
;
T
T
T → (·E) ④ E → · T; E→·T+E T → · int T → · (E)
(
E → T ;· ⑧
)
Semantic analysis
• Attribute Grammar
– SDD – Dependency Graphs- Evaluation Order – Synthesized and Inherited Attributes
Parser
• Bottom-up
– Rightmost derivation traced in reverse(reduction) – Right sentential form – Handle: a string is a substring in right sentenial form that matches the right hand side of a production – viable prefix
Parser
• Top-down:
– Recursive-Descent parser and LL(1) parser – LL(1) parser
• LL(1) Parsing Tables
‘$’ is used to mark the end of the input strings
Parsing stack stores symbols waiting to matched in parsing the be
Repeat the following two steps for each nonterminal A and production choice A→ α 1. For each token ‘a’ in First(α), add A→ α to the entry M[A,a] 2. if ε is in First(α), for each element ‘a’ of Follow(A) (token or $), add A→ α to M[A,a]
a
+
b
$
Input
X Y S $
‘$’ is used to mark the bottom of the stack
predictive parser
Output
parsing table
Hale Waihona Puke LL(1) Parse Tables


The Construction of Parsing Tables
start

E
S→E ·
E

+ int
E→T+·E ③ E → · T; E→·T+E T → · int T → · (E)
int T ( int
T → (E)·
)

T → (E·) ⑤
E
S→ · E ① E → · T; E→·T+E T → · int T → · (E)
⑦ T → int ·
Attribute computations for the number 345o
based-num val=229
num val=28*8+5=229 base=8 num val=3*8+4=28 base=8 num val=3 base=8 digit val=3 base=8 digit val=5 base=8 basechar base=8 o
相关主题