Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous...

Click here to load reader

download Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars.

of 17

Transcript of Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous...

Ambiguity

Lecture 15Naveen Z QuazilbashAmbiguityOverviewS-GrammarsAmbiguity in GrammarsAmbiguous grammars and Unambiguous GrammarsS-grammarsSimple grammars a.k.a s-grammars.Rules for s-grammars:Productions of the form AsB1B2B3.Bn,where s is a symbol and B1B2B3.Bn are variables.For each variable A and symbol s, there is at most one production of the form: AsB1B2B3.Bn

S-grammarsFor example, we can not haveAaBCAaCDBecause we must only have one Aa production.

S-grammarsExample of a simple grammar:SaA|bBCAaA|bbBbB|aCCcC|aw=bbaccaParsing:S=>bBC=>bbBC=>bbaC=>bbacC=>bbaccC=>bbaccaS-grammarsWe use leftmost derivation.At each step we know which production to apply, by looking at the next symbol in w.

Remark: Thus, the derivation takes linear time; that is, the time is proportional to |w|.AmbiguitySome important notesApplications of CFGs (like Parsers, DTDs and markup languages) often rely on the grammar to provide the structure of files.Tacit Assumption: A grammar uniquely determines a structure for each string in its language.Problem: However, thats not always possible.Solution: Redesign the grammar!!solutionOptions:Redesign possibilityRedesign impossibility ~ Inherently ambiguous (beyond our scope)

So, first, lets try to understand the problem now!AmbiguityExample 1:SSA | AAaAb | w=aabbGive it lm and rm derivations and also draw parse trees for both lm and rm derivations.

Ambiguitylm:S=>SA=>AA=>aAbA=>aaAbbA=>aabbA=>aabbrm:S=>SA=>SaAb=>SaaAbb=>Saabb=>Aaabb=>aabb

AmbiguityExample 2:EIEE+EEE*EE(E)Ia|b|Ia|Ib|I0|I1w=E+E*EGive it lm and rm derivations and also draw parse trees for both lm and rm derivations.

Ambiguityrm: E=>E+E=>E+E*Elm: E=>E*E=>E+E*E

Ambiguity in GrammarsThere is a significant difference between the two derivations.Whats that?Hence, this grammar is not a good one for providing unique structures.In particular, while it can give correct groupings to arithmetic expressions, it also gives them incorrect groupings.

Ambiguity in GrammarsLets have a look at another very important factor, i-e what causes ambiguity?Multiplicity of derivations?

//discussionAmbiguous Grammars and Unambiguous GrammarsA CFG, G={V, T, P, S} is ambiguous if there is at least one string w in T* for which we can find two different parse trees, each with root labeled S and yield w.If each string has at most one parse tree, then the grammar is unambiguous.Unambiguous Grammars - Avoiding ambiguityNext class