January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.

Slides:



Advertisements
Similar presentations
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Advertisements

Theory of Computation CS3102 – Spring 2014 A tale of computers, math, problem solving, life, love and tragic death Nathan Brunelle Department of Computer.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
February 6, 2015CS21 Lecture 141 CS21 Decidability and Tractability Lecture 14 February 6, 2015.
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
PDAs => CFGs Sipser 2.2 (pages ). Last time…
CFGs and PDAs Sipser 2 (pages ). Last time…
PDAs => CFGs Sipser 2.2 (pages ). Last time…
Decidable languages Sipser 4.1 (pages ). CS 311 Mount Holyoke College 2 Hierarchy of languages All languages Turing-recognizable Turing-decidable.
CS5371 Theory of Computation
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Converting NPDAs to Context-Free Grammars. 2 For any NPDA we will construct a context-free grammar with.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2009 with acknowledgement.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 23, 2015CS21 Lecture 81 CS21 Decidability and Tractability Lecture 8 January 23, 2015.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
1 Pushdown Automata PDAs. 2 Pushdown Automaton -- PDA Input String Stack States.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
نظریه زبان ها و ماشین ها فصل دوم Context-Free Languages دانشگاه صنعتی شریف بهار 88.
CSCI 2670 Introduction to Theory of Computing September 20, 2005.
Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Pushdown Automata (PDAs)
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Context-free Languages
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
January 20, 2016CS21 Lecture 71 CS21 Decidability and Tractability Lecture 7 January 20, 2016.
Formal Languages, Automata and Models of Computation
Lecture 14UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 14.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
CS 154 Formal Languages and Computability March 15 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
Lecture 11  2004 SDU Lecture7 Pushdown Automaton.
Formal Languages, Automata and Models of Computation
Context-Free Grammars: an overview
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
فصل دوم Context-Free Languages
CS21 Decidability and Tractability
CS21 Decidability and Tractability
Chapter 2 Context-Free Language - 01
CS21 Decidability and Tractability
CS21 Decidability and Tractability
… NPDAs continued.
Presentation transcript:

January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014CS21 Lecture 62 Outline Context-Free Grammars and Languages –parse trees –ambiguity –normal form equivalence of NPDAs and CFGs non context-free languages

January 15, 2014CS21 Lecture 63 Context-Free Grammars A → 0A1 A → B B → # start symbol terminal symbols non-terminal symbols production

January 15, 2014CS21 Lecture 64 CFG example Arithmetic expressions over {+,*,(,),a} –(a + a) * a –a * a + a + a + a + a A CFG generating this language: → * → + → ( ) | a

January 15, 2014CS21 Lecture 65 CFG example A derivation of the string: a+a*a  *  + *  a + *  a + a *  a + a * a → * → + → ( ) | a

January 15, 2014CS21 Lecture 66 Parse Trees Easier way to picture derivation: parse tree grammar encodes grouping information; this is captured in the parse tree. * + aa a

January 15, 2014CS21 Lecture 67 CFGs and parse trees Is this a good grammar for arithmetic expressions? –can group wrong way (+ precedence over *) –can also group correct way (ambiguous) → * → + → ( ) | a

January 15, 2014CS21 Lecture 68 Solution to first problem forces correct precedence in parse tree grouping –within parentheses, * cannot occur as ancestor of + in the parse tree. → + | → * | → * → ( ) | a

January 15, 2014CS21 Lecture 69 Parse Trees parse tree for a + a * a in new grammar: → + | → * | → * → ( ) | a + * a a a

January 15, 2014CS21 Lecture 610 Ambiguity Second problem: ambiguous grammar Definitions: –a string is derived ambiguously if it has two different parse trees –a grammar is ambiguous if its language contains an ambiguously derived string ambiguity sometimes undesirable some CFLS are inherently ambiguous

January 15, 2014CS21 Lecture 611 Ambiguity Definition in terms of derivations (rather than parse trees): –order in which we replace terminals in shouldn’t matter (often several orders possible) –define leftmost derivation to be one in which the leftmost non-terminal is always the one replaced –a string is ambiguously derived if it has 2 leftmost derivations

January 15, 2014CS21 Lecture 612 Chomsky Normal Form Useful to deal only with CFGs in a simple normal form Most common: Chomsky Normal Form (CNF) Definition: every production has form A → BCor S → ε or A → a where A, B, C are any non-terminals (and B, C are not S) and a is any terminal.

January 15, 2014CS21 Lecture 613 Chomsky Normal Form Theorem: Every CFL is generated by a CFG in Chomsky Normal Form. Proof: Transform any CFG into an equivalent CFG in CNF. Four steps: –add a new start symbol –remove “ε-productions” A → ε –eliminate “unit productions”A → B –convert remaining rules into proper form

January 15, 2014CS21 Lecture 614 Chomsky Normal Form add a new start symbol –add production S 0 → S remove “ε-productions” A → ε –for each production with A on rhs, add production with A’s removed: e.g. for each rule R → uAv, add R → uv eliminate “unit productions”A → B –for each production with B on lhs: B → u, add rule A → u

January 15, 2014CS21 Lecture 615 Chomsky Normal Form convert remaining rules into proper form – replace production of form: A → u 1 U 2 u 3 …u k with: A → U 1 A 1 U 1 → u 1 A 1 → U 2 A 2 A 2 → U 3 A 3 U 3 → u 3 : A k-2 → U k-1 U k U k-1 → u k-1 U k → u k U 2 already a non-terminal

January 15, 2014CS21 Lecture 616 Some facts about CFLs CFLs are closed under –union(proof?) –concatenation(proof?) –star(proof?) Every regular language is a CFL –proof?

January 15, 2014CS21 Lecture 617 NPDA, CFG equivalence Theorem: a language L is recognized by a NPDA iff L is described by a CFG. Must prove two directions: (  ) L is recognized by a NPDA implies L is described by a CFG. (  ) L is described by a CFG implies L is recognized by a NPDA.

January 15, 2014CS21 Lecture 618 NPDA, CFG equivalence Proof of (  ): L is described by a CFG implies L is recognized by a NPDA. 0 #1 q1q1 0 : 1 A $ 0 #1 q2q2 0 : 1 # $ 0 #1 q3q3 0 : 1 # $ A → 0A1 A → # an idea:

January 15, 2014CS21 Lecture 619 NPDA, CFG equivalence 1.we’d like to non-deterministically guess the derivation, forming it on the stack 2.then scan the input, popping matching symbol off the stack at each step 3.accept if we get to the bottom of the stack at the end of the input. what is wrong with this approach?

January 15, 2014CS21 Lecture 620 NPDA, CFG equivalence –only have access to top of stack –combine steps 1 and 2: allow to match stack terminals with tape during the process of producing the derivation on the stack 0 #1 q1q1 0 1 A $ 0 #1 q2q2 1 A $ 0 #1 q3q3 # 1 $ A → 0A1 A → #

January 15, 2014CS21 Lecture 621 NPDA, CFG equivalence informal description of construction: –place $ and start symbol S on the stack –repeat: if the top of the stack is a non-terminal A, pick a production with A on the lhs and substitute the rhs for A on the stack if the top of the stack is a terminal b, read b from the tape, and pop b from the stack. if the top of the stack is $, enter the accept state.

January 15, 2014CS21 Lecture 622 NPDA, CFG equivalence ε, ε → S$ ε, A → w b, b → ε one transition for each production A → w one transition for each terminal b ε, A → w = w 1 w 2 …w k shorthand for: ε, A → w k qr qr q1q1 q2q2 qkqk … ε, ε → w k-1 ε, ε → w 1 ε, $ → 

January 15, 2014CS21 Lecture 623 NPDA, CFG equivalence Proof of (  ): L is recognized by a NPDA implies L is described by a CFG. –harder direction –first step: convert NPDA into “normal form”: single accept state empties stack before accepting each transition either pushes or pops a symbol

January 15, 2014CS21 Lecture 624 NPDA, CFG equivalence –main idea: non-terminal A p,q generates exactly the strings that take the NPDA from state p (w/ empty stack) to state q (w/ empty stack) –then A start, accept generates all of the strings in the language recognized by the NPDA.

January 15, 2014CS21 Lecture 625 NPDA, CFG equivalence Two possibilities to get from state p to q: stack height abcabbacacbacbacabacabbabbabaacabbbababaacaccaccccc input string taking NPDA from p to q pq r generated by A p,r generated by A r,q

January 15, 2014CS21 Lecture 626 NPDA, CFG equivalence NPDA P = (Q, Σ, , δ, start, {accept}) CFG G: –non-terminals V = {A p,q : p, q  Q} –start variable A start, accept –productions: for every p, r, q  Q, add the rule A p,q → A p,r A r,q

January 15, 2014CS21 Lecture 627 NPDA, CFG equivalence Two possibilities to get from state p to q: stack height abcabbacacbacbacabacabbabbabaacabbbababaacaccaccccc input string taking NPDA from p to q pq r generated by A r,s s push d pop d

January 15, 2014CS21 Lecture 628 NPDA, CFG equivalence NPDA P = (Q, Σ, , δ, start, {accept}) CFG G: –non-terminals V = {A p,q : p, q  Q} –start variable A start, accept –productions: for every p, r, s, q  Q, d  , and a, b  (Σ  {ε}) if (r, d)  δ(p, a, ε), and (q, ε)  δ(s, b, d), add the rule A p,q → aA r,s b from state p, read a, push d, move to state r from state s, read b, pop d, move to state q

January 15, 2014CS21 Lecture 629 NPDA, CFG equivalence NPDA P = (Q, Σ, , δ, start, {accept}) CFG G: –non-terminals V = {A p,q : p, q  Q} –start variable A start, accept –productions: for every p  Q, add the rule A p,p → ε