4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.

Slides:



Advertisements
Similar presentations
Completeness and Expressiveness
Advertisements

Context free languages 1. Equivalence of context free grammars 2. Normal forms.
Chapter 5 Pushdown Automata
Erik Jonsson School of Engineering and Computer Science FEARLESS Engineering CS 4384 – 001 Automata Theory Thursday: Context-Free.
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Closure Properties of CFL's
3.2 Pumping Lemma for Regular Languages Given a language L, how do we know whether it is regular or not? If we can construct an FA to accept the language.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
Lecture 15UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 15.
CS5371 Theory of Computation
Courtesy Costas Busch - RPI1 More Applications of the Pumping Lemma.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
Costas Busch - RPI1 The Pumping Lemma for Context-Free Languages.
1 More Applications of the Pumping Lemma. 2 The Pumping Lemma: Given a infinite regular language there exists an integer for any string with length we.
CS5371 Theory of Computation Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)
Fall 2003Costas Busch1 More Applications of The Pumping Lemma.
Normal forms for Context-Free Grammars
Fall 2006Costas Busch - RPI1 More Applications of the Pumping Lemma.
Specifying Languages CS 480/680 – Comparative Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Prof. Busch - LSU1 Pumping Lemma for Context-free Languages.
Prof. Busch - LSU1 More Applications of the Pumping Lemma.
Homework #7 Solutions. #1. Use the pumping lemma for CFL’s to show L = {a i b j a i b j | i, j > 0} is not a CFL. Proof by contradiction using the Pumping.
1 Applications of Regular Closure. 2 The intersection of a context-free language and a regular language is a context-free language context free regular.
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Costas Busch1 More Applications of The Pumping Lemma.
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
1 Homework #7 (Models of Computation, Spring, 2001) Due: Section 1; April 16 (Monday) Section 2; April 17 (Tuesday) 2. Covert the following context-free.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
ELIMINATING LEFT RECURSIVENESS. Abbreviation. “cfg” stands for “context free grammar” Definition. A cfg is left recursive if it contains a production.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Costas Busch - LSU1 Pumping Lemma for Context-free Languages.
Chapter 5 Context-free Languages
Context-Free Languages
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
Copyright © Cengage Learning. All rights reserved. CHAPTER 8 RELATIONS.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
Theory of Languages and Automata By: Mojtaba Khezrian.
NPDAs Accept Context-Free Languages
Context-Free Languages
Context-Free Grammars and Languages
5. Context-Free Grammars and Languages
فصل دوم Context-Free Languages
Pumping Lemma for Context-free Languages
Properties of Context-Free Languages
Chapter 2 Context-Free Language - 01
More Applications of the Pumping Lemma
Theory of Computation Lecture #
Applications of Regular Closure
Presentation transcript:

4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate the languages. Some of the CFG’s are ambiguous, but some are not. We need to show that there are infinitely many strings of the form a n b n c n d n, n ≧ 1, that have two distinct leftmost derivations. In this section, we show that the language L = {a n b n c m d m | n ≧ 1, m ≧ 1} ∪ {a n b m c m d n | n ≧ 1, m ≧ 1} is inherently ambiguous, i.e., there is no unambiguous CFG to generate the language L.

Lemma 2 Let (N i, M i ), 1 ≦ i ≦ r, be pairs of sets of integers. (The sets may be finite or infinte.) Let S i = {(n, m) | n in N i, m in M i } and let S = S 1 ∪ S 2 ∪ … ∪ S r. If each pair of integers (n, m) is in S for all n and m, where n≠m, then (n, n) is in S for all but some finite set of n. Proof : By contradiction. Suppose that there are infinitely many n that (n, n) is not in S. Let T ={n| (n, n) is not in S}. It is obvious that T is infinite.

S r is a subset of S  There are infinitely many n that (n, n) is not in S r.  There are infinite many n that are not in N r, or not in M r.  T\N r is infinite or T \M r is infinite. Say T\N r is infinite. Let T r = T\N r. We have that T r is infinite and for all n and m in T r, (n, m) is not in S r. For all n in T r, (n, n) is not in S r-1.  For all n in T r, n is not in N r-1 or not in M r-1.  T r \N r is infinite or T r \M r is infinite. Say T r \N r is infinite. Let T r-1 = T r \N r. We have that T r-1 is infinite and for all n and m in T r-1, (n, m) is not in S r-1 ∪ S r.

By the same argument, we have that T 1 is infinite and for all n and m in T 1, (n, m) is not in S 1 ∪ S 2 ∪ … ∪ S r.  There exist n≠m in T 1, but (n, m) is not in S. Contradiction.

Lemma 3 Let G be an unambiguous CFG. Then we can construct an unambiguous CFG G’ equivalent to G, such that G’ has no useless symbols or productions, and for every variable A in G’, A≠S and S is the start symbol of G’, we have that A =>* x 1 Ax 2, where x 1 and x 2 are in T* and not both ε.

Theorem 8 The CFL, L = {a n b n c m d m | n ≧ 1, m ≧ 1} ∪ {a n b m c n d m | n ≧ 1, m ≧ 1}, is inherently ambiguous. Proof By contradiction. Assume that L can be generated by an unambiguous CFG. By Lemma 3, there is an unambiguous CFG G={V, T, P, S} generating L without useless symbols and productions; and for each A in V\{S}, A=>* x 1 Ax 2 for some x 1 and x 2 in T*, not both ε.

A=>* x1Ax2 for some x1 and x2 in T*, not both ε. We must have the following: x1 and x2 each consists of only one type of symbols a, b, c and d. |x1| = |x2| If A=>* x1Ax2 and A=>* x3Ax4, then x1 and x3 consist of the same type of symbol, same as x2 and x4. 1.x1 consists of solely of a’s, and x2 solely of b’s or of d’s, 2.x1 consists of solely of b’s, and x2 solely of c’s, or 3.x1 consists of solely of c’s, and x2 solely of d’s.

Variables other than S in V can be divided into four classes, C ab, C ad, C bc, and C cd. A in C ab, A=>x1Ax2, where x1 in a* and x2 in b*. For variables in C ad, C bc, and C cd are similar. A derivation containing a symbol in C ab or C cd can not contain a symbol in C ad or C bc or vice versa.

Divide G into two grammars, G1 = ({S} ∪ Cab ∪ Ccd, T, P1, S) and G2 = ({S} ∪ C ad ∪ C bc, T, P2, S), P1 contains all productions of P in which each production has only one variable in C ab or C cd. P2 contains all productions of P in which each production has only one variable in C ad or C bc. P1 contains all productions from P of the form S→a n b n c m d m, n≠m. P2 contains all productions from P of the form S→a n b m c m d n, n≠m. Productions of the form S→a n b n c n d n in P are not in either P1 or P2.

Consider G1, number the productions in P1 of the form S→α from 1 to r. If S →α is the ith production, 1 ≦ i ≦ r, let Ni be the set of all n such that S=> G1 α=>* G1 a n b n c m d m for some m, and let Mi be the set of all m such that S=> G1 α=>* G1 a n b n c m d m for some n, and For all n in Ni and m in Mi, We have that S=> G1 α=>* G1 a n b n c m d m. By lemma 2, we have that G1 must generate all but finite number of sentences in { a n b n c n d n | n ≧ 1}.

Apply the same agument, G2 must generate all but finite number of sentences in { a n b n c n d n | n ≧ 1}. Therefore, L(G1) ∩ L(G2) contain infinite number of sentences in { a n b n c n d n | n ≧ 1}. Those sentences are generated by two distinct derivations in G. Contradict to the assumption that G is unambiguous.