1 Chapter 12 - 1 Context-Free Languages. 2 Section 12.1 Context-Free Languages We know that the language {a n b n | n ∈ N} is not regular, but it certainly.

Slides:



Advertisements
Similar presentations
1 Lecture 32 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union,
Advertisements

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.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
1 Lecture 31 EQUAL language –Designing a CFG –Proving the CFG is correct.
1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition.
1 Lecture 29 Context Free Grammars –Examples of “real-life” grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language.
1 1. Show the result of each of the following set operations in terms of set property. Write your sets as simple as possible. (a) L 0  L 4 (b) L 0  L.
1 Module 30 EQUAL language –Designing a CFG –Proving the CFG is correct.
79 Regular Expression Regular expressions over an alphabet  are defined recursively as follows. (1) Ø, which denotes the empty set, is a regular expression.
1 Module 31 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union, concatenation.
Key to Homework #2 1. What is the language of L-system G = ({a, b, c}, h, acb ), where the rewriting rule h is defined as follows: h (a) = aa h (b) = cb.
Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Finite State Machines Data Structures and Algorithms for Information Processing 1.
Chapter 12: Context-Free Languages and Pushdown Automata
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.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
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.
CMSC 330: Organization of Programming Languages
1 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example.
1 Chapter Construction Techniques. 2 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the.
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]
1 Let L= { w= u v : u  {a, b}*, v  {c, d}* and |u|= |v|} 1.Design a context-free grammar that generates L. 2.Use your grammar and the construction from.
1 Chapter Regular Language Topics. 2 Section 11.4 Regular Language Topics Regular languages are also characterized by special grammars called regular.
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
Regular Grammars Chapter 7. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7.
Regular Grammars Chapter 7 1. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
Context Free Grammars.
1 Module 31 Closure Properties for CFL’s –Kleene Closure –Union –Concatenation CFL’s versus regular languages –regular languages subset of CFL.
CMSC 330: Organization of Programming Languages
Section 12.4 Context-Free Language Topics
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
Context-Free and Noncontext-Free Languages Chapter 13 1.
Enter Chomsky Grammars. 2 What has Chomsky* to do with computing? Linguistics and computing intersect at various places: Things that are used to create.
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.
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.
Section 11.4 Regular Language Topics
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
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
R. Johnsonbaugh Discrete Mathematics 5 th edition, 2001 Chapter 10 Automata, Grammars and Languages.
Context Free Grammars and Regular Grammars Needs for CFG Grammars and Production Rules Context Free Grammars (CFG) Regular Grammars (RG)
1 Section 11.1 Regular Languages Problem: Suppose the input strings to a program must be strings over the alphabet {a, b} that contain exactly one substring.
1 Chapter Regular Languages. 2 Section 11.1 Regular Languages Problem: Suppose the input strings to a program must be strings over the alphabet.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
1 Section 12.1 Context-Free Languages We know that the language {a n b n | n  N} is not regular, but it certainly has a non-regular grammar, such as S.
Chomsky Normal Form.
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.
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.
1 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations pop, push, and nop. PDAs always.
Recap lecture 31 Context Free Grammar, Terminals, non- terminals, productions, CFG, context Free language, examples.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Context Free Grammars & Parsing CPSC 388 Fall 2001 Ellen Walker Hiram College.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Context-Free and Noncontext-Free Languages Chapter 13.
Chapter 1 INTRODUCTION TO THE THEORY OF COMPUTATION.
Grammars.
Formal Language & Automata Theory
Chapter Thirteen: Stack Machines
Chapter 7 Regular Grammars
Presentation transcript:

1 Chapter Context-Free Languages

2 Section 12.1 Context-Free Languages We know that the language {a n b n | n ∈ N} is not regular, but it certainly has a non-regular grammar, such as S → aSb | ٨. A context-free grammar has productions of the form N → w Where N is a nonterminal and w is any string containing terminals and/or nonterminals. A context-free language is the set of strings derived from a context-free grammar.

3 Examples/Quizzes Example. {a n b n | n ∈ N} is a C-F language derived from the C-F grammar S → aSb | ٨. Example. Any regular grammar is context-free. So regular languages are C-F languages. Quiz. Find a grammar for {a n b n + 2 | n ∈ N}. Answer. S → aSb | bb. Quiz. Find a grammar for {ww R | w ∈ {a, b}*}, where w R is the reverse of w. Answer. S → aSa | bSb | ٨.

4 Techniques for Constructing Grammars Let L and M be two C-F grammars with disjoint sets of nonterminals and with start symbols A and B, respectively. Then –L ⋃ M has grammar S → A | B. –LM has grammar S → AB. –L* has grammar S → AS | ٨.

5 Example Let L be the language of strings over {a, b} with the same number of a’s and b’s. Does L have the following grammar? S → aSbS | bSaS | ٨. It’s easy to see that the language of the grammar is a subset of L. What about the other way? Assume that w ∈ L and show w is derived by the grammar. If w = ٨, then S ➾ ٨. Let w ≠ ٨ and assume that if s ∈ L and | s | < | w |, then S ➾ + s.

6 Example Cont’d Show that S ➾ + w. Consider the four cases: 1.w = asb for some string s. In this case, s ∈ L and | s | < | w |. So by induction we have S ➾ + s. Therefore, we have S ➾ aSbS ➾ aSb ➾ + asb = w. 2. w = bsa for some string s. Similar to case 1. 3.w = axa for some string x. In this case, x has two more b’s than a’s. So x ∉ L. What do we do now? Notice, for example, if | w | = 4, then w = abba. If | w | = 6, then w has one of the forms aabbba, ababba, abbaba, abbbaa. We claim that x can be written in the form x = ubbv where u, v ∈ L. (Can you prove it?) So by induction we have derivations S ➾ + u and S ➾ + v. Therefore, we have S ➾ aSbS ➾ + aubS ➾ aubbSaS ➾ + aubbvaS ➾ aubbva = axa = w. 4. w = bxb for some string x. Similar to case 3. QED.

7 Examples/Quizzes For a string x and letter a let n a (x) be the number of a’s in x. Let L = {x ∈ {a, b}* | n a (x) = n b (x)}. A grammar for L with start symbol E can be written as: E → aEbE | bEaE | ٨.

8 Quizzes Use this information to find grammars for the following languages. 1. {x ∈ {a, b}* | n a (x) = 1 + n b (x)}. Solution: S → EaE. 2. {x ∈ {a, b}* | n a (x) = 2 + n b (x)}. Solution: S → EaEaE. 3. {x ∈ {a, b}* | n a (x) > n b (x)}. Solution: S → EaET T → aET | ٨. 4. {x ∈ {a, b}* | n a (x) < n b (x)}. Solution: S → EbET T → bET | ٨.

9 Quizzes cont’d 5. {x ∈ {a, b}* | n a (x) ≠ n b (x)}. Solution: This language is the union of the languages in (3) and (4). Rename the nonterminals in the grammars for (3) and (4) as follows: (3)A → EaET T → aET | ٨. (4)B → EbEU U → bEU | ٨. Then S → A | B is the desired grammar.

10 The End of Chapter