Definition 2.8: A CFG is in Chomsky normal form if every rule. only appear on the left-hand side, we allow the rule S ǫ.

Similar documents
CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

CSE 105 THEORY OF COMPUTATION

Theory of Computation Dr. Weiss Extra Practice Exam Solutions

JNTUWORLD. Code No: R

Pushdown Automata. A PDA is an FA together with a stack.

CT32 COMPUTER NETWORKS DEC 2015

Limits of Computation p.1/?? Limits of Computation p.2/??

Derivations of a CFG. MACM 300 Formal Languages and Automata. Context-free Grammars. Derivations and parse trees

Definition: A context-free grammar (CFG) is a 4- tuple. variables = nonterminals, terminals, rules = productions,,

Chapter 14: Pushdown Automata

T.E. (Computer Engineering) (Semester I) Examination, 2013 THEORY OF COMPUTATION (2008 Course)

ECS 120 Lesson 16 Turing Machines, Pt. 2

PDA s. and Formal Languages. Automata Theory CS 573. Outline of equivalence of PDA s and CFG s. (see Theorem 5.3)

I have read and understand all of the instructions below, and I will obey the Academic Honor Code.

Midterm Exam II CIS 341: Foundations of Computer Science II Spring 2006, day section Prof. Marvin K. Nakayama

UNIT I PART A PART B

Multiple Choice Questions


Skyup's Media. PART-B 2) Construct a Mealy machine which is equivalent to the Moore machine given in table.

CS402 - Theory of Automata Glossary By

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

QUESTION BANK. Unit 1. Introduction to Finite Automata

(a) R=01[((10)*+111)*+0]*1 (b) ((01+10)*00)*. [8+8] 4. (a) Find the left most and right most derivations for the word abba in the grammar

Models of Computation II: Grammars and Pushdown Automata

HKN CS 374 Midterm 1 Review. Tim Klem Noah Mathes Mahir Morshed

where is the transition function, Pushdown Automata is the set of accept states. is the start state, and is the set of states, is the input alphabet,

1 Parsing (25 pts, 5 each)

QUESTION BANK. Formal Languages and Automata Theory(10CS56)

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

Theory of Computations Spring 2016 Practice Final Exam Solutions

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

PS3 - Comments. Describe precisely the language accepted by this nondeterministic PDA.

Closure Properties of CFLs; Introducing TMs. CS154 Chris Pollett Apr 9, 2007.

lec3:nondeterministic finite state automata

Decidable Problems. We examine the problems for which there is an algorithm.

CS 44 Exam #2 February 14, 2001

Introduction to Computers & Programming

Final Course Review. Reading: Chapters 1-9

Turing Machines. A transducer is a finite state machine (FST) whose output is a string and not just accept or reject.

6 NFA and Regular Expressions

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

Theory of Computation

CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013

Computer Sciences Department

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

DHANALAKSHMI SRINIVASAN INSTITUTE OF RESEARCH AND TECHNOLOGY SIRUVACHUR, PERAMBALUR DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Automating Construction of Lexers

Actually talking about Turing machines this time

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

1. (10 points) Draw the state diagram of the DFA that recognizes the language over Σ = {0, 1}

Theory of Languages and Automata

Normal Forms and Parsing. CS154 Chris Pollett Mar 14, 2007.

Context Free Languages and Pushdown Automata

1. [5 points each] True or False. If the question is currently open, write O or Open.

Theory of Computations Spring 2016 Practice Final

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Variants of Turing Machines

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CS311 / MATH352 - AUTOMATA AND COMPLEXITY THEORY

Context-Free Grammars

CSE450. Translation of Programming Languages. Automata, Simple Language Design Principles

ONE-STACK AUTOMATA AS ACCEPTORS OF CONTEXT-FREE LANGUAGES *

CSCI 340: Computational Models. Turing Machines. Department of Computer Science

Regular Languages and Regular Expressions

pp Variants of Turing Machines (Sec. 3.2)

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Theory of Programming Languages COMP360

1. Which of the following regular expressions over {0, 1} denotes the set of all strings not containing 100 as a sub-string?

Enumerations and Turing Machines

Decision Properties for Context-free Languages

Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

Formal Definition of Computation. Formal Definition of Computation p.1/28

CSE 105 THEORY OF COMPUTATION

Assignment 4 CSE 517: Natural Language Processing

CISC 4090 Theory of Computation

Theory Bridge Exam Example Questions Version of June 6, 2008

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

Chapter Seven: Regular Expressions

CS/B.Tech/CSE/IT/EVEN/SEM-4/CS-402/ ItIauIafIaAblll~AladUnrtel1ity

Midterm I review. Reading: Chapters 1-4

14.1 Encoding for different models of computation

Context-Free Grammars

CS2 Language Processing note 3

We can create PDAs with multiple stacks. At each step we look at the current state, the current input symbol, and the top of each stack.

Lecture 12: Cleaning up CFGs and Chomsky Normal

UNIT 1 SNS COLLEGE OF ENGINEERING

Lecture Bottom-Up Parsing

CSE 105 THEORY OF COMPUTATION

A Characterization of the Chomsky Hierarchy by String Turing Machines

Languages and Automata

Lecture 2 Finite Automata

Answer All Questions. All Questions Carry Equal Marks. Time: 20 Min. Marks: 10.

Formal Grammars and Abstract Machines. Sahar Al Seesi

CS154 Midterm Examination. May 4, 2010, 2:15-3:30PM

Syntax Analysis Part I

CS 310: State Transition Diagrams

R10 SET a) Explain the Architecture of 8085 Microprocessor? b) Explain instruction set Architecture Design?

Compilation Lecture 3: Syntax Analysis: Top-Down parsing. Noam Rinetzky

Transcription:

CS533 Class 02b: 1 c P. Heeman, 2017 CNF Pushdown Automata Definition Equivalence Overview CS533 Class 02b: 2 c P. Heeman, 2017 Chomsky Normal Form Definition 2.8: A CFG is in Chomsky normal form if every rule is of the form A BC or A a. Let S be the start symbol. S can only appear on the left-hand side, we allow the rule S ǫ. - Note that our previous definition did not haveǫbut did allow the righthand side to have any number of terminals and variables (including zero of them). - CNF is a more restrictive grammar, which will make some proofs easier + Only have to worry about ǫ on start varible + Only have to worry about binary branching

CS533 Class 02b: 3 c P. Heeman, 2017 Equivalence of CFL and CFGs Theorem 2.9: Any CFL is generated by a CFG in Chomsky normal form What are the problems with the CFG? - Start state just on left-hand-side -ǫ productions on rules other than the start symbol - Unit rules: A B - Other rulesa ab,a BCDE CS533 Class 02b: 4 c P. Heeman, 2017 Proof Step 1: add a new start variable S o and the rule S o S - Start state is just on left-hand-side Step 2: ǫ productions (other than S o ǫ) - Remove rulesa ǫ, whereais nots o - Add extra versions of rules whereais on right-hand-side, without thea + Example: R uav (u a string of terminals and variables without A) + Example: R uavaw + Example: R A (R is not start symbol)

CS533 Class 02b: 5 c P. Heeman, 2017 Proof (continued) Step 3: Unit rules: - Remove each rulea B - For each ruleb u (whereuis a string of terminals and variables) adda u, unless this is a unit rule that was already removed + Example: A B B DE C AD B F Step 4: Other rules -A u 1 u 2...u k wherek 3 where eachu i is a variable or terminal + Replace with binary rules A u 1 A 1, A 1 u 2 A 2,..., A k2 u k 1 u k + where A i s are new variables -A u 1 u 2 whereu 1 oru 2 is a terminal + Replace terminal u i with a new variable, say U i, and add rule U i u i CS533 Class 02b: 6 c P. Heeman, 2017 Example 2.10 S ASA ab A B B b ǫ Step 1: add new start state: T

CS533 Class 02b: 7 c P. Heeman, 2017 Get rid of B ǫ Get rid of A ǫ Step 2: Remove epsilon rules CS533 Class 02b: 8 c P. Heeman, 2017 Step 3: Remove unit rules Remove unit rule S S - Anything thats goes to,s already goes to it, so just remove it Remove A B - IfB goes to something, add a rule withagoes to it Remove A S - IfS goes to something, add a rule withagoes to it. Remove T S - IfS goes to something, add a rule witht goes to it.

CS533 Class 02b: 9 c P. Heeman, 2017 Step 4: Rules with more than 2 on right side Replace T ASA Replace S ASA CS533 Class 02b: 10 c P. Heeman, 2017 Step 4: Rules with 2 on right side & terminals Remove T ab Remove S ab

CS533 Class 02b: 11 c P. Heeman, 2017 CNF Pushdown Automata Definition Equivalence Overview CS533 Class 02b: 12 c P. Heeman, 2017 Pushdown Automata Like a NFA, but has a State stack as well Control - Provides extra memory beyond finite amount in the control Function - Decides what to do based on: - Has an input head, which it can read the next character - Can pop off the top of the stack (and read it) - Transitions to another state x y z stack a a b b input - Can push onto the top of the stack

CS533 Class 02b: 13 c P. Heeman, 2017 Example: 0 n 1 n Pushdown automata can read the 0 s and push them onto stack Can pop off the 0 s from the stack, as it reads the 1 s CS533 Class 02b: 14 c P. Heeman, 2017 Non-determinism For Finite Automata - NFA and DFA s have same power: they recognize the same classes of languages Not so with Push-down automata - We will focus on non-deterministic push down automata, which are equivalent to CFG

CS533 Class 02b: 15 c P. Heeman, 2017 CNF Pushdown Automata Definition Equivalence Overview CS533 Class 02b: 16 c P. Heeman, 2017 Formal Definition A pushdown automaton is a 6-tuple (Q,Σ,Γ,δ,q 0,F) where Q, Σ, Γ, and F are all finite sets, and 1.Q is the set of states 2.Σ is the input alphabet 3.Γ is the stack alphabet 4.δ : Q Σ ǫ Γ ǫ P(Q Γ ǫ ) is the transition function 5.q 0 Q is the start state 6.F Q is the set of accept states

CS533 Class 02b: 17 c P. Heeman, 2017 δ : Q Σ ǫ Γ ǫ P(Q Γ ǫ ) Transition Function Transition function decides what to do based on (inputs) - What state it is in - Can look at the input (and consume it) or not (ǫ) - Can look at the stack (in which case it pops it) + Does this by specifying what to do on a certain stack value - Can ignore the stack (not pop anything off) + Specified by reading ǫ on the stack Transition function outcomes: - New state - Can push onto stack or not(ǫ) To prepare for next slide, draw this figure CS533 Class 02b: 18 c P. Heeman, 2017 Computation A pushdown automaton M = (Q,Σ,Γ,δ,q 0,F) accepts w if w can be written as w = w 1,w 2...w m, where each w i Σ ǫ and there is a sequence of states r 0,r 1,...,r m Q and a sequence of stack contents s 0,s 1,...,s m Γ such that 1.r 0 = q 0 (M starts in start state) 2.s 0 = ǫ (M starts with an empty stack) 3. Fori = 0,...,m-1, we have(r i+1,b) δ(r i,w i+1,a), wheres i = at and s i+1 = bt, for somea,b Γ ǫ andt Γ. 4.r m F - Stack formalization is a bit tricky +ais what gets popped off the stack with this transition +bis what gets pushed on the stack with this transition +tis part of the stack that won t change with the transition + Transition doesn t care what t is, as long as it doesn t change

CS533 Class 02b: 20 c P. Heeman, 2017 What is PDA for 0 n 1 n Example - Give state diagram (with just two states). Don t worry about how it ends. - Make sure that it ends properly Give formal definition CS533 Class 02b: 17 c P. Heeman, 2017 w r i,a b i r i+1 Go over special cases where some parts might be ǫ Talk about if stack has s on it beforehand, for w i,a b, s can be written as at after transition, stack is bt

CS533 Class 02b: 22 c P. Heeman, 2017 Give state diagram Example ww R where Σ = {0,1} CS533 Class 02b: 19 c P. Heeman, 2017 PDA How is the nondeterminism captured? Must stack be empty at accept? Can it pop and push in the same transition? Can it not look at the stack at all during a transition? Can it pop on an empty stack? Does it know when the stack is empty? Does it know when it is at the end of the input?

CS533 Class 02b: 24 c P. Heeman, 2017 Equivalence with CFG Theorem: A language is context free if and only if some pushdown automaton recognizes it. CS533 Class 02b: 21 c P. Heeman, 2017 Running out of Stack or Input Formal definition of PDA does not have a mechanism to allow a PDA to test for an empty stack - Can add $ to stack alphabet - Have an initial state push this on stack - Add extra states to test for this Formal definition of PDA does not allow it to check for end of input - Not needed as accept state only takes effect when machine is at end of input

CS533 Class 02b: 26 c P. Heeman, 2017 Give a derivation of aacbb with S asb c ǫ Trace out stack operations Example CS533 Class 02b: 23 c P. Heeman, 2017 Overview CNF Pushdown Automata Definition Equivalence

CS533 Class 02b: 28 c P. Heeman, 2017 Rules If top of the stack is a variable A, pop it, and non-deterministically choose a rule with A on left-hand side, and push the right-hand side onto the stack For each rule, A xyz where x,y,z are terminals or symbols - Have a sequence of states in which the first state is transitioned to if the top of the stack isa(and pops it) and pushesz onto the stack + Subsequence transitions to the subsequent states push y and then push x + Final transition returns to the main looping state CS533 Class 02b: 25 c P. Heeman, 2017 Context-Free Grammar PDA Lemma: If language is context free, some PDA recognizes it. Let A be a CFL, so there is a CFG G that generates it. Idea: Construct PDA P that simulates doing a derivation in G - Use stack to hold the current derivation, starting withs - If top of the stack is a terminal, pop it, and read that character from input - If top of the stack is a variablea, pop it, and non-deterministically choose a rule withaon left-hand side, and push the right-hand side onto the stack + Push in reverse order so that A u 1 u 2...u k has u k pushed first, so that u 1 is on top So we can tell when we are at end of derivation, start by pushing a $ onto stack, and just transition to accept state if $ is on top of stack

CS533 Class 02b: 27 c P. Heeman, 2017 So far Make a PDA that does the following for CFG G - Place $ on top of stack - Place start variable on top of stack - Repeat the following forever + If top of the stack is a variable A, pop it, and non-deterministically choose a rule with A on left-hand side, and push the right-hand side onto the stack + If top of the stack is a terminal, pop it, and read that character from input (if they don t match, then this branch of nondeterminism dies) + If the top of stack is a $, enter the accept state (doing so accepts the input if it is entirely read in at that point; otherwise this branch will die)