Context-Free Grammars

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.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
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,
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
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.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
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
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
CS 490: Automata and Language Theory Daniel Firpo Spring 2003.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Final Exam Review Cummulative Chapters 0, 1, 2, 3, 4, 5 and 7.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 Computer Language Theory Chapter 2: Context-Free Languages.
CSCI 2670 Introduction to Theory of Computing September 21, 2005.
Theory of Languages and Automata
Pushdown Automata (PDAs)
Context-free Languages
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.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Non-Context-Free Languages Section 2.3 CSC 4170 Theory of Computation.
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,
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Regular Languages Chapter 1 Giorgi Japaridze Theory of 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,
 2004 SDU Lecture8 NON-Context-free languages.  2004 SDU 2 Are all languages context free? Ans: No. # of PDAs on  < # of languages on  Pumping lemma:
Theory of Languages and Automata By: Mojtaba Khezrian.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
Context-Free and Noncontext-Free Languages Chapter 13.
CSCI 2670 Introduction to Theory of Computing September 22, 2004.
Context-Free Grammars: an overview
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
CSE 105 theory of computation
Chapter 7 PUSHDOWN AUTOMATA.
Lecture 17 Oct 25, 2011 Section 2.1 (push-down automata)
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Summary.
Non-Context-Free Languages
Context-free Languages
CHAPTER 2 Context-Free Languages
Context-Free Languages
فصل دوم Context-Free Languages
Pushdown automata a_introduction.htm.
CS21 Decidability and Tractability
CS21 Decidability and Tractability
Context-Free Languages
Chapter 1 Regular Language - 02
Chapter 2 Context-Free Language - 01
CS21 Decidability and Tractability
Decidability and Tractability
Computer Language Theory
CS21 Decidability and Tractability
CSE 105 theory of computation
Chapter 1 Regular Language
Pushdown automata The Chinese University of Hong Kong Fall 2011
Context-Free Grammars
CSE 105 theory of computation
Presentation transcript:

Context-Free Grammars CSC 4170/CSC 8510 Theory of Computation Context-Free Grammars Chapter 2

A  B A   B  0A1 Terminals: 0,1 Variables: A,B Productions: What is a CFG 2.1.a A  B A   B  0A1 Terminals: 0,1 Variables: A,B Productions: Start variable: A Derivation: A  B  0A1  0B1  00A11  0011 A B Parse tree: 0 1 A B 0 1 A 

Our grammar simplified 2.1.b A  0A1 A   A  0A1  00A11  000A111  0000A1111  00001111 What language does this grammar produce?

N  men | women | children V  like | hate | respect A more complex CFG 2.1.c1 S  N’_V_N’ N’  N | N_who_V_N’ N  men | women | children V  like | hate | respect SN’_V_N’N’_like_N’N_like_N’N_like_Nwomen_like_Nwomen_like_children S N’ _ V _ N’ N like N women children

N  men | women | children V  like | hate | respect A more complex CFG 2.1.c2 S  N’_V_N’ N’  N | N_who_V_N’ N  men | women | children V  like | hate | respect S N’ _ V _ N’ N respect N _ who _ V _ N’ men women hate N children

A context-free grammar is a 4-tuple (V,,R,S), where Formal definitions 2.1.d A context-free grammar is a 4-tuple (V,,R,S), where 1. V is a finite set called the variables; 2.  is a finite set, disjoint from V, called the terminals; 3. R is a finite set of rules, with each rule being a pair of a variable and a string of variables and terminals; 4. S is an element of V called the start variable. If u,v, and w are strings of variables and terminals and A w is a rule, we say that uAv yields uwv, written uAv  uwv. x * y means that x=y, or x y, or there are z1,…,zn such that x z1 … zn y. The language produced (defined, described) by the grammar is {w | S * w and w is a string of (only) terminals}. A context-free language is a language produced by some CFG.

Ambiguity: An informal example the girl touches the boy with the flower Does this mean the girl touches (the boy with the flower) or the girl touches the boy with the flower (the girl touches the boy) with the flower ? with the flower the girl touches the boy

An example of an ambiguous CFG <EXPR>  <EXPR> + <EXPR> | <EXPR>  <EXPR> | a a + a  a <EXPR> <EXPR> <EXPR> + <EXPR> <EXPR>  <EXPR> a <EXPR>  <EXPR> <EXPR> + <EXPR> a a a a a A grammar is ambiguous iff it has two different parse trees for the same string

An equivalent but unambiguous grammar <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  a | a <EXPR> <EXPR> + <TERM> <TERM> <TERM>  a a a a + a  a

A more complex unambiguous grammar 2.1.h <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  <FACTOR> | <FACTOR> <FACTOR>  (<EXPR>) | a <EXPR> <TERM> <EXPR> <TERM>  <FACTOR> <EXPR> + <TERM> <FACTOR> a <TERM> <TERM>  <FACTOR> ( <EXPR> ) <FACTOR> <FACTOR> a <EXPR> + <TERM> <TERM> <FACTOR> a a <FACTOR> a a + a  a a (a + a)  a

Designing context-free grammars Design a CFG that produces all regular expressions over the alphabet {0,1}: <RE>  Design a CFG G that produces the union of the languages produced by two given CFGs G1 and G2. G1: A1  w1 … An  wn G2: B1  u1 … Bm  um

Converting a DFA into a CFG 2.1.j Variables: The states of the DFA 1 Q1 Q2 1 Start variable: The start state of the DFA Productions: 1. Qi  a Qj, whenever there is an a-arrow from Qi to Qj; 2. Qi  , whenever Qi is an accept state.

011001 Testing in work Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2   2.1.j Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2   1 Q1 Q2 1 Q1 0Q1 01Q2 011Q1 0110Q1 01100Q1 011001Q2 011001 011001

languages correspond pushdown automata. 2.2 Giorgi Japaridze Theory of Computability Just as to regular languages correspond finite automata, to context-free languages correspond pushdown automata. This is a new model of computation. What makes a PDA different from NFA is that it has potentially infinite, stack-type (LIFO) memory, where unlimited number of things can be remembered (pushed) and read (popped). A given transition depends on not only the current symbol in the input, but also the symbol currently at the top of the stack. PDAs are still not the most powerful machines. This is because of their limited (LIFO) access to memory.

Components of a pushdown automaton (PDA) Stack Input a a b a c … x y z . Push: write a symbol on the top of the stack Pop: delete a symbol from the top of the stack a,xy (Q,,,,s,F) q1 q2 If the input symbol is a and the top stack symbol is x, go from q1 to q2, pop x and push y Q is the set of states  is the input alphabet  is the stack alphabet  is the transition function s is the start state FQ is the set of accept states If a=, the read head is not advanced If x=, nothing is popped If y=, nothing is pushed

0 0 0 1 1 1 Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b1 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   0 0 0 1 1 1 Stack Input

0 0 0 1 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b2 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   0 0 0 1 1 1 $ Stack Input

0 0 0 1 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b3 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 0 0 1 1 1 Stack Input

0 0 0 1 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b4 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 0 0 1 1 1 Stack Input

0 0 0 1 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b5 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 0 0 1 1 1 Stack Input

0 0 0 1 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b6 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 0 0 1 1 1 Stack Input

0 0 0 1 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b7 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 0 0 1 1 1 Stack Input

0 0 0 1 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b8 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   0 0 0 1 1 1 $ Stack Input

0 0 0 1 1 1 Accept Stack Input How a PDA works , $ 0, 0 q1 q2 2.2.b9 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   Accept 0 0 0 1 1 1 Stack Input

What language does this automaton recognize? How a PDA works 2.2.b10 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   What language does this automaton recognize?

0 0 1 Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   1,0   2.2.b11 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   0 0 1 Stack Input

0 0 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b12 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   0 0 1 $ Stack Input

0 0 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b13 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 0 1 Stack Input

0 0 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b14 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 0 1 Stack Input

0 0 1 Reject $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b15 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   Reject $ 0 0 1 Stack Input

0 1 1 Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   1,0   2.2.b16 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   0 1 1 Stack Input

0 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b17 , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 1 1 Stack Input

0 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b18 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 1 1 Stack Input

0 1 1 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b19 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 1 1 Stack Input

0 1 1 Reject Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b20 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   Reject 0 1 1 Stack Input

0 1 0 Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   1,0   2.2.b21 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   0 1 0 Stack Input

0 1 0 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b22 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 1 0 Stack Input

0 1 0 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b23 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 1 0 Stack Input

0 1 0 $ Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b24 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   $ 0 1 0 Stack Input

0 1 0 Reject Stack Input How a PDA works , $ 0, 0 q1 q2 1,0   2.2.b25 Giorgi Japaridze Theory of Computability , $ 0, 0 q1 q2 1,0   1,0   q3 q4 ,$   Reject 0 1 0 Stack Input

Designing pushdown automata 2.2.c Giorgi Japaridze Theory of Computability Design a pushdown automaton that recognizes the language {w | w has an equal number of 0s and 1s} = s 1

Converting NFA into PDA Giorgi Japaridze Theory of Computability Every NFA can be understood as a PDA that never pushes or pops. Just replace every label a of the NFA by a, 1 1 b b,  a , a, a a, a b a, b, 3 2 3 2

Theorem 2.20: A language is context-free iff Main theorems 2.2.e Giorgi Japaridze Theory of Computability Theorem 2.20: A language is context-free iff some pushdown automaton recognizes it. Theorem: Not every nondeterministic PDA has an equivalent deterministic PDA. Example 2.18: There is a nondeterministic PDA recognizing {wwR | w{0,1}* } (wR means w reversed), but no deterministic PDA can recognize this language. Proofs omitted.

The pumping lemma for context-free languages Giorgi Japaridze Theory of Computability Theorem 2.34 (Pumping lemma for context-free languages) If L is a context-free language, then there is a number p (the pumping length) where, if s is any string in L of length at least p, then s may be divided into five pieces s = uvxyz satisfying the conditions: 1. For each i0, uvixyiz L; 2. |vy| > 0; 3. |vxy| p. uxz uvxyz uvvxyyz uvvvxyyyz uvvvvxyyyyz uvvvvvxyyyyyz

The pumping lemma in work: example 2.3.b Giorgi Japaridze Theory of Computability S  “R” is a regular expression R  0 | ( R )* “0” is a regular expression “(0)*” is a regular expression “((0)*)*” is a regular expression “(((0)*)*)*” is a regular expression … u = “( v = ( x = 0 y = )* z = )*” is a regular expression “((0)*)*” is a regular expression uv0xy0z: “(0)*” is a regular expression uv1xy1z: “((0)*)*” is a regular expression uv2xy2z: “(((0)*)*)*” is a regular expression uv3xy3z: “((((0)*)*)*)*” is a regular expression

Using the pumping lemma for proving that certain languages are not CF Giorgi Japaridze Theory of Computability Example 2.36: Show that the following language is not CF: B = {anbncn | n0} Proof by contradiction: Assume B is CF. Let then p be its pumping length. Select wB with |w|  p. By the pumping lemma, w=uvxyz and v and y can be pumped. If either v or y contain more than one type of symbols, then pumping would intermix these symbols in a wrong way. aaaabbbbcccc B aaaababbbbccccc B Thus, one of the three symbols should be neither in neither v, nor in y. aaaabbbbcccc B But then, after pumping, the number of that symbol will not change, while the number of the other symbols will increase. aaaaaabbbbbbbbcccc B

Regular vs context-free vs computer-recognizable languages 2.3.d Giorgi Japaridze Theory of Computability Computer-recognizable languages Context-free languages Regular languages {anbn | n0} {anbncn | n0}