1 / 14

Removing Ambiguity

Removing Ambiguity. Lecture 16 Naveen Z Quazilbash. Overview. Attendance Review of Ambiguity Removing Ambiguity from GFGs Assignment result. Ambiguity-Review.

kenny
Télécharger la présentation

Removing Ambiguity

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Removing Ambiguity Lecture 16 Naveen Z Quazilbash

  2. Overview • Attendance • Review of Ambiguity • Removing Ambiguity from GFGs • Assignment result

  3. Ambiguity-Review • A CFG, G={V, T, P, S} is ambiguous if there is at least one string winT* 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.

  4. Ambiguity-Review • Example: EI EE+E EE*E E(E) Ia|b|Ia|Ib|I0|I1 w=E+E*E

  5. Removing Ambiguity from grammars • There is no algorithm whatsoever that can even tell us whether a CFG is ambiguous in the first place! • Moreover, there are context-free languages that have nothing but ambiguous CFGs; for these languages, removal of ambiguity is impossible. • Fortunately, the situation in practice is not so grim. • Well-known techniques for eliminating ambiguity for the sorts of constructs that appear in programming languages. • Typical problem of Expression grammar.

  6. Technique: • Identify the causes of ambiguity in the grammar • Implement a tailored solution!

  7. Causes of Ambiguity • There are two causes of ambiguity in the expression grammar: • The precedence of operators is not respected. • A sequence of identical operators can group either from the left or from the right. For example, if we *’s were replaced by +’s, we would see two different parse trees for the string E+E+E. since, addition and multiplication are associative, it doesn’t matter whether we group from the left or the right, but to eliminate ambiguity, we must pick one. Conventional approach is to insist grouping from the left.

  8. Solution to the problem • The solution to the problem of enforcing precedence is to introduce several different variables, each of which represents those expressions that share a level of “binding strength”. Specifically: • Factor: an expression that cannot be broken apart by any adjacent operator, either a * or a +. The only factors in our expression language are: • Identifiers • Any parenthesized expression • Term: an expression that cannot be broken by the + operator • Expression: an possible expression, including those that can be broken by either an adjacent * or an adjacent +.

  9. Unambiguous Expression Grammar Ia|b|Ia|Ib|I0|I1 FI|(E) TF|T*F ET|E+T • This shows an unambiguous grammar that generates the same language as the grammar of the given example. • Moreover, this grammar allows only one parse tree for the string a+a*a. Check!

  10. Checking Un-ambiguity!! • Key observations- why no string in the language can have two different parse trees: • A string derived from T, a term, must be a sequence of one or more factors, connected by *’s. a factor as we have defined it, and as follows from productions for F in solution grammar, is either a single identifier or any parenthesized expression.

  11. Because of the form of the two productions for T, the only parse tree for a sequence of factors is the one that breaks f1*f2*….fn-1 and a factor fn. The reason is that F cannot derive an expression like fn-1*fn without introducing parentheses around them. Thus, it is not possible that when using the production TT*F, the F derives anything but the last of the factors.

  12. Fig: the form of all parse trees for a term: T F T * . F T . * . . T F T * F

  13. Likewise, an expression is a sequence of terms connected by +. When we use the production EE+T to derive t1+t2+…+tn, the T must derive only tn, and the in the body derives t1+t2+…+tn-1. The reason, again, is that the sum of two or more terms without putting parentheses around them.

  14. Announcement • Extra double-class on Sunday!!!

More Related