Download presentation
Presentation is loading. Please wait.
Published byBuddy Warner Modified over 9 years ago
1
Lecture 15 Naveen Z Quazilbash Ambiguity
2
Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars
3
S-grammars Simple grammars a.k.a s-grammars. Rules for s-grammars: Productions of the form A sB 1 B 2 B 3 ….B n,where s is a symbol and B 1 B 2 B 3 ….B n are variables. For each variable A and symbol s, there is at most one production of the form: A sB 1 B 2 B 3 ….B n
4
S-grammars For example, we can not have A aBC A aCD Because we must only have one “A a…” production.
5
S-grammars Example of a simple grammar: S aA|bBC A aA|bb B bB|aC C cC|a w=bbacca Parsing: S=>bBC=>bbBC=>bbaC=>bbacC=>bbaccC=>bbacca
6
S-grammars We 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|.
7
Ambiguity
8
Some important notes Applications of CFG’s (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, that’s not always possible. Solution: Redesign the grammar!!
9
solution Options: 1. Redesign possibility 2. Redesign impossibility ~ Inherently ambiguous (beyond our scope) So, first, lets try to understand the problem now!
10
Ambiguity Example 1: S SA | A A aAb | ε w=aabb Give it lm and rm derivations and also draw parse trees for both lm and rm derivations.
11
Ambiguity lm: S=>SA=>AA=>aAbA=>aaAbbA=>aabbA=>aabb rm: S=>SA=>SaAb=>SaaAbb=>Saabb=>Aaabb=>aabb
12
Ambiguity Example 2: E I E E+E E E*E E (E) I a|b|Ia|Ib|I0|I1 w=E+E*E Give it lm and rm derivations and also draw parse trees for both lm and rm derivations.
13
Ambiguity rm: E=>E+E=>E+E*E lm: E=>E*E=>E+E*E
14
Ambiguity in Grammars There is a significant difference between the two derivations. What’s 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.
15
Ambiguity in Grammars Lets have a look at another very important factor, i-e “what causes ambiguity?” Multiplicity of derivations? //discussion
16
Ambiguous Grammars and Unambiguous Grammars A 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.
17
Unambiguous Grammars - Avoiding ambiguity Next class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.