Description of Inherently Ambiguous Context-Free Grammars - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Description of Inherently Ambiguous Context-Free Grammars

Description:

Description of Inherently Ambiguous Context-Free Grammars Ryan Villalpando April 7, 2003 CPSC 627 Introduction Review of Context-Free Grammars Definition of Ambiguity ... – PowerPoint PPT presentation

Number of Views:184
Avg rating:3.0/5.0
Slides: 24
Provided by: adammikea
Category:

less

Transcript and Presenter's Notes

Title: Description of Inherently Ambiguous Context-Free Grammars


1
Description of Inherently Ambiguous Context-Free
Grammars
  • Ryan Villalpando
  • April 7, 2003
  • CPSC 627

2
Introduction
  • Review of Context-Free Grammars
  • Definition of Ambiguity
  • Examples of Ambiguity
  • Parse Trees
  • Leftmost Derivations
  • Removing Ambiguity
  • Definition of Inherent Ambiguity
  • Proof of Inherent Ambiguity

3
Review of Context-Free Grammars
  • A context-free grammar is a 4-tuple (V, S, R, S)
  • V is the finite set of variables
  • S is the finite set of terminals
  • R is a set of rules
  • S ? V is the start variable

4
Example of a CFG
  • The language 1n0n n gt 0 gives the grammar
  • S-gtS1 S2
  • S1-gt0S11 e
  • S2-gt1S20 e

5
Ambiguity
  • A CFG is ambiguous if it can generate the same
    string in different ways
  • These CFGs will have several different parse
    trees and several meanings.
  • This result may cause issues with some
    programming applications

6
Example of Ambiguity in English
  • The girl touches the boy with the flower.
  • Meaning 1
  • The flower is the object being used to touch the
    boy.
  • Meaning 2
  • The boy is holding the flower.

7
Example of Ambiguity in Math
  • a a a
  • Meaning 1
  • (a a) a
  • Meaning 2
  • a (a a)

8
Parse Trees
  • Derivations
  • E gt E E gt E E E
  • E gt E E gt E E E

9
Leftmost Derivation
  • A derivation of a string is a leftmost derivation
    if at every step, the leftmost remaining variable
    is the one replaced.
  • If a grammar is unambiguous, it will have unique
    leftmost derivations.

10
The leftmost derivations of a a a
  • E
  • gt E E
  • gt a E
  • gt a E E
  • gt a a E
  • gt a a a
  • E
  • gt E E
  • gt E E E
  • gt a E E
  • gt a a E
  • gt a a a

11
Removing Ambiguity
  • There is no algorithm that can tell whether a CFG
    is ambiguous.
  • There are techniques for eliminating ambiguity
  • Adding non-terminals or dividing the variables
    into factors, terms, and expressions.

12
Removing Ambiguity from a b c
  • Grammar
  • I -gt abIaIbI0I1
  • F -gt I(E)
  • T -gt FT F
  • E -gt TE T
  • Terms (T) cannot be broken by the operator.
  • A factor (F) cannot be broken by a or a .
  • Is are identifiers
  • Es are expressions

13
Inherently Ambiguous Context-Free Grammar
  • A context-free language L is said to be
    inherently ambiguous if all its grammars are
    ambiguous.
  • If even one grammar for L is unambiguous, then L
    is an unambiguous language.

14
Example of an Inherently Ambiguous CFL
  • L anbncmdm ngt1, mgt1 U anbmcmdn ngt1,
    mgt1
  • Thus,
  • There are as many as as bs and as many cs as
    ds, or
  • There are as many as as ds and as many bs as
    cs.

15
A Grammar for L
  • S -gt AB C
  • A -gt aAb ab
  • B -gt cBd cd
  • C -gt aCd aDd
  • D -gt bDc bc

16
Leftmost Derivations for L with string aabbccdd
  • S gt AB gt aAbB gt aabbB gt aabbcBd gt aabbccdd
  • S gt C gt aCd gt aaDdd gt aabDcdd gt aabbccdd

17
Parse Trees for aabbccdd
18
Proving Inherent Ambiguity
  • The previous parse trees and leftmost derivations
    prove that language L has at least some ambiguous
    grammars.
  • This doesnt prove that all of Ls grammars are
    ambiguous, so we dont know if L is ambiguous.

19
Proof Idea
  • We argue that all but a finite number of the
    strings, whose counts of the four symbols a, b,
    c, d are all equal, must be generated in two
    different ways
  • as and bs are equal and cs and ds are equal
  • as and ds are equal and bs and cs are equal

20
Proof Idea Continued
  • The only way to generate strings where as and
    bs are equal is with a variable like A.
  • Variations can occur, but we must follow the
    mechanism for generating as in a way that
    matches the bs.
  • This idea carries for the variables B, C, and D.
  • This argument will show that no matter what
    modifications we make to the basic grammar, it
    will generate at least some of the strings of the
    form anbncndn.
  • Following this proof idea will show that L is
    inherently ambiguous, because no matter how the
    grammar is changed, it will remain ambiguous.

21
Summary
  • Ambiguity can cause problems in programming
    applications, yet sometimes ambiguity can be
    removed.
  • If all the grammars of a context-free language
    are ambiguous, then that language is ambiguous.
  • A CFL can be proved ambiguous by showing that all
    the grammars generated by the CFL fall into
    certain categories.

22
Citations
  • Hopcroft, John E. Introduction to automata
    theory, languages, and computation. Reading,
    Addison-Wesley, 1979.
  • Sipser, Michael Introduction to the Theory of
    Computation. Boston, PWS Publishing Company, 1997

23
The End
  • Questions and Answers
Write a Comment
User Comments (0)
About PowerShow.com