编译原理复习
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
复习
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
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
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)
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
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]
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
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).
– Predictive – lookahead
Parser
• Top-down:
– LL(1) grammar – FirstSet and FollowSet
• nullable nontermianls
– Left Factor and Left Recursion
LL(1) Grammar
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 ·
Intermediate code Code generator Target code
Target code optimizer
Target code
Scanner
• Regular expression + automaton • NFA,DFAs • Convert NFAs to DFAs.
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
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
•
– –
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) = Φ.
a
+
b
$
Input
X Y S $
‘$’ is used to mark the bottom of the stack
predictive parser
பைடு நூலகம்
Output
parsing table
LL(1) Parse Tables
•
–
The Construction of Parsing Tables
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
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
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
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)
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
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]
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
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).
– Predictive – lookahead
Parser
• Top-down:
– LL(1) grammar – FirstSet and FollowSet
• nullable nontermianls
– Left Factor and Left Recursion
LL(1) Grammar
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 ·
Intermediate code Code generator Target code
Target code optimizer
Target code
Scanner
• Regular expression + automaton • NFA,DFAs • Convert NFAs to DFAs.
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
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
•
– –
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) = Φ.
a
+
b
$
Input
X Y S $
‘$’ is used to mark the bottom of the stack
predictive parser
பைடு நூலகம்
Output
parsing table
LL(1) Parse Tables
•
–
The Construction of Parsing Tables
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