Professional Documents
Culture Documents
Lecture II
Lecture II
Part II
Writing Grammars
Example:
SÆ (S) S | ε
Generates all strings of balanced parentheses
S0 … Si … f
Path labeled ai Path labeled ambn
m+j = n
m+i ≠ n
Elimination of Ambiguity
Ambiguous Grammar
• A Grammar is ambiguous
g if there are multiple
p p parse
trees for the same sentence
• For the most parsers, the grammar must be
unambiguous
Unambiguous
U bi grammar
unique selection of the parse tree for a sentence
Disambiguation
• Express Preference for one parse tree over others
– Add disambiguating rule into the grammar
Dangling-else grammar
Dangling-else grammar
Dangling-else grammar
Left Recursion
Immediate Left Recursion Elimination: example
• Grammar
EÆE+T|T
TÆT *F|F
F Æ ( E ) | id
• Left
L ft recursion
i Eliminated
Eli i t d
E Æ T E'
E' | ε
E' Æ + T E
E
T Æ F T'
T' Æ * F T' | ε
F Æ ( E ) | id
Left Recursion in More Than One Step
Left Recursion in More Than One Step
Left Recursion in More Than One Step
Left Recursion in More Than One Step
Left Recursion in More Than One Step
Left Recursion in More Than One Step
Left Recursion in More Than One Step
Left Recursion in More Than One Step
Next Form
Left Recursion in More Than One Step
Algorithm for Eliminating Left Recursion
Left Factoring
Left Factoring