Professional Documents
Culture Documents
Class 08 B
Class 08 B
Class 08 B
de
Syntax analysis
1. Introduction
2. Parsing techniques and derivations
3. Verifying the language generated by a grammar
4. Preprocessing grammars
5. Problems with grammars
6. Top-down parsing
7. Automatic top-down parsing
8. Table-driven parser
9. Bottom-up parsing
10. Operator precedence
11. LR(0) parsing
12. From SLR to LR(k)
13. LR(1) parsers
14. LALR parsers
TECHNISCHE HOCHSCHULE DEGGENDORF
2
www.th-deg.de
Syntax analysis
1. Introduction
2. Parsing techniques and derivations
3. Verifying the language generated by a grammar
4. Preprocessing grammars
5. Problems with grammars
6. Top-down parsing
7. Automatic top-down parsing
8. Table-driven parser
9. Bottom-up parsing
10. Operator precedence
11. LR(0) parsing
12. From SLR to LR(k)
13. LR(1) parsers
14. LALR parsers
TECHNISCHE HOCHSCHULE DEGGENDORF
6
www.th-deg.de
Left factoring
• Useful for creating grammars for top-down parsers:
– Choice between alternative productions may be unclear
– rewrite grammar so that decision is postponed to a point
where enough input has been gathered
• Example:
– Original grammar:
A → αβ1 | αβ2
(which β to use? β1 or β2?)
– Left factored:
A → α A'
A' → β1 | β2
Left factoring
• Algorithm LeftFact (=4.21)
• Input: Grammar G
• Output: Equivalent left-factored grammar
1.While ∃ nonterminal A with several productions using the same prefix:
1. Determine longest common prefix α in productions
A → α β1 | … | α βn | γ1 | … | γm
2.If α≠ε:
1.Replace
A → α β1 | … | α βn | γ1 | … | γm
by
A → α A' | γ1 | … | γm
A' → β1 | … | βn
Left factoring
• Example:
A → if E then S | if E then S else S | a
E→b
Here, it is not immediately clear on reading the input (up to S),
which production to use – should we read on or produce the "if"?
– α=if E then S
– Replace
A → if E then S | if E then S else S | a
by
A → if E then S A' | a
A' → ε | else S
– Result:
A → if E then S A' | a
A' → ε | else S
E→b
TECHNISCHE HOCHSCHULE DEGGENDORF
12