G ={V, , R, S}
V = {S, G, D, I}, = {0, 1, 2, 3,., 9, +, -}
Derivation Trees
Derivation Trees
Derivation Trees
Derivation Trees
Derivation Tree
Ambiguity
Consider the grammar for arithmetic expressions involving
addition and multiplication operators:
In the figure, the parse tree to the left gives the addition
operator precedence over multiplication. In other words, an
expression such as 3+5 9 is evaluated as (3+5) 9 with a
result of 72. Whereas, the tree to the right, does what is
considered the standard practice in programming
languages, i.e. giving precedence over +. The previous
example would be evaluated as 3+(5 9) resulting in 48.
If G is the grammar
Show that G is ambiguous.
Ambiguity
To prove that G is ambiguous, we need to find a w L(G),
which is ambiguous . Consider the word abababa.
Ambiguity
Removing Ambiguity
What about a + a a ?
Removing Ambiguity
Simplification of CFG
a) Construction of Wi
W1={S}.
Wi+1=Wi {X (V ) | there exists a production
A with A Wi and containing the symbol X }
At some point Wk=Wk+1.
b) Construction of V, , P.
V=V Wk , = Wk , P={A | A Wk }
Simplification of CFG
W3={S,A,B} {a,b}
V= {S,A,B} , ={a,b}
P= S AB, A a, B b
Simplification of CFG
Answers:
1) S CA, A a, C b
2) S aAa, A Sb|bCC, C abb
Normal Forms for CFG
Reduction to CNF
Reduction to GNF
Rename
The productions are :
Normal Forms for CFG
Normal Forms for CFG
Normal Forms for CFG