Ambiguity & Precedence Grammars

advertisement
Discussion #4
Ambiguity &
Precedence Grammars
Discussion #4
1/17
Topics
• Ambiguity
• Precedence Grammars
• Grammar Reductions
Discussion #4
2/17
Problems with Grammars
• We are heading towards grammars that are
reasonable, get the job done, and for which
we can find efficient parsing algorithms.
• Not all grammars are usable!
–
–
–
–
Ambiguous
“Silly”
Have unproductive non-terminals
Have unreachable rules
Discussion #4
3/17
Ambiguous Grammars
• A grammar for which there are two
different parse trees for the same terminal
string is said to be ambiguous.
• We can show that a grammar is ambiguous
by giving two parse trees for the same
terminal string.
Discussion #4
4/17
An Ambiguous Grammar
={ED|(E)|E+E|E–E|E*E|E/E,
D0|1|…|9 }
E
E
E
E
+
E
*
E
D
E *
E
E +
E
D
1
D
D
D
D
3
2
2 *
3
3
1
1
2
+ 2 *
3
1 +
Discussion #4
5/17
Ambiguity & Multiple Meanings
•
•
•
•
Precedence: (1+2)*3  1+(2*3)
Associativity: (32)1  3(21)
Different if-then-else nestings
…
Discussion #4
6/17
Fixing Precedence Ambiguity
• Observe: Operators lower in the parse tree
are executed first in an expression tree.
• Observe: Operators executed first have
higher precedence.
• Fix: We make our grammar correctly
represent precedence levels by introducing a
new non-terminal symbol for each
precedence level.
Discussion #4
7/17
Example: Fixing Precedence
Ambiguity
ET|E+T|E–T
 ={ED|(E)|E+E|
E–E|E*E|E/E,
D0|1|…|9 }
TF|T*F|T/F
FD|(E)
D0|1|…|9
E
E
E
E
E
+
E
*
E
E
D
E
*
E
E
+
E
D
T
1
D
2
D
3
D
1
D
2
T
*
F
3
F
D
Discussion #4
T
+
1
F
D
D
3
2
8/17
Fixing Associative Ambiguity
• Left recursion yields left associativity.
• Right recursion yields right associativity.
Discussion #4
9/17
Left Associativity ~ Left Recursion
ED|DE
3 21
E
321
E
D  E
E  D
3
D  E
2
Discussion #4
ED|ED
E  D
D
D
1
3
1
2
10/17
Right Associativity ~ Right Recursion
ED|DE
2
3
2
ED|ED
= 232
E
232
E
D  E
E  D
2
D E
3
Discussion #4
E D
D
D
2
2
2
3
11/17
Adding the Power Operator
E  T | E+T | ET
E  T | E+T | ET
T  F | T*F | T/F
T  P | T*P | T/P
F  D | (E)
P  F | FP
F  D | (E)
Discussion #4
12/17
Grammar Reductions:
“Silly” Rules
Some grammars have “silly” constructions
={AA|a}
AAa
AAAAa
Always ambiguous; rewrite as  = { A  a }.
Discussion #4
13/17
Grammar Reductions:
Unreachable Rules
Some grammars have unreachable rules.
 = {S  aABb ,
A  a | aA ,
B  b | bBD ,
C  cD ,
De}
Discussion #4
can’t be reached
14/17
Least Fixed Point Algorithm
Finds Unreachable Rules
1.
2.
3.
4.
Initialize the set of reachable non-terminals R with the start symbol.
For each round, if R includes the lhs of a production rule, add the nonterminals in the rhs to R.
Loop on #2 until there are no changes to R.
Rules whose lhs’s are non-terminals in VN minus the non-terminals in
R constitute the set of unreachable rules.
= { S  aABb ,
A  a | aA ,
B  b | bBD ,
C  cD ,
De}
Discussion #4
Initialize: R = {S}
Round 1: R = {S, A, B}
Round 2: R = {S, A, B, D}
Round 3: R = {S, A, B, D}
Done: no change: VN – {S, A, B, D} = {C}
15/17
Grammar Reductions:
Unproductive Non-terminals
Some grammars have non-terminals that can never
become all terminals (unproductive, inactive).
 = { S  aABb ,
A  bC ,
B  d | dB ,
C  eC }
C never produces all terminals.
C  eC  eeC  eeeC  … enC
A also because it always produces C
A  bC  beC  beeC  … benC
S also because it always produce A
S  aABb  aAbb  abCbb  …
Discussion #4
16/17
Least Fixed Point Algorithm
Finds Unproductive Non-terminals
1. Start with the set of terminals T.
2. For each round, if T covers a rhs of a production rule, add the lhs
to T.
3. Loop on #2 until there are no changes to T.
4. The alphabet of terminals and non-terminals, V, minus T is the
set of unproductive non-terminals.
 = { S  aABb ,
A  bC ,
B  d | dB ,
C  eC }
Discussion #4
Initialize: T = {a, b, d, e}
Round 1: T = {a, b, d, e, B}
Round 2: T = {a, b, d, e, B}
Done: no change: {a, b, d, e, A, B, C, S} – T = {A, C, S}
17/17
Download