Context-free Languages Chapter 2. Ambiguity.

Slides:



Advertisements
Similar presentations
Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
Advertisements

Chapter 5 Pushdown Automata
Pushdown Automata Section 2.2 CSC 4170 Theory of Computation.
Pushdown Automata CPSC 388 Ellen Walker Hiram College.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Pushdown Automata Chapter 12. Recognizing Context-Free Languages We need a device similar to an FSM except that it needs more power. The insight: Precisely.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
CS 3240: Languages and Computation Properties of Context-Free Languages.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS21 Decidability and Tractability
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material has been reproduced and communicated to you by or on behalf of Monash University.
PDAs => CFGs Sipser 2.2 (pages ). Last time…
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
Theory of Computation What types of things are computable? How can we demonstrate what things are computable?
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2009 with acknowledgement.
CS5371 Theory of Computation Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
CS 3240: Languages and Computation Pushdown Automata & CF Grammars NOTE: THESE ARE ONLY PARTIAL SLIDES RELATED TO WEEKS 9 AND 10. PLEASE REFER TO THE TEXTBOOK.
Nathan Brunelle Department of Computer Science University of Virginia Theory of Computation CS3102 – Spring 2014 A tale.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Final Exam Review Cummulative Chapters 0, 1, 2, 3, 4, 5 and 7.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
1 Computer Language Theory Chapter 2: Context-Free Languages.
CSCI 2670 Introduction to Theory of Computing September 21, 2005.
Pushdown Automata.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
Chapter 7 PDA and CFLs.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
Context-free Languages
Cs3102: Theory of Computation Class 6: Pushdown Automata Spring 2010 University of Virginia David Evans TexPoint fonts used in EMF. Read the TexPoint manual.
Push-down Automata Section 3.3 Fri, Oct 21, 2005.
CSCI 2670 Introduction to Theory of Computing September 22, 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Cs3102: Theory of Computation Class 8: Non-Context-Free Languages Spring 2010 University of Virginia David Evans.
Chapter 7 Pushdown Automata
PZ03A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03A - Pushdown automata Programming Language Design.
Non-CF Languages The language L = { a n b n c n | n  0 } does not appear to be context-free. Informal: A PDA can compare #a’s with #b’s. But by the time.
PushDown Automata. What is a stack? A stack is a Last In First Out data structure where I only have access to the last element inserted in the stack.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
January 20, 2016CS21 Lecture 71 CS21 Decidability and Tractability Lecture 7 January 20, 2016.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
1.Draw a parse tree for the following derivation: S  C A C  C A b b  b b A b b  b b B b b  b b a A a a b b  b b a b a a b b 2. Show on your parse.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Lecture 6: Context-Free Languages
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
1 Chapter Pushdown Automata. 2 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations.
Bottom-up parsing Pumping Theorem for CFLs MA/CSSE 474 Theory of Computation.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Recap: Nondeterministic Finite Automaton (NFA) A deterministic finite automaton (NFA) is a 5-tuple (Q, , ,s,F) where: Q is a finite set of elements called.
CSE 105 theory of computation
Context-free Languages
Context-Free Grammars
فصل دوم Context-Free Languages
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

Context-free Languages Chapter 2

Ambiguity

Chomsky normal form Study Example 2.10 in the book

Pushdown Automata

PDA Example

What language is this?

{w | w has exactly one more b than a’s where Σ = {a, b}} 1 2 a, ɛ  a b, ɛ  b a, b  ɛ b, a  ɛ ɛ, b  ɛ

{w | w has exactly one more b than a’s where Σ = {a, b}} 1 2 a, ɛ  a b, ɛ  b a, b  ɛ b, a  ɛ ɛ, b  ɛ Dilemma 1: I am in State 1; The current input symbol is b and there is a b on the stack. What do you do?

{w | w has exactly one more b than a’s where Σ = {a, b}} 1 2 a, ɛ  a b, ɛ  b a, b  ɛ b, a  ɛ ɛ, b  ɛ Dilemma 2: What if my input string is bbb?

1 3 a, ɛ  a b, ɛ  b a, b  ɛ b, a  ɛ ɛ, b  ɛ Dilemma 2: What if my input string is bbb? 20 ɛ, $  ɛ ɛ, ɛ  $

{w | w has exactly one more b than a’s where Σ = {a, b}} Possible Answer 1: S  Sab | aSb | abS | Sba | bSa | baS | b How do you create bbaaabb?

{w | w has exactly one more b than a’s where Σ = {a, b}} Possible Answer 2: S  SaSbS | SbSaS | b What is the problem with this?

{w | w has exactly one more b than a’s where Σ = {a, b}} Possible Answer 3: S  XbX | XbX // one b surrounded by X /* X can be empty or it must contain an equal number of a’s and b’s */ X  XaXbX | XbXaX | ɛ /* We need ab and ba with X’s nested anywhere inbetween */

Pumping Lemma for CFL Can prove that a language A is NOT context free, i.e., no PDA can accept and no CFG can generate. Pumping Lemma says: s = uvxyz 1.For each i >= 0, s i = uv i xy i z  A 2.|vy| > 0 3.|vxy| <= p Assume A is regular Pick string s in A s  A s has to be big enough to reach a loop in the PDA |s| >= p p is the number of PDA transitions needed to visit the same state twice, i.e., take a loop

Consider this CFL A = {w | w has exactly one more b than a’s where Σ = {a, b}} s = a p b p+1 s is clearly in A Consider p = 2, s.t., s = aabbb, which we know is a reasonably small p. The adversary can choose: u = a, v = a, x = ɛ, y = b, z = bb 1.s i = uv i xy i z  A 2.|vy| > 0 3.|vxy| <= p

Adversary wins s = a p b p+1 p = 2, s.t., s = aabbb The adversary can choose: u = a, v = a, x = ɛ, y = b, z = bb 2. vy = ab, i.e, |vy| > 0 3. vxy = ab, i.e., |vxy| >= 2 1. s i = uv i xy i z  A for all i Every time we pump up v=a, we also pump up y=b. 1.s i = uv i xy i z  A 2.|vy| > 0 3.|vxy| <= p

Adversary wins s = a p b p+1 p = 2, s.t., s = aabbb The adversary can choose: u = a, v = a, x = ɛ, y = b, z = bb 1. s i = uv i xy i z  A for all i Every time we pump up v=a, we also pump up y=b. This proves nothing because perhaps we could have picked a better s where the adversary would not win. 1.s i = uv i xy i z  A 2.|vy| > 0 3.|vxy| <= p

Consider a language that might actually be Context Free B = {ww | w in {0,1}*} Consider s = 0 p 1 0 p 1

Non-Determinism in PDAs All non-deterministic Finite State Automata’s can be transformed into deterministic ones (see proof of Theorem 1.39 on p55) Many non-deterministic Pushdown Automata’s CANNOT be transformed into deterministic ones (see pp ) Non-determinism is necessary to recognize many Context-free Languages

Non-Determinism in PDAs Consider A = {ww R | w = {a, b}* } B = {wcw R | w = {a, b}* }

Non-Deterministic Grammars R  S | T S  aSb | ab T  aTbb | abb Consider the string aabb and aabbbb There is no way to know which rules were used unless we analyze the whole string. This indicated non-determinism.

DK-Test Essentially, a more algorithm for determining if a grammar is non-deterministic. We will not be covering this as it requires two weeks of explanation.

Is this non-deterministic S  E ˧ E  E + T | T T  T x a | a Consider the string a + a x a + a ˧ Consider the string a x a + a x a ˧

LR(1) Grammars S  E ˧ E  E + T | T T  T x a | a Consider the string a + a x a + a ˧ By scanning left to right (LR) and by looking ahead one symbol (1), we can determine which rule was used. (Called a forced handle).

LR(k) Grammars Grammars where you can determine which rules were used by scanning a string left to right and look ahead a constant (K) number of symbols LR(2) would require looking ahead two symbols. Almost all programming languages can be defined and generated using LR(k) Grammars.

Big Picture Deterministic PDAs require O(n) computation because for each symbol you deterministically move to another state and perhaps push or pop the stack. Implementing Non-deterministic PDAs requires a lot more computation. {ww R } requires O(n 2 ). Because you have to guess where to start popping at n possible locations. Implementing some non-deterministic PDAs require O(2 n ) or O(n!) LR(k) grammars yield non-deterministic PDAs that require n*k = O(n) computation. Cool!