Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.

Slides:



Advertisements
Similar presentations
Context-Free and Noncontext-Free Languages
Advertisements

Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
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,
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture7: PushDown Automata (Part 1) Prof. Amos Israeli.
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
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.
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2009 with acknowledgement.
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.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
CS5371 Theory of Computation Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)
Normal forms for Context-Free Grammars
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Today Chapter 2: (Pushdown automata) Non-CF languages CFL pumping lemma Closure properties of CFL.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
1 Computer Language Theory Chapter 2: Context-Free Languages.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
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.
Pushdown Automata (PDAs)
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.
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.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Context-Free and Noncontext-Free Languages Chapter 13.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 1 Regular Languages Some slides are in courtesy.
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
C SC 473 Automata, Grammars & Languages Automata, Grammars and Languages Discourse 04 Context-Free Grammars and Pushdown Automata.
CSCI 2670 Introduction to Theory of Computing October 13, 2005.
Donghyun (David) Kim Department of Mathematics and Computer Science North Carolina Central University 1 Chapter 4 Decidability Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
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,
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Theory of Languages and Automata By: Mojtaba Khezrian.
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.
CS 154 Formal Languages and Computability March 22 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Closed book, closed notes
Context-Free Grammars: an overview
CSE 105 theory of computation
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
7. Properties of Context-Free Languages
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
CHAPTER 2 Context-Free Languages
Context-Free Grammars
فصل دوم Context-Free Languages
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:

Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy of Prof. Xinhua Zhuang at Dept. of CECS at University of Missouri-Columbia Spring 2016 CISG 5115 Theory of Computation

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University 2.1 Context-free Grammars 2

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University More Powerful Method of Describing Languages? Chapter 1 introduced two different, but equivalent, methods of describing languages: finite automata and regular expressions Many languages can be described in this way but some simple languages cannot This chapter introduces “context-free grammars”, which describe certain features that have a recursive structure Context-free languages (including regular languages) Pushdown automata (recognizes context-free languages) 3

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example One of the variables is designated as the start variable (i.e. A in the example) The grammar generates the string 000#111 Derivation method Parse tree method All language generated in this way constitute the language of the grammar Any language that can be generated by context-free grammar is called a context-free language 4 terminals variables Context-free grammar substitution rules (also called as productions)

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Context Free Grammar (CFG) Formal Definition V : finite set of variables (nonterminals) : finite set of terminals. : a special start variable, R : finite set of productions of form, where (rules) Conventions 5

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Language of a Grammar Formal Definition Define yield, Define such that “A finite # of application of productions" The language of a grammar G, L ( G ) All strings of terminals derivable from S L is a CFL iff CFG G such that L = L ( G ) Parse tree: a tree structured representation of a derivation. 6

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Designing CFG Union of simpler CFGs For strings of form X or Y, use where A derives X and B derives Y. Repeated symbols Equal number of symbols 7

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Designing CFG Example Find a grammar for the language How to Find a grammar for Merge the two grammars 8

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Ambiguity A grammar can generate the same string in many ways Depending on its derivation procedure, each string may have different meaning Undesirable for certain applications such as programming language ( + may calculated earlier than ) 9

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Ambiguity – cont’ A string is derived ambiguously in G if it has two or more leftmost derivations. G is ambiguous if it generates some strings ambiguously. A language L is inherently ambiguous if G such that L ( G ) = L, G is ambiguous. Ex (p. 2.29) : 10

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Ambiguity – cont’ Leftmost derivation: a way to remove ambiguity in CFG Replace the leftmost variable at every step Each parse tree has a unique leftmost derivation. CFG: How to derive ? 11

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Normal Forms Write grammars in simpler forms. Eliminate -productions unit productions useless symbols Examples Chomsky normal form (CNF) Greibach Normal Form (GNF) 12

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Chomsky Normal Form (CNF) Every production is of the form where Also Theorem: Any CFL is generated by a CFG in CNF. Proof: Convert any G into CNF. 13

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Convert a CFG G into CNF 1.Add a new start symbol 2.Eliminate -productions 3.Eliminate unit productions 4.Eliminate useless symbols 5.Convert rules into proper form 14

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 15 1.Add a new start symbol

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 16 2.Eliminate -productions

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 17 3.Eliminate unit productions

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 18 3.Eliminate unit productions

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example – cont’ 19 4.Eliminate useless symbols 5.Convert rules into proper form

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University 2.2 Pushdown Automata 20

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Pushdown Automata (PDA) PDA: NFA with an extra component called a stack Stack: unlimited storage, LIFO (last-in first-out) Pushing a symbol Popping a symbol The stack provides additional memory beyond the finite amount available in the control PDA can write symbols on the stack and read them back later Schematic of FA and PDA 21 aabb state control Input tape aabb state control Input tape xyz stack

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Pushdown Automata (PDA) – cont’ PDA may be nondeterministic Deterministic and nondeterministic PDA are not equivalent in power How PDA recognize ? (informally) Read symbols from the input. As each 0 is read, push it onto the stack. As soon as 1s are seen, pop a 0 off the stack for each 1 read. If reading the input is finished exactly when the stack becomes empty of 0s, accept the input. Otherwise, reject it. 22

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Pushdown Automata (PDA) Formal Definition 23

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Procedure of Converting CFG to PDA Push special mark $ and the start symbol S to the stack Repeat forever if the top of the stack is variable A : (apply a rule for A ) substitute A by the right hand side (RHS) of a rule. terminal a : (check applicability) compare the next input symbol with a If match, repeat. If not match, stop the branch. symbol $: enter the accept state. 24 state control A1A stack $ input Intermediate string: 01A1A0

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 1 Following is the formal description of the PDA that recognizes the language. Let be, where,,,, and is given by the following table, wherein blank entries signify. 25 Input01 stack0$0$0$

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 1 – cont’ Following is the state diagram of the PDA that recognizes the language. 26

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 1 – cont’ Following is the state diagram of the PDA that recognizes the language 27

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Example 2 28

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University PDA: Equivalence with CFG 29

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof 30

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ For simplicity, following modifications are enforced on P. 1.It has a single accept state,. 2.It empties its stack before accepting. 3.Each transition either pushes a symbol onto the stack or pops on off the stack, but it does not do both at the same time. Features 1 and 2 are easy to enforce. To give it Feature 3, we replace each transition that simultaneously pops and pushes with a two transition sequence that goes through a new state, each transition that neither pops nor pushes with a two transition sequence that pushes then pops an arbitrary stack symbol. 31

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ For any input string x, P ’s first move on x must be a push. The last move on x must be a pop, because the stack ends up empty. Two Possible Cases Either the symbol popped at the end is the symbol that was pushed at the beginning => stack became empty only at the end of P ’s computation on x : or not => stack became empty in the middle: 32

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ Two Possible Cases Either the symbol popped at the end is the symbol that was pushed at the beginning => stack became empty only at the end of P ’s computation on x : 33

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Brief Sketch of Proof – cont’ Two Possible Cases or not => stack became empty in the middle: 34

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof of Lemma 2.27 Let Variables of G are, and the start variable is Transition Rules G : For each,, and, if contains and contains, put the rule in G For each, put the rule in G. Finally, for each, put the rule in G. 35

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University 2.3 Non-context-free Languages 36

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Big Picture 37

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Non-Context-Free Languages 38

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof 39

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof – cont’ 40

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Proof – cont’ 41 For Condition 3, we need to be sure that vxy has length at most p. In the parse tree for s, the upper occurrence of R generates vxy. We choose R so that both occurrences fall within the bottom | V |+1 variables on the path (After R, no variable is repeated) and we chose the longest path in the parse tree, so the subtree where R generates vxy is at most | V |+1 high. A tree of this height can generate a string of length at most

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Using Pumping Lemma to Prove Languages not Context-Free Pumping lemma specifies a property that all CFL have. If a language L doesn't have this property, then L is not CFL. Using pumping lemma to prove non-context-free Assume L is a CFL and obtain a contradiction using conditions in pumping lemma: Choose a long string Show that s cannot be pumped. Examples not context free 42

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Examples of Using Pumping Lemma 43

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Examples of Using Pumping Lemma – cont’ 44

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Examples of Using Pumping Lemma – cont’ 45

Spring 2016 CISG 5115 Department of Mathematics and Physics Donghyun (David) Kim North Carolina Central University Closure Properties of CFLs CFLs are closed under union, concatenation, Kleene Closure (*), intersection with regular languages, … CFLs are not closed under intersection, complement, … 46