Intro to Theory of Computation

Similar documents
PUSHDOWN AUTOMATA (PDA)

Intro to Theory of Computation

Pushdown Automata (Pre Lecture)

Intro to Theory of Computation

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

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

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,

Intro to Theory of Computation

CISC4090: Theory of Computation

Intro to Theory of Computation

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

CPS 220 Theory of Computation Pushdown Automata (PDA)

Homework. Context Free Languages. Announcements. Before We Start. Languages. Plan for today. Final Exam Dates have been announced.

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

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

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

(pp ) PDAs and CFGs (Sec. 2.2)

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

(pp ) PDAs and CFGs (Sec. 2.2)

5 Context-Free Languages

Context-free Languages and Pushdown Automata

Pushdown Automata (2015/11/23)

Push-down Automata = FA + Stack

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

SCHEME FOR INTERNAL ASSESSMENT TEST 3

Context-Free Languages

CS375: Logic and Theory of Computing

Accept or reject. Stack

CS Pushdown Automata

Computational Models - Lecture 4

Theory of Computation (IV) Yijia Chen Fudan University

Pushdown Automata: Introduction (2)

CFGs and PDAs are Equivalent. We provide algorithms to convert a CFG to a PDA and vice versa.

CISC 4090 Theory of Computation

Introduction to Theory of Computing

CISC 4090 Theory of Computation

Foundations of Informatics: a Bridging Course

Einführung in die Computerlinguistik

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

CPS 220 Theory of Computation

Pushdown Automata. Chapter 12

UNIT-VI PUSHDOWN AUTOMATA

Computational Models - Lecture 4 1

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

Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. The stack

CSE 105 THEORY OF COMPUTATION

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

Computational Models - Lecture 5 1

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

Pushdown Automata (PDA) The structure and the content of the lecture is based on

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Lecture 17: Language Recognition

MTH401A Theory of Computation. Lecture 17

MA/CSSE 474 Theory of Computation

Fundamentele Informatica II

Intro to Theory of Computation

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Theory of Computation

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

Computational Models: Class 5

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

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

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

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

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

Computability and Complexity

Equivalence of CFG s and PDA s

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

CSE 105 Theory of Computation

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

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

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

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

Theory of Computation Turing Machine and Pushdown Automata

Section 1 (closed-book) Total points 30

Computational Models - Lecture 4 1

Undecidable Problems and Reducibility

CSE 105 THEORY OF COMPUTATION

Automata Theory (2A) Young Won Lim 5/31/18

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

CSE 355 Test 2, Fall 2016

Context-Free and Noncontext-Free Languages

Chapter 3. Regular grammars

Computational Models Lecture 5

CFG and PDA accept the same languages

Computational Models - Lecture 3

Theory of Computation - Module 3

Decidable and undecidable languages

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Lecture Notes on Inductive Definitions

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

Pushdown Automata. Reading: Chapter 6

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

CMP 309: Automata Theory, Computability and Formal Languages. Adapted from the work of Andrej Bogdanov

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

Syntax Analysis Part I

Context-Free Grammars and Languages. We have seen that many languages cannot be regular. Thus we need to consider larger classes of langs.

Lecture Notes on Inductive Definitions

Transcription:

Intro to Theory of Computation LECTURE 7 Last time: Proving a language is not regular Pushdown automata (PDAs) Today: Context-free grammars (CFG) Equivalence of CFGs and PDAs Sofya Raskhodnikova 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.1

CONTEXT-FREE GRAMMARS (CFGs) start variable production rules A variables A 0A1 A B B # terminals 0A1 00A11 00B11 00#11 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.2

VALLEY GIRL GRAMMAR <PHRASE> <FILLER><PHRASE> <PHRASE> <START><END> <FILLER> LIKE <FILLER> UMM <START> YOU KNOW <START> ε <END> GAG ME WITH A SPOON <END> AS IF <END> WHATEVER <END> LOSER 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.3

VALLEY GIRL GRAMMAR <PHRASE> <FILLER><PHRASE> <START><END> <FILLER> LIKE UMM <START> YOU KNOW ε <END> GAG ME WITH A SPOON AS IF WHATEVER LOSER 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.4

Formal Definition A CFG is a 4-tuple G = (V, Σ, R, S) V is a finite set of variables Σ is a finite set of terminals (disjoint from V) R is set of production rules of the form A W, where A V and W (V Σ)* S V is the start variable L(G) is the set of strings generated by G A language is context-free if it is generated by some CFG. 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.5

Formal Definition A CFG is a 4-tuple G = (V, Σ, R, S) V is a finite set of variables Σ is a finite set of terminals (disjoint from V) R is set of production rules of the form A W, where A V and W (V Σ)* S V is the start variable Example: G = {{S}, {0,1}, R, S} R = { S 0S1, S ε } L(G) = { 0 n 1 n n 0 } 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.6

CFG terminology start variable production rules A variables A 0A1 A B B # terminals 0A1 00A11 00B11 00#11 uvw yields uvw if (V v) R. A derives 00#11 in 4 steps. 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.7

Example GIVE A CFG FOR EVEN-LENGTH PALINDROMES S S for all Σ S ε 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.8

Example GIVE A CFG FOR THE EMPTY SET G = { {S}, Σ,, S } 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.9

GIVE A CFG FOR L 3 = { strings of balanced parens } L 4 = { a i b j c k i, j, k 0 and (i = j or j = k) } 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.10

CFGs in the real world The syntactic grammar for the Java programming language BasicForStatement: for ( ; ; ) Statement for ( ; ; ForUpdate ) Statement for ( ; Expression ; ) Statement for ( ; Expression ; ForUpdate ) Statement for ( ForInit ; ; ) Statement for ( ForInit ; ; ForUpdate ) Statement for ( ForInit ; Expression ; ) Statement for ( ForInit ; Expression ; ForUpdate ) Statement 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.11

COMPILER MODULES LEXER PARSER SEMANTIC ANALYZER TRANSLATOR/INTERPRETER 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.12

Parse trees A A A B 0 0 # 1 1 A 0A1 00A11 00B11 00#11 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.13

PDAs: reminder string pop push ε,ε $ 0,ε 0 1,0 ε ε,$ ε 1,0 ε The language of P is the set of strings it accepts. PDAs are nondeterministic. 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.14

I-clicker problem (frequency: AC) When PDA takes a nondeterministic step, what happens to the stack? 1,ε 0 q 0 q 1 1,ε 1 A. First 0 is pushed onto the stack, then 1. B. First 1 is pushed onto the stack, then 0. C. Now PDA has access to two stack and it pushes 0 onto one and 1 onto the other. D. Two different computational branches are available to the PDA: it pushes exactly one symbol (0 or 1) onto the stack on each branch. E. None of the above q 2

Equivalence of CFGs & PDAs A language is generated by a CFG It is recognized by a PDA 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.16

Converting a CFG to a PDA Suppose L is generated by a CFG G = (V, Σ, R, S). Construct a PDA P = (Q, Σ, Γ,, q, F) that recognizes L. Idea: P will guess steps of a derivation of its input w and use its stack to derive it. 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.17

Algorithmic description of PDA (1) Place the marker symbol $ and the start variable on the stack. (2) Repeat forever: (a) If a variable V is on top of the stack, and (V s) R, Choose the rule from R pop V and nondeterministically. push string s on the stack in reverse order. (b) If a terminal is on top of the stack, pop it and match it with input. (3) On (ε,$), accept. 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.18

Designing states of PDA (q start ) Push S$ and go to q loop (q loop ) Repeat the following steps forever: (a) On (ε,v) where (V s) R, push s and go to q loop (b) On (, ), pop and go to q loop (c) On (ε,$) go to q accept Otherwise, the PDA will get stuck! 2/2/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.19

Designing PDA ε,ε S$ ε,$ ε ε,a w for each rule A w a,a ε for each terminal a 1/31/2016 Sofya Raskhodnikova; based on slides by Nick Hopper L7.20

S atb T Ta ε ε,ε $ ε,ε T ε,ε S ε,ε T ε,$ ε ε,ε a 1/31/2016 ε,t ε a,a ε b,b ε