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

Similar documents
COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 9 : Myhill-Nerode Theorem and applications

Theory of Computation (II) Yijia Chen Fudan University

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 5 : DFA minimization

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lecture 1 : Introduction

Computational Models - Lecture 4

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 16 : Turing Machines

CISC4090: Theory of Computation

Computational Models - Lecture 3

CPS 220 Theory of Computation

Computational Models - Lecture 4 1

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lecture 2 : Regular Expressions & DFAs

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 14 : Turing Machines

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Computational Models - Lecture 4 1

The View Over The Horizon

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

Context-Free Grammars (and Languages) Lecture 7

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

Foundations of Informatics: a Bridging Course

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

CISC 4090 Theory of Computation

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

Introduction to Theory of Computing

DM17. Beregnelighed. Jacob Aae Mikkelsen

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

What we have done so far

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

Context Free Grammars

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

Functions on languages:

Final exam study sheet for CS3719 Turing machines and decidability.

Before We Start. The Pumping Lemma. Languages. Context Free Languages. Plan for today. Now our picture looks like. Any questions?

Computational Models - Lecture 5 1

Lecture 17: Language Recognition

Einführung in die Computerlinguistik

CISC 4090 Theory of Computation

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)

Properties of Context-Free Languages

Context Free Languages and Grammars

Context-Free and Noncontext-Free Languages

Closure Properties of Context-Free Languages. Foundations of Computer Science Theory

CFLs and Regular Languages. CFLs and Regular Languages. CFLs and Regular Languages. Will show that all Regular Languages are CFLs. Union.

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

DD2371 Automata Theory

Theory of Computation

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

Chapter 3. Regular grammars

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed

Context-Free Grammars and Languages. Reading: Chapter 5

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

This lecture covers Chapter 7 of HMU: Properties of CFLs

FLAC Context-Free Grammars

MA/CSSE 474 Theory of Computation

Section 1 (closed-book) Total points 30

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Theory of Computation (IV) Yijia Chen Fudan University

PUSHDOWN AUTOMATA (PDA)

Properties of Context-Free Languages. Closure Properties Decision Properties

Computational Models - Lecture 5 1

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

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

Properties of Context-free Languages. Reading: Chapter 7

Lecture III CONTEXT FREE LANGUAGES

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

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

NPDA, CFG equivalence

Theory Of Computation UNIT-II

Einführung in die Computerlinguistik

CS 341 Homework 16 Languages that Are and Are Not Context-Free

CS20a: summary (Oct 24, 2002)

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

MA/CSSE 474 Theory of Computation

2.1 Solution. E T F a. E E + T T + T F + T a + T a + F a + a

Computational Models: Class 5

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Computability and Complexity

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS

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

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

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Theory of Computation - Module 3

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Introduction to Formal Languages, Automata and Computability p.1/42

Automata and Computability. Solutions to Exercises

The Pumping Lemma for Context Free Grammars

Properties of context-free Languages

Automata and Computability. Solutions to Exercises

CPSC 421: Tutorial #1

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

CS 455/555: Finite automata

(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},

Automata Theory. CS F-10 Non-Context-Free Langauges Closure Properties of Context-Free Languages. David Galles

Context Free Language Properties

Properties of Context Free Languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

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

Computability and Complexity

Chap. 7 Properties of Context-free Languages

Ogden s Lemma for CFLs

Transcription:

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

COMP 330 Fall 2017: Lectures Schedule 1-2. Introduction 1.5. Some basic mathematics 2-3. Deterministic finite automata +Closure properties, 4. Nondeterministic finite automata 5. Minimization 6. Determinization+Kleene s theorem 7. Regular Expressions+GNFA 8. The pumping lemma 9. Myhill-Nerode theorem 10. Context-Free Grammar 11. PushDown automata 12.CFG - PDA equivalence 13. CFG Pumping lemma and applications 14. MIDTERM 15. 16. 17. 18. Introduction to computability 19. Models of computation Basic computability theory 20. Reducibility, undecidability and Rice s theorem 21. Undecidable problems about CFGs 22. Post Correspondence Problem 23. Validity of FOL is RE / Gödel s and Tarski s thms 24. Universality / The recursion theorem 25. Degrees of undecidability 26. Introduction to complexity

Context-Free Grammars Let s call the following grammar G 1 : A 0A1 A B B # Derivation of a string 000#111 : A 0A1 00A11 000A111 000B111 000#111.

Definition of CFG Variables A, B, C, TERM, EXPR Alphabet (of terminals) 0, 1, # Substitution Rules A 0A1 EXPR TERM Start Variable A (left-hand side of the first substitution rule)

Definition of CFG

Parse Tree

Definition of CFL If u, v and w are strings of variables and terminals, and A w is a rule of the grammar, we say that uav yields uwv, written uav uwv. We say that u derives v ( u * v ) if u=v or if u u 1 u 2... u k v, k 0. The language of G is { w * S * w }.

Context-Free Grammars Formally, grammar G 1 : V = {A,B} = {0,1,#} R = {A 0A1 B, B #} S = A L(G 1 ) = { 0 n #1 n n 0 }.

Example of CFG Rules of grammar G 2 :

Example of CFG ARTICLE a the means Rules of grammar G 2 : ARTICLE a ARTICLE the

Regular Operations : Kleene s theorem (CFG)

Regular Operations : Kleene s theorem (CFL) CFLs

Kleene s theorem (CFL) Let G A =(V A,,R A,S A ) be a CFG generating L A and G B =(V B,,R B,S B ) be a CFG generating L B (V A V B = ). Consider G U =( {S U } V A V B,,{S U S A S B } R A R B,S U ). L U = L A L B.

V = {A,B} = {0,1,#} R 1 = {A 0A1 B, B #} S = A R 2 : Let G 1 =({A,B},{0,1,#},R 1,A) be a CFG generating L 1 and G 2 =({ SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {a,b,c,,z," "}, R 2, SENTENCE ) be a CFG generating L 2. Let G U =( {S U,A,B, SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {0,1,#,a,b,c,,z," "}, {S U A SENTENCE } R 1 R 2,S U ). L U = L 1 L 2.

Regular Operations : Kleene s theorem (CFL) CFLs

Kleene s theorem (CFL) Let G A =(V A,,R A,S A ) be a CFG generating L A and G B =(V B,,R B,S B ) be a CFG generating L B (V A V B = ). Consider G C =( {S C } V A V B,,{S C S A S B } R A R B,S C ). L C = L A L B.

V = {A,B} = {0,1,#} R 1 = {A 0A1 B, B #} S = A R 2 : Let G 1 =({A,B},{0,1,#},R 1,A) be a CFG generating L 1 and G 2 =({ SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {a,b,c,,z," "}, R 2, SENTENCE ) be a CFG generating L 2. Let G C =( {S C,A,B, SENTENCE, NOUN-PHRASE, VERB-PHRASE, PREP-PHRASE, CMPLX-NOUN, CMPLX-VERB, ARTICLE, NOUN, VERB, PREP }, {0,1,#,a,b,c,,z," "}, { S C A SENTENCE } R 1 R 2,S C ). L C = L 1 L 2.

Regular Operations : Kleene s theorem (CFL) CFLs

Kleene s theorem (CFL) Let G A =(V A,,R A,S A ) be a CFG generating L A. Consider G S =( {S S } V A,,{S S ε S A S S } R A,S S ). L S = (L A )*.

V = {A,B} = {0,1,#} R 1 = {A 0A1 B, B #} S = A Let G 1 =({A,B},{0,1,#},R 1,A) be a CFG generating L 1. Let G S =( {S S,A,B}, {0,1,#}, { S S ε AS S, A 0A1 B, B #}, S S ). L S = (L 1 )*.

Construction tools (and Reductions) CFLs are closed under union, concatenation and star. If there exists a CFL C s. t. either A*=A, A C=A, A C=A (but not complement nor intersection) or any combinations of these operations then A is a CFL as long as A is. ( If A is NON-CFL then so is A. )

Construction tools Constructing a CFG for a regular language L: M = (Q={q 0,q 1,...,q k },,δ,q 0,F) is converted to G = (V={R 0,R 1,...,R k },,R,S=R 0 ) where R contains rule R i ar j for each δ(q i,a) = q j in M, and rule R i ε for each accept-state q i F. R 0 is the start variable.

extra EXAMPLE of CFG

extra EXAMPLE of CFG

Ambiguity in CFGs

Leftmost Derivation A derivation is Leftmost if every time a variable is substituted, it is always the leftmost variable. E X A M P L E

Ambiguity A string w is derived ambiguously by a CFG G if it has two or more distinct leftmost derivations. Grammar G is ambigious if it generates some string ambiguously.

Ambiguity Ambiguity is not desirable in CFG because it may lead to unexpected interpretations of a string, for instance in the context of arithmetic expressions or programming languages. However, some languages are inherently ambiguous, meaning that all grammars generating this language must be ambiguous. example : {a i b j c k i=j or j=k}

Ambiguous version of example 2.4 G 5

Ambiguous CFG

Noam Chomsky Chomsky Normal Form

Chomsky Normal Form

Chomsky Normal Form

157

Proof: First, we add a new start variable S 0 and the rule S 0 S, where S was the original start variable.

Chomsky Normal Form

Second, we take care of all ε-rules. We remove an ε-rule "A ε", where A is not the start variable. Then for each occurrence of A on the righthand side of a rule we add a new rule with that occurrence deleted. Accordingly, each rule "R A" is replaced by "R ε" unless it has been already removed.

Chomsky Normal Form

Third, we handle all unit rules by removing each unit rule A B. In consequence whenever B u appears, we add the rule A u unless this is a unit rule previously removed.

Chomsky Normal Form

Finally, we convert all remaining rules as follows: A u 1 u 2...u k for k>2, where each u i is a variable or terminal with a series of rules A u 1 A 1, A 1 u 2 A 2,..., A k-2 u k-1 u k where each A i is a new variable. When k=2, and A u 1 u 2, we may replace any terminal u i by a variable U i and the rule U i u i.

Chomsky Normal Form

Chomsky Normal Form

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