1 91.304 Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.

Slides:



Advertisements
Similar presentations
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
Foundations of (Theoretical) Computer Science Chapter 3 Lecture Notes (Section 3.2: Variants of Turing Machines) David Martin With.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
CFGs and PDAs Sipser 2 (pages ). Last time…
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 1 Lecture Notes (more on Section 1.4) David Martin This work is licensed under.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
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.
Foundations of (Theoretical) Computer Science Chapter 4 Lecture Notes (Section 4.1: Decidable Languages) David Martin With modifications.
Foundations of (Theoretical) Computer Science Chapter 1 Lecture Notes (Section 1.1: DFA’s) David Martin With some modifications.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
Normal forms for Context-Free Grammars
Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.3: Non-Context-Free Languages) David Martin With.
Specifying Languages CS 480/680 – Comparative Languages.
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Foundations of (Theoretical) Computer Science Chapter 1 Lecture Notes (Section 1.4: Nonregular Languages) David Martin With some.
Foundations of (Theoretical) Computer Science Chapter 1 Lecture Notes (Section 1.2: NFA’s) David Martin With some modifications.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
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.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
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.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Pushdown Automata (PDAs)
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Grammars CPSC 5135.
PART I: overview material
CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.
Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Lecture 11 Theory of AUTOMATA
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Foundations of (Theoretical) Computer Science Chapter 3 Lecture Notes (Section 3.1: Turing Machines) David Martin With some modifications.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
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.
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.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Context-Free Languages
Foundations of (Theoretical) Computer Science
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
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 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Spring
CSE 311 Foundations of Computing I Lecture 18 Recursive Definitions: Context-Free Grammars and Languages Autumn 2011 CSE 3111.
Theory of Languages and Automata By: Mojtaba Khezrian.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Context-Free Grammars: an overview
Complexity and Computability Theory I
CHAPTER 2 Context-Free Languages
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
Presentation transcript:

Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some modifications by Prof. Karen Daniels, Fall 2009 This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit sa/2.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

2 Chapter 2: CFGs, CFLs, PDAs  We introduce our next programming model with a grammatical formulation: more like REX than DFA  A context-free grammar (CFG) is a list of permitted substitution rules S ! S may be rewritten as  S ! 0 S 1...or as 0 S 1  The variables are the substitutable things, written in UPPER CASE, sometimes called nonterminals  The terminals are the nonsubstitutable characters  Each rule has a single variable on the left of ! and a list of terminals and variables on the right  The language generated by a CFG is the set of all (terminal) strings that can be found by starting from S and repeatedly substituting until no variables remain.

3 Language generated by example  So in S !  S ! 0 S 1 We can generate the strings , 01, 0011, , etc. In other words, the language {0 n 1 n | n ¸ 0} -- which is non-regular

4 Formal definition  G is a Context Free Grammar (CFG) if G = (V, , R, S) where 1. V is a finite set of variables 2.  is a finite set of terminals and V Å  = ;  It's an alphabet that can't overlap with V 3.R is a set of rules of the form  ! where   2 V — a single variable   2 ( [ V) * — a string of vars and terminals 4.S 2 V is the starting variable

5 Semantics of CFGs  If G=(V,,R,S) is a CFG, then ) G and ) G * are relations on the set (V [ ) * as follows If  2 (V [ ) *, then  ) G means that  can be rewritten as  by using some rule from R exactly once ) G * is the reflexive and transitive closure of ) G  In other words,  ) G * means that  can be rewritten as  by using zero or more rules Pronounce them "yields" or "derives"

6 Language derived by a CFG  Finally, the language derived by or generated by (or simply of) G is L(G) = { w 2  * | S ) G * w }  Note that w 2  * — so w may not contain any variables from V They are "nonterminal" — not done yet is the set of "terminals" — when you stop

7 Conventional form  You can either formally specify the 4 parts of a CFG, or use the conventional notation in this example: S ! 0B | 0A A ! BAB | AS | 1B2 B ! 0 | S |  nonterminalsmust be terminals start abbreviation for multiple rules Here  = { 0, 1, 2}

8 Facts about this grammar  S ! 0B | 0A A ! BAB | AS | 1B2 B ! 0 | S |  S ) 0B ) 0S ) 00A ) 00BAB ) 000AB ) 0001B2B ) B ) Thus 00A ) * 0001B2B and L(G) Also 1A ) 11B2 ) 1102 yet 1102 L(G) 2 Why?

9 Context-free languages  Definition L µ  * is a context-free language (CFL) if it is generated by some context free grammar (CFG).  Definition CFL() = { L µ  * | L is context free } is the class of all CFLs  Note that "context free" refers to the left hand side of the rules B ! 0 Ok — "B may always be replaced by 0" 2B ! 1ANot ok  such a rule is not allowed; this is a context-sensitive rule that says B may only be replaced when preceded by a 2 — context-free grammars can't say that

10 Context-free languages  CFLs (and variants of them) are often used in compilers and interpreters in order to make sense of programs and other formal specifications  Sample grammar: E ! E + E | E £ E | ( E ) | N N ! 0 | 1 | NN  Then E ) * 1 + (101 £ 1110)  See discussion of parsing and ambiguity in textbook (p ). Covered more in a compiler course.

11 Another example  Let ={a,b} and recall REX() — the set of all regular expressions over  S ! B | I (base or inductive cases) B ! ; |  | a | b I ! ( S ¢ S ) | ( S * ) | ( S [ S ) The nonvariables are literal characters and are underlined here, including the symbols ; and 

12 Yet another example  Let G be the grammar S !  | 0S1 | 1S0 | SS  and L 1 = { x 2 {0,1} * | n 0 (x) = n 1 (x) }  Clearly L(G) µ L 1. It's true but harder to see that L 1 µ L(G) as well...

13 Chomsky Normal Form  Definition 2.8: A context-free grammar is in Chomsky normal form if every rule is of the form: where a is any terminal and A, B, and C are any variables (except that B and C may not be the start variable). In addition, we permit the rule: where S is the start variable.

14 Chomsky Normal Form (continued)  Theorem 2.9: Any context-free language is generated by a context- free grammar in Chomsky normal form.  Proof Idea (see p for complete proof): Add a new start variable S 0 and rule: Process  rules:  Remove where A is not the start variable.  For every occurrence of A on right-hand side of a rule, add new rule with that occurrence deleted.  Repeat until all  rules not involving start variable are removed. Handle all unit rules:  Remove unit rule:  Where rule appears, add unless this was unit rule previously removed.  Repeat until all unit rules are eliminated. Convert remaining rules into the proper form:  Replace each rule where and each u i is a variable or a terminal symbol with rules: Example 2.10: Board work

15 Picture so far ALL FIN Each point is a language in this Venn diagram REG RPP B = { 0 n 1 n | n ¸ 0 } { 0101,  } 0 * (101)* F = { a i b j c k | i1 or j=k } CFL Does this exist?