Grammars and Context Free Languages

Similar documents
Grammars and Context Free Languages

Formal Languages, Grammars and Automata Lecture 5

Grammars and Context-free Languages; Chomsky Hierarchy

Foundations of Informatics: a Bridging Course

Lecture 11 Context-Free Languages

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

CSCI Compiler Construction

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Automata Theory CS F-08 Context-Free Grammars

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Properties of Context-Free Languages

Introduction to Theory of Computing

Computational Models - Lecture 3

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Computational Models - Lecture 4 1

Introduction and Motivation. Introduction and Motivation. Introduction to Computability. Introduction and Motivation. Theory. Lecture5: Context Free

CISC4090: Theory of Computation

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

(b) If G=({S}, {a}, {S SS}, S) find the language generated by G. [8+8] 2. Convert the following grammar to Greibach Normal Form G = ({A1, A2, A3},

Theory of Computation (IV) Yijia Chen Fudan University

Harvard CS 121 and CSCI E-207 Lecture 9: Regular Languages Wrap-Up, Context-Free Grammars

Computational Models - Lecture 4

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Non-context-Free Languages. CS215, Lecture 5 c

Formal Languages and Automata

Chapter 6. Properties of Regular Languages

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

Context-Free Grammars and Languages

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

CISC 4090 Theory of Computation

5 Context-Free Languages

Testing Emptiness of a CFL. Testing Finiteness of a CFL. Testing Membership in a CFL. CYK Algorithm

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

Chap. 7 Properties of Context-free Languages

INSTITUTE OF AERONAUTICAL ENGINEERING

This lecture covers Chapter 5 of HMU: Context-free Grammars

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

CS481F01 Prelim 2 Solutions

Even More on Dynamic Programming

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) October,

Part 3 out of 5. Automata & languages. A primer on the Theory of Computation. Last week, we learned about closure and equivalence of regular languages

Finite Automata Theory and Formal Languages TMV026/TMV027/DIT321 Responsible: Ana Bove

Properties of Context-free Languages. Reading: Chapter 7

Computational Models - Lecture 4 1

CS20a: summary (Oct 24, 2002)

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

Automata and Computability. Solutions to Exercises

Chapter 4: Context-Free Grammars

(pp ) PDAs and CFGs (Sec. 2.2)

Solutions to Problem Set 3

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 17 april Classrum Edition

Chapter 5: Context-Free Languages

Automata and Computability. Solutions to Exercises

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Einführung in die Computerlinguistik

Context-free Grammars and Languages

CS 373: Theory of Computation. Fall 2010

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Decidable and undecidable languages

CPSC 421: Tutorial #1

Theory of Computation

Recitation 4: Converting Grammars to Chomsky Normal Form, Simulation of Context Free Languages with Push-Down Automata, Semirings

C6.2 Push-Down Automata

Concordia University Department of Computer Science & Software Engineering

What we have done so far

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Fundamentele Informatica II

CA320 - Computability & Complexity

Theory of Computation (II) Yijia Chen Fudan University

Notes for Comp 497 (Comp 454) Week 10 4/5/05

CISC 4090 Theory of Computation

CYK Algorithm for Parsing General Context-Free Grammars

Theory of Computation - Module 3

ECS 120: Theory of Computation UC Davis Phillip Rogaway February 16, Midterm Exam

download instant at Assume that (w R ) R = w for all strings w Σ of length n or less.

Properties of Context-Free Languages. Closure Properties Decision Properties

CDM Parsing and Decidability

CPS 220 Theory of Computation

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen

Context Free Grammars

(pp ) PDAs and CFGs (Sec. 2.2)

CS Pushdown Automata

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CS Lecture 29 P, NP, and NP-Completeness. k ) for all k. Fall The class P. The class NP

Computability and Complexity

Einführung in die Computerlinguistik

HW6 Solutions. Micha l Dereziński. March 20, 2015

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 10 : Context-Free Grammars

Transcription:

Grammars and Context Free Languages H. Geuvers and J. Rot Institute for Computing and Information Sciences Version: fall 2016 H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 1 / 24

Outline Grammars H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 2 / 24

Organisational Next week is different: Test over part I (lectures 1-4) Make sure you are registered for the course in Blackboard! Time: 13:30 15:30, make sure you are there at 13:15. Locations: Names A G : HG00.307 Names H M : HG00.071 Names N Z : LIN 3 Extra time students: HG00.086 H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 3 / 24

Generating words Example of a grammar G: S O E E λ aea beb O a b aoa bob Productions, always start with S S E aea abeba abba S E beb baeab babebab babaeabab babaabab S O bob bab S O bob baoab babab We can generate exactly the set of words w with w = w R (w is a palindrome) H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 5 / 24

Context-free grammar Let Σ be a finite alphabet. Definition A context-free grammar (CFG) G = V, S, P over Σ consists of V, a set of non-terminal symbols, S V, a start symbol, P, a set of production rules of the form X w, where X V and w (V Σ). Notation: E λ aea beb is shorthand for three rules: E λ, E aea, E beb. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 6 / 24

Context-free language Using G, a language is generated using a relation ( produces ) defined as follows (where u, v, w are arbitrary elements of (Σ V ) ) X w implies uxv uwv u v, v w implies u w Definition The language generated by G, is L(G) = {w Σ S w}. A language L is context-free if L = L(G) for some context-free grammar G. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 7 / 24

Non-terminal symbols have a meaning When we write down a grammar, each non-terminal symbol has a meaning. This helps in explaining the grammar and in proving that it satisfies a property. Back to our first example grammar G: S O E E λ aea beb O a b aoa bob Words produced by E are palindromes of even length, Words produced by O are palindromes of odd length, Words produced by S are palindromes of even or odd length. So: L(G) {w {a, b} w = w R }. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 8 / 24

Leftmost derivations There can be many ways to produce a word w L(G). G: S asb λ, B b Derivations of aabb: d 1 : S asb aasbb aabb aabb aabb d 2 : S asb asb aasbb aasbb aabb d 3 : S asb asb aasbb aabb aabb Derivation d 1 is leftmost, d 2 is rightmost, d 3 is neither. Definition A derivation is leftmost if in each step a rule is applied to the leftmost non-terminal. Lemma For all grammars G and words w, w L(G) iff there is a leftmost derivation of w. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 9 / 24

Parse trees / Derivation trees G: S a S B λ, B b A parse tree (or derivation tree) for a a b b is: S a S B a S B b λ b H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 10 / 24

Ambiguity Definition A context-free grammar G is unambiguous if for each w L(G) there exists a unique leftmost derivation of w in G. Otherwise G is ambiguous. Equivalently: A context-free grammar is ambiguous if there is a word with two different parse trees. Example dangling else S print E if E then S else S if E then S E a b H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 11 / 24

Some context-free languages Is there a grammar G 2 such that G 2 : S λ asb L(G 2 ) = {a n b n n 0}? All possible derivations S asb aasbb a n Sb n λ ab aabb a n b n What is a grammar G 3 such that G 3 : S ab asb L(G 3 ) = {a n b n n > 0}? H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 12 / 24

More context-free languages Let Σ = {a, b, c} Claim: L = {a n b m c 2n+1 m, n 0} is context-free. Let us first show that L = {a n c 2n+1 m, n 0} is context-free. For L use S c ascc For L use S Bc ascc B λ bb Fact {a n b n c n n 0} is not context-free. This can be proved using the Pumping Lemma for Context Free Languages H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 13 / 24

Regular languages Theorem Every regular language is context-free. Proof: Let M = Q, q 0, δ, F be an NFA (or DFA, or NFA λ ) that accepts L Σ. Define a context-free grammar G M as follows: V = Q non-terminals are states S = q 0 P = {q aq q δ(q, a)} {q λ q F } L(G M ) = L(M), since there is a 1-1 correspondence between computations and derivations: a M : q 1 a 0 2 a q1 n qn F G M : q 0 a 1 q 1 a 1 a 2 q 2 a 1 a n q n a 1 a n λ H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 15 / 24

Regular languages: example start S a X a b b a Y Corresponding grammar: S ax by X ax ay Y bx λ Observe: The context free grammars we construct from an automaton are of a specific (simple) form. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 16 / 24

Definition A (right) regular grammar is a context-free grammar G = V, S, P in which all rules have the form where X, Y V and a Σ. X a Y or X λ Example Consider the regular language L := L(a(aa + b) (bb) ). A regular grammar for this language is found by first also allowing rules X w Y for w Σ, and then refining. S a X a Y X a a X b X a a Y b Y Y b b Y λ S a X a Y X a U b X b Y U a X a Y Y b V λ V b Y H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 17 / 24

Regular Languages and Theorem Let L Σ. L is regular L = L(G) for some regular grammar G. Proof: For = : From an NFA accepting L, we define a regular grammar accepting L. (As shown before.) For =: Given G, build an NFA M G = Q, δ, q 0, F as follows: State set Q = V, q 0 = S, F = {X V (X λ) P}. δ(x, a) = {Y V (X a Y ) P} L(M G ) = L(G), since derivations correspond to computations: G : S a 1 X 1 a 1 a 2 X 2 a 1 a n X n a 1 a n λ M G : S a 1 a X 2 a 1 n Xn F H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 18 / 24

Parsing A parser is an algorithm that determines for given G and w whether w L(G)? In practice we also want the parse tree. The well-known Cocke-Younger-Kasami (CYK) algorithm requires G to be in Chomsky normal form. For simplicity we assume λ / L(G). Definition A CFG G is in Chomsky normal form if all its productions are of the form: where X, Y, Z V and a Σ X YZ or X a Lemma Every CFG G (with λ / L(G)) can be transformed into an equivalent CFG in Chomsky normal form. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 20 / 24

Chomsky normal form and the CYK Algorithm Example of a grammar in Chomsky normal form: S AT AB T SB A a B b Accepts L := {a n b n n 1} Advantages of grammar in Chomsky normal form: A derivation of a word w is at most 2 w 1 steps long. A parse tree is a binary tree, so a parse tree of a word w has maximum depth w. Idea of CYK algorithm: For each substring u of input word w, compute the set of non-terminals that can produce u. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 21 / 24

Pumping Lemma for context-free languages Theorem Let L be a context-free language over Σ Then there exists a number k > 0 such that every word z L with z > k can be written as z = u 1 v 1 w v 2 u 2 such that v 1 w v 2 k v 1 + v 2 > 0 u 1 v i 1 w v i 2 u 2 L for all i 0 Proofsketch If there is a sufficiently large word z L then in the derivation of z one has S u 1 A u 2 A v 1 A v 2 a grammar loop! A w So: S u 1 v 1 w v 2 u 2 = z and also S u 1 A u 2 u 1 v 1 A v 2 u 2 u 1 v i 1 A v i 2 u 2 u 1 v i 1 w v i 2 u 2 L H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 23 / 24

Applications of the pumping lemma for context-free languages L 1 := {a k b k c k k 0} is not context-free because it violates the pumping lemma for CF languages. L 2 := {ww w {a, b} } is not context-free because it violates the pumping lemma for CF languages. NB: L 3 := {a k b k k 0} is context-free hence satisfies the pumping lemma for CF languages. H. Geuvers & J. Rot Version: fall 2016 Talen en Automaten 24 / 24