CA320 - Computability & Complexity

Similar documents
5 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.

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

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

Theory of Computation - Module 3

Theory of Computation

Fundamentele Informatica II

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

Einführung in die Computerlinguistik

CISC4090: Theory of Computation

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

6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs

Fundamentele Informatica 3 Antwoorden op geselecteerde opgaven uit Hoofdstuk 7 en Hoofdstuk 8

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

HW6 Solutions. Micha l Dereziński. March 20, 2015

Introduction to Theory of Computing

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

Pushdown Automata. Reading: Chapter 6

CPS 220 Theory of Computation

Theory Bridge Exam Example Questions

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

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009

CPS 220 Theory of Computation Pushdown Automata (PDA)

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

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,

Lecture 11 Context-Free Languages

CA320 - Computability & Complexity

Pushdown Automata (Pre Lecture)

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Theory of Computation (IV) Yijia Chen Fudan University

Solutions to Problem Set 3

Foundations of Informatics: a Bridging Course

Pushdown Automata: Introduction (2)

CISC 4090 Theory of Computation

Context-Free and Noncontext-Free Languages

Grammars and Context Free Languages

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

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Pushdown Automata (2015/11/23)

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

ÖVNINGSUPPGIFTER I SAMMANHANGSFRIA SPRÅK. 17 april Classrum Edition

Computational Models - Lecture 4

Concordia University Department of Computer Science & Software Engineering

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

Automata Theory CS F-13 Unrestricted Grammars

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

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

Grammars and Context-free Languages; Chomsky Hierarchy

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

CISC 4090 Theory of Computation

Grammars and Context Free Languages

NPDA, CFG equivalence

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

MA/CSSE 474 Theory of Computation

SFWR ENG 2FA3. Solution to the Assignment #4

Section 1 (closed-book) Total points 30

SCHEME FOR INTERNAL ASSESSMENT TEST 3

Properties of Context-free Languages. Reading: Chapter 7

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

CSE 468, Fall 2006 Homework solutions 1

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

INSTITUTE OF AERONAUTICAL ENGINEERING

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

Problem Session 5 (CFGs) Talk about the building blocks of CFGs: S 0S 1S ε - everything. S 0S0 1S1 A - waw R. S 0S0 0S1 1S0 1S1 A - xay, where x = y.

Theory of Computation

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

PUSHDOWN AUTOMATA (PDA)

Chapter 5: Context-Free Languages

TAFL 1 (ECS-403) Unit- IV. 4.1 Push Down Automata. 4.2 The Formal Definition of Pushdown Automata. EXAMPLES for PDA. 4.3 The languages of PDA

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

Miscellaneous. Closure Properties Decision Properties

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

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

UNIT-VI PUSHDOWN AUTOMATA

CPSC 421: Tutorial #1

An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State Machine (FSM).

Indexed Languages and why you care. Presented by Pieter Hooimeijer

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

Context-Free Grammar

FLAC Context-Free Grammars

Automata and Computability. Solutions to Exercises

CS375: Logic and Theory of Computing

MA/CSSE 474 Theory of Computation

Push-down Automata = FA + Stack

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Automata and Computability. Solutions to Exercises

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

Section: Pushdown Automata

Homework 4. Chapter 7. CS A Term 2009: Foundations of Computer Science. By Li Feng, Shweta Srivastava, and Carolina Ruiz

Solution Scoring: SD Reg exp.: a(a

DM17. Beregnelighed. Jacob Aae Mikkelsen

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

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

Automata Theory CS F-08 Context-Free Grammars

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

Context-Free Languages

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

Transcription:

CA320 - Computability & Complexity Dav Sinclair Context-Free Grammars A context-free grammar (CFG) is a 4-tuple G = (N,Σ,S,P) where N is a set of nonterminal symbols, or variables, Σ is a set of terminal symbols, and N and Σ are disjoint finite sets, S is a special nonterminal (S N) called the start symbol, P is a finite set of grammar rules, or productions, of the form A α, where A V and α (N Σ). The set V = N Σ is called the vocabulary of G.

Context-Free Grammars (2) A derivation, α β, is the application of one or more production rules starting with the string α and resulting in the string β. Conser the following grammar. S SS S (S) S ǫ An example derivation is: S SS S(S) S((S)) S(()) (S)(()) ()(()) If A γ then αaβ αγβ is a single step derivation using A γ. The grammar is a context-free grammar since the production rule, A γ, does not depend on the context surrounding nonterminal, A. Context-Free Grammars (3) Derivations requiring 0 and 1 steps are denoted by and + respectively. The start symbol denotes the entire set of strings that can be generated by G, L(G). L(G) = {x Σ S + x} xample: AnBn S asb S ǫ This grammar yields derivations such as S asb aasbb aabb. L(G) = {a k b k k 0}

Context-Free Grammars (4) xample: xpr S num ( ) + / This grammar generates simple expressions over number and entifiers. Derivations A derivation is a sequence of steps where in each step a non-terminal is replaced by the left-hand se of a productions rule that starts with the non-terminal. If the leftmost non-terminal is always chosen to be replaced then it is a leftmost derivation. If the rightmost non-terminal is always chosen to be replaced then it is a rightmost derivation. A derivation is also called a parse. The process of discovering a derivation is called parsing.

Conser the CFG grammar xpr. Parse Tree S + + + num + num + num This can be denoted as S + num. Because we always choose the leftmost non-terminal this is a leftmost derivation. Parse Tree (2) This derivation can also be represented as a parse tree. S + num But what if we choose a rightmost derivation of the same expression. S + num

Ambiguity What is the implication of the fact that there at least 2 parse trees for the same expression? S S + num + num A CFG G is ambiguous if x L(G) such that x has more than one derivation tree. This is equivalent to saying it has more than one distinct leftmost or rightmost derivations tree. Ambiguity (2) Sometimes redesigning the grammar can remove the ambiguity. The following grammar, xpr1, does not have the ambiguity of xpr. xpr1 S + T T T T T F T T/F T F F F num F () The only parse tree for + num is: S + T T F T F num F

Pushdown Automaton We know that the language AnBn is not a regular language and cannot be recognised by a Finite State Automaton. The following language SimplPal = {wcw r w Σ }, where w r is the reverse of w, is also not a regular language. Both of these languages require a machine that can remember something. In the case of SimpPal it must remember w. Then after seeing c it then checks for w r. We can extend a Nondeterministic Finite State Automaton by adding a stack memory. This is called a Pushdown Automaton. Pushdown Automaton (2) A Pushdown Automaton (PDA) is a 7-tuple M = (Q,Σ,Γ,q 0,Z 0,A,δ) where Q is a finite set of states; Σ and Γ are finite input and stack alphabets; q 0 Q is the initial state; Z 0 Γ is the initial stack symbol; A Q is the set of accepting states; δ : Q (Σ {ǫ}) Γ 2 (Q Γ ) is the transition function. The PDA is nondeterministic because the transition function δ can map the same element of the range to different elements of the range. A configuration of the PDA M = (Q,Σ,Γ,q 0,Z 0,A,δ) is a triple (q,x,α) where q Q,x Σ and α Γ.

Pushdown Automaton (3) (p,x,α) M (q,y,β) represents the PDA M moving from configuration (p,x,α) to configuration (q,y,β). This can occur in two ways: an input symbol is read; or a Λ-transition occurs. i.e. x = σy,σ Σ {ǫ}. If α = Xγ,X Γ,y Γ then β = ξγ where (q,ξ) δ(p,σ,x). (p,x,α) n M (q,y,β) denotes moving from configuration (p,x,α) to configuration (q,y,β) in n steps. (p,x,α) M (q,y,β) denotes moving from configuration (p,x,α) to configuration (q,y,β) in zero or more steps. Pushdown Automaton (4) If M = (Q,Σ,Γ,q 0,Z 0,A,δ) and x Σ, then x is accepted by M if α Γ,q A such that (q 0,x,Z 0 ) M (q,ǫ,α) A language L Σ is sa to be accepted by M if L is precisely the set of strings accepted by M. Theorem A language is context-free iff it can be recognized by a pushdown automaton.

xample PDAs AnBn Q = {q 0,q 1,q 2,q 3 } A = {q 3 } The transition table for AnBn is: Pushdown Automaton (5) State Input Top of Stack Move(s) q 0 ǫ Z 0 (q 3,Z 0 ) q 0 a Z 0 (q 1,aZ 0 ) q 1 a a (q 1,aa) q 1 b a (q 2,ǫ) q 2 b a (q 2,ǫ) q 2 ǫ Z 0 (q 3,Z 0 ) The sequence of moves that accepts aabb is: (q 0,aabb,Z 0 ) (q 1,abb,aZ 0 ) (q 1,bb,aaZ 0 ) (q 2,b,aZ 0 ) (q 2,ǫ,Z 0 ) (q 3,ǫ,Z 0 ) SimplPal Q = {q 0,q 1,q 2 } A = {q 2 } Pushdown Automaton (6) The transition table for SimplPal is: State Input Top of Stack Move(s) q 0 a Z 0 (q 0,aZ 0 ) q 0 b Z 0 (q 0,bZ 0 ) q 0 a a (q 0,aa) q 0 b a (q 0,ba) q 0 a b (q 0,ab) q 0 b b (q 0,bb) q 0 c Z 0 (q 1,Z 0 ) q 0 c a (q 1,a) q 0 c b (q 1,b) q 1 a a (q 1,ǫ) q 1 b b (q 1,ǫ) q 1 ǫ Z 0 (q 2,Z 0 ) (q 0,abcba,Z 0 ) (q 0,bcba,aZ 0 ) (q 0,cba,baZ 0 ) (q 1,ba,baZ 0 ) (q 1,a,aZ 0 ) (q 1,ǫ,Z 0 ) (q 2,ǫ,Z 0 )

Pushdown Automaton (7) We can rewrite the same PDA as: Q = {q 0,q 1,q 2 } Σ = {a,b,c} Γ = {a, b} A = {q 2 } δ = ((q 0,a,Z 0 ),(q 0,aZ 0 )),((q 0,b,Z 0 ),(q 0,bZ 0 )), ((q 0,a,a),(q 0,aa)),((q 0,b,a),(q 0,ba)), ((q 0,a,b),(q 0,ab)),((q 0,b,b),(q 0,bb)) ((q 0,c,Z 0 ),(q 1,Z 0 )),((q 0,c,a),(q 1,a)) ((q 0,c,b),(q 1,b)),((q 1,a,a),(q 1,ǫ)) ((q 1,b,b),(q 1,ǫ)),((q 1,ǫ,Z 0 ),(q 2,Z 0 )) Pushdown Automaton (8) r draw it as a transition diagram; a,z 0 /az 0 a, b/ab b,z 0 /bz 0 a, a/aa q 0 ǫ,z 0 /Z 0 q 1 c,a/a q 2 c,b/b c,z 0 /Z 0 b,b/ǫ b, b/bb b, a/ba a,a/ǫ

Pushdown Automaton (9) Conser the language venpal = {ww r w {a,b} } The PDA that accepts venpal is: Q = {q 0,q 1,q 2 } Σ = {a, b} Γ = {a, b} A = {q 2 } δ = ((q 0,a,Z 0 ),(q 0,aZ 0 )),((q 0,a,a),(q 0,aa)), ((q 0,a,b),(q 0,ab)),((q 0,b,Z 0 ),(q 0,bZ 0 )), ((q 0,b,a),(q 0,ba)),((q 0,b,b),(q 0,bb)), ((q 0,ǫ,ǫ),(q 1,ǫ)), ((q 1,a,a),(q 1,ǫ)),((q 1,b,b),(q 1,ǫ)), ((q 1,ǫ,Z 0 ),(q 2,ǫ)) Deterministic PDA A PDA M = (Q,Σ,Γ,q 0,Z 0,A,δ) is deterministic if we can always dece which transition will be used next, i.e. q Q,σ Σ {ǫ},x Γ, the set δ(q,σ,x) has at most one element; and q Q,σ Σ,X Γ, the sets δ(q,σ,x) and δ(q,ǫ,x) cannot both be nonempty. The SimplPal language is deterministic. The venpal language is nondeterministic. Why? A language L is deterministic context-free if there is some deterministic PDA that recognizes L. Theorem Not every non-deterministic PDA can be converted to an equivalent deterministic PDA. This has serious implications for the efficient parsing of context-free languages.

Pumping Lemma for Theorem If L is a context-free language there is an integer n such that u L with u n, u = vwxyz such that 1. wy > 0 2. wxy n 3. m 0,vw m xy m z L xample: The language AnBnCn = {a n b n c n n 0} is not a context-free language. Pumping Lemma for (2) Proof. Let s assume AnBnCn is context-free. Then a n b n c n = vw m xy m z, m 0. Conditions 1 and 2 imply that wxy has at least one symbol and no more that 2 distinct symbols. Let σ 1 be one of the symbols occurring in wy and σ 2 be the symbol that does not occur in wy. Then the string vw 0 xy 0 z, which deletes w and y from u, will have less than n occurrences of σ 1 but exactly n occurrences of σ 2. But u = vw 0 xy 0 z AnBnCn must have an equal number each symbol but we have shown that number of occurrences of σ 2 is greater than teh number of occurrences of σ 1. This contradiction invalates the assumption that AnBnCn is context-free.