CISC 4090 Theory of Computation

Similar documents
CISC 4090 Theory of Computation

CISC4090: Theory of Computation

Computational Models - Lecture 4

Computational Models - Lecture 4 1

Introduction to Theory of Computing

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

Theory of Computation (IV) Yijia Chen Fudan University

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

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

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

PUSHDOWN AUTOMATA (PDA)

The View Over The Horizon

SCHEME FOR INTERNAL ASSESSMENT TEST 3

Einführung in die Computerlinguistik

Computational Models - Lecture 4 1

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

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

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

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

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

Lecture 17: Language Recognition

Computational Models - Lecture 5 1

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

Section 1 (closed-book) Total points 30

Context-Free Grammars and Languages. Reading: Chapter 5

CSE 105 THEORY OF COMPUTATION

CISC 4090 Theory of Computation

CSE 355 Test 2, Fall 2016

Theory of Computation (II) Yijia Chen Fudan University

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

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

CSE 105 THEORY OF COMPUTATION

Please give details of your answer. A direct answer without explanation is not counted.

Foundations of Informatics: a Bridging Course

Computational Models - Lecture 3

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

CS500 Homework #2 Solutions

October 6, Equivalence of Pushdown Automata with Context-Free Gramm

Cliff s notes for equivalence of CFLs and L(PDAs) LisaCFL L = L(M) for some PDA M L=L(M)forsomePDAM L = L(G) for some CFG G

(pp ) PDAs and CFGs (Sec. 2.2)

CpSc 421 Final Exam December 15, 2006

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

The Pumping Lemma for Context Free Grammars

Theory of Computation - Module 3

5 Context-Free Languages

CS311 Computational Structures More about PDAs & Context-Free Languages. Lecture 9. Andrew P. Black Andrew Tolmach

MA/CSSE 474 Theory of Computation

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

Theory of Computation

CPS 220 Theory of Computation Pushdown Automata (PDA)

Context Free Grammars

Context-Free and Noncontext-Free Languages

Lecture III CONTEXT FREE LANGUAGES

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

CPS 220 Theory of Computation

Final exam study sheet for CS3719 Turing machines and decidability.

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Context-Free Languages (Pre Lecture)

Intro to Theory of Computation

Computational Models: Class 5

Grammars and Context-free Languages; Chomsky Hierarchy

Accept or reject. Stack

Automata and Computability. Solutions to Exercises

MA/CSSE 474 Theory of Computation

CSE 105 THEORY OF COMPUTATION

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

Automata and Computability. Solutions to Exercises

Pushdown Automata (2015/11/23)

Problem 2.6(d) [4 pts] Problem 2.12 [3pts] Original CFG:

Pumping Lemma for CFLs

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

Pushdown Automata. Reading: Chapter 6

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

Chapter 5: Context-Free Languages

Theory of Computation (VI) Yijia Chen Fudan University

Concordia University Department of Computer Science & Software Engineering

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

Pushdown Automata (Pre Lecture)

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

Theory of Computation Turing Machine and Pushdown Automata

NPDA, CFG equivalence

CMPT-825 Natural Language Processing. Why are parsing algorithms important?

Computational Models - Lecture 5 1

DM17. Beregnelighed. Jacob Aae Mikkelsen

Properties of Context-Free Languages

Solution Scoring: SD Reg exp.: a(a

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

Grade 6 Math Circles October 20/21, Formalism and Languages: Beyond Regular Languages

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

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

Grammars and Context Free Languages

Theory of Computation (Classroom Practice Booklet Solutions)

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

CSE 105 THEORY OF COMPUTATION

Sheet 1-8 Dr. Mostafa Aref Format By : Mostafa Sayed

FLAC Context-Free Grammars

Transcription:

CISC 4090 Theory of Computation Context-Free Languages and Push Down Automata Professor Daniel Leeds dleeds@fordham.edu JMH 332 Languages: Regular and Beyond Regular: Captured by Regular Operations a b c d e Recognized by Finite State Machines Context Free Grammars: Human language Parsing of computer language 2 An example Context-Free Grammar Grammar G1 A 0A1 A B B # Variables: A, B; Terminals: 0, 1, # One start variable: A Example strings generated: #, 0#1, 00#11, 000#111, L(G1) = {0 n #1 n n 0} Substitution rules/productions Variable -> Variables, Terminals 3 Example English Grammar Sentence -> NounPhrase VerbPhrase NounPhrase -> Article NounSub NounSub -> Noun Adjective NounSub VerbPhrase -> Verb Verb NounPhrase Noun -> Girl Boy Duck Ball Article -> The A Verb -> Throws Sings Example 1: S -> NP VP -> A NS V -> A N V -> The Boy Sings Example 2: S -> NP VP -> A NS V -> A N V -> A Duck Throws 4 1

Formal CFG Definition A CFG is a 4-tuple V, Σ, R, S V is finite set of variables Σ finite set of terminals R finite set of rules S V start variable Another example G3 = S, a, b, R, S R: S asb SS ε Example strings generated: ε, ab, abab, aabb, aaabbbab, ababababab, abaaabbb, Example rule expansion: S -> asb aasbb aaεbb aabb S -> SS asb asb aεb aasbb aεb aaεbb abaabb 5 L(G3) = {a s & b s; each a is followed by a matching b, every b matches exactly one corresponding preceding a} (like parenthesis matching) 7 Another example G4 = A, B, C, a, b, c, R, A R: A BC ε B Bb C C c ε Designing CFGs Creativity required If CFL is union of simpler CFL, design grammar for simpler ones (G1, G2, G3), then combine: S -> G1 G2 G3 If language is regular, can make CFG mimic DFA Example strings generated: ε, aaa, cbbc, aacc L(G4) = {Hard to describe } 9 10 2

Example: express as CFG 0 1 q 0 q 1 Q 0 -> 1Q 1 Q 1 -> 0Q 1 1Q 2 Q 2 -> ε 1 q 2 Designing CFGs Creativity required If language is regular, can make CFG mimic DFA Match each state with a single corresponding variable Q={q 0,,q n } V={R 0,, R n } Start state q 0 corresponds to state variable S -> R 0 Replace transition function with Production rule δ q i, a = q j R i ar j Accept state q k : transition to ε R k ε 12 13 Chomsky Normal Form CFG is in Chomsky normal form if every rule takes form: A BC B and C may not be the start variables The start variable may transition to ε Any CFL can be generated by CFG in Chomsky Normal Form 14 Converting to Chomsky Normal Form S 0 S where S was original start variable Remove A ε Shortcut all unit rules Given A B and B u, add A u Replace variable-terminal rules with variable-variable rules Given A Bc, add U C c and change A to A BU C Replace rules A u 1 u 2 u 3 u k with: A u 1 A 1, A 1 u 2 A 2, A 2 u 3 A 3,, A k 2 u k 1 u k 15 3

Non-normal form: S asa bx X Ycc ε 16 Non-normal form: S asa bx X Ycc ε Step 1: S 0 ->S, S 0 S S asa bx X Ycc ε Step 2: Remove ε, S 0 S S asa bx b X Ycc Step 3: Use unit rules, S 0 asa bx b S asa bx b X Ycc 17 Step 3: Use unit rules, S 0 asa bx b S asa bx b X Ycc Step 4: Replace terminals, S 0 ASA BX b S ASA BX b X YCC B b C c Step 5: Reduce multi-variable S 0 AN BX b S AN BX b X YM B b C c N SA M CC Ambiguity examples A grammar may generate a string in multiple ways Math example: Expr Expr + Expr Expr Expr Expr a English example: the girl touches the boy with the flower 18 19 4

Ambiguity definitions A grammar generates a string ambiguously if there are two or more different parse trees Definitions: Leftmost derivation: at each step the leftmost remaining variable is replaced w is derived ambiguously in CFG G if there exist more than one leftmost derivations Non-normal form: S aa bxc X Xc Y Y Ycc a 20 21 Non-normal form: S aa bxc X Xc Y Y Ycc a Step 1: Replace unit rules S aa bxc X Xc Ycc a Y Ycc a Step2: Replace terminals S AA BXC X XC YCC a Y YCC a B b C c 22 Step2: Replace terminals S AA BXC X XC YCC a Y YCC a B b C c Step 3: Reduce multi-var S AA BN X XC YM a Y YM a B b C c N XC M CC 23 5

Push down automata FSA augmented with memory Equivalent to CFG if use non-determinism Finite control: transition function Tape: holds input string Stack: Can write to/read from stack Input is Last In First Out ( LIFO ) PDA and Language 0 n 1 n Read symbol from input, push each 0 onto stack As soon as see 1 s, start popping 0 for each 1 seen If finish reading and stack empty, accept If stack is empty and 1 s remain, reject If inputs finished but stack still has 0 s, reject In 0 appears on input, reject 24 25 Definition of PDA A PDA is a 6-tuple Q, Σ, Γ, δ, q 0, F where Q, Σ, Γ, and F are finite sets Q is sets of states Σ is the input alphabet Γ is the stack alphabet δ: Q Σε Γε P Q Γε is transition function q 0 Q is start state F Q is set of accept states PDA computation M must start in q 0 with empty stack M must move according to transition function To accept string, M must be at accept state at end of input Start stack with $. If you see $ at top of stack, it is empty 26 27 6

Understanding transition δ PDA to accept 0 n 1 n a, b c means: when you read a from tape and b is on top of stack replace b with c on top of stack a, b, or c can be ε If a is ε then change stack without reading a symbol If b is ε then push new symbol c without popping b If c is ε then no new symbol pushed, only pop b M1 is Q, Σ, Γ, δ, q 0, F Q = q 1, q 2, q 3, q 4 Σ = 0,1 Γ = 0, $ F = q 1, q 4 0, ε 0 1,0 ε ε, ε $ 1,0 ε q 1 q 2 q 3 ε, $ ε q 4 28 29 PDA to accept 0 n 1 n 0, ε 0 1,0 ε ε, ε $ 1,0 ε ε, $ ε q 1 q 2 q 3 Input: 0011 q 4 PDA to accept {ww R } Power of non-determinism: At start, don t know where string w ends 0, ε 0 1, ε 1 0,0 ε 1,1 ε 0 $ $ 0 0 $ 0 $ $ ε, ε $ ε, ε ε q 1 q 2 q 3 ε, $ ε q 4 30 31 7

PDA to accept a i b j c k, i=j or j=k PDA to accept a i b j c k, i=j or j=k Power of non-determinism: At start, don t know if i=j or j=k 0, ε 0 1, ε 1 ε, ε $ a, ε a a, ε a b, a ε c, ε ε b, a ε ε, $ ε q 3 q 3 q 4 q 1 q 2 a, ε ε b, ε b c, b ε ε, ε ε q 5 b, ε b q 3 c, b ε q 3 ε, $ ε q 4 33 Theorem: A language is context free if and only if some PDA recognizes it Let s prove: If a language L is CFL, some PDA recognizes it Idea: Show how CFG can define a PDA Stack has set of terminals/variables to compare with input Place proper terminal/variable pattern onto stack based on rules Non-determinism: Clone your machine, following different branches of rules CFG -> PDA If top of stack is variable, sub one right-hand rule for the variable If top of stack is terminal, keep going iff terminal matches input If top of stack is $, accept! 34 35 8

Example 2.25 in textbook q 1 ε, ε $ S -> atb b T -> Ta ε ε, S b ε, ε T ε, ε a Regular languages vs. CFLs CFGs define CFLs PDAs recognize CFLs and Regular languages ε, ε S ε, T a ε, ε T FSAs recognize Regular languages, but not CFLs loop CFLs and Regular languages not equivalent ε, $ ε fin ε, S b ε, T ε a, a ε b, b ε 36 37 Non Context Free Languages Languages recognized by PDAs L={ww R } L={a n b n n 0} Languages not recognized by PDAs L={ww} L={a n b n c n n 0} CFL pumping: Proof idea Pigeonhole idea: Given a long enough string, some variable will need to be repeated Example Grammar: S -> urz R -> x vry T R R u v x y z 38 39 9

Prove F={ww w= 0 1 } not CFL Try a sample string s={0 p 10 p 1} s >p Can we define uvxyz=s so uv i xy i z F? Yes: u=0 p-1, v=0, x=1, y=0, z=0 p-1 1 Try another sample string s={0 p 1 p 0 p 1 p } Can we define uvxyz=s so uv i xy i z F? No: If vxy is in first w, pumping will make increase 1 s and/or 0 s in first w but not in second If vxy straddles the middle, vxy will either increase 1 s for first w and 0 s for second w, or will break the 0 n 1 n pattern 40 10