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

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

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

Pushdown Automata (Pre Lecture)

Theory of Computation - Module 3

CS375: Logic and Theory of Computing

Einführung in die Computerlinguistik

CPS 220 Theory of Computation Pushdown Automata (PDA)

Context-Free Grammars and Languages. Reading: Chapter 5

Pushdown Automata. Reading: Chapter 6

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

CSE 105 THEORY OF COMPUTATION

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

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

MA/CSSE 474 Theory of Computation

5 Context-Free Languages

Theory of Computation (IV) Yijia Chen Fudan University

Theory of Computation Turing Machine and Pushdown Automata

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

Lecture 11 Context-Free Languages

CISC4090: Theory of Computation

Automata Theory CS F-08 Context-Free Grammars

Accept or reject. Stack

Section 1 (closed-book) Total points 30

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

Computability and Complexity

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

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,

CISC 4090 Theory of Computation

CPS 220 Theory of Computation

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

MTH401A Theory of Computation. Lecture 17

Solutions to Problem Set 3

PUSHDOWN AUTOMATA (PDA)

Context-free Languages and Pushdown Automata

Introduction to Theory of Computing

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

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

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

Context-Free Grammar

Miscellaneous. Closure Properties Decision Properties

CISC 4090 Theory of Computation

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

Pushdown Automata: Introduction (2)

Chapter 5: Context-Free Languages

Context-Free Grammars and Languages

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

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

Push-down Automata = FA + Stack

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

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

Foundations of Informatics: a Bridging Course

5/10/16. Grammar. Automata and Languages. Today s Topics. Grammars Definition A grammar G is defined as G = (V, T, P, S) where:

Fundamentele Informatica II

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

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

Computational Models - Lecture 4

download instant at Assume that (w R ) R = w for all strings w Σ of length n or less.

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

Intro to Theory of Computation

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

CS481F01 Prelim 2 Solutions

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

CA320 - Computability & Complexity

CSE 105 THEORY OF COMPUTATION

Context Free Languages and Grammars

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

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

Grammars and 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.

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

INSTITUTE OF AERONAUTICAL ENGINEERING

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

The Idea of a Pushdown Automaton

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

Theory Bridge Exam Example Questions

FLAC Context-Free Grammars

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

CS Pushdown Automata

UNIT-VI PUSHDOWN AUTOMATA

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

Grammars and Context Free Languages

Concordia University Department of Computer Science & Software Engineering

(pp ) PDAs and CFGs (Sec. 2.2)

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 Theory and Formal Languages TMV026/TMV027/DIT321 Responsible: Ana Bove

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

Pushdown Automata (2015/11/23)

(pp ) PDAs and CFGs (Sec. 2.2)

Context-Free Languages

Mildly Context-Sensitive Grammar Formalisms: Embedded Push-Down Automata

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

Theory Of Computation UNIT-II

Computational Models - Lecture 5 1

Theory of Computation

Context-Free Grammars: Normal Forms

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

Transcription:

Recitation 11 Notes Context Free Grammars Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x A V, and x (V T)*. Examples Problem 1. Given the language L = {a n b n : n 0}, what is the CFG that generates the language? G = ({S}, {a, b}, {S asb, S ε}, S} Problem 2. Given the language L = {a n b k : k > n 0}, what is the CFG that generates the language? G = ({S, B}, {a, b}, {S asb, S B, B bb, B b}, S). Problem 3. The language L = {w: w {a, b}*, n a (w) = n b (w)}, what is the CFG that defines the language? G = ({S}, {a, b}, {S asb, S bsa, S SS, S ε},s). What is the PDA that recognizes this language? 1. Get input symbol 2. If input symbol is an a o If the stack is empty or stack_top = A, push stack symbol A on the stack Go to Step 1. o If stack_top = B Pop it from the stack Go to Step 1. 3. If input symbol is a b o If the stack is empty or stack_top = B, push stack symbol B on the stack

Go to Step 1. o If the stack_top = A Pop it from the stack Go to Step 1. 4. If there are no more input symbols, o If stack is empty, then accept o Else Reject. Problem 4. The language L consists of a balanced set of parentheses. Write a CFG that generates the language. G = ({S}, {, }, {S S, S SS, S ε}, S). Definition: A sentential form is the start symbol S of a grammar or any string in (V T)* that can be derived from S. Consider the grammar G = ({S, A, B, C}, {a, b, c}, P, S) P = {S ABC, A aa, A ε, B bb, B ε, C cc, C ε}. Derivation 1: S ABC aabc aabcc abcc abbcc abbc abbbcabbc Definition: In a leftmost derivation, the leftmost variable is always expanded first: S ABCaABCaBCabBC abbbc abbc abbcc abbc Definition: In a rightmost derivation, the rightmost variable is always expanded first: S ABC ABcC ABc AbBc AbbBc Abbc aabbc abbc

Derivation Trees Consider Derivation 1, the tree can be constructed as follows: S A B C a A b B c C ε b B ε ε The yield of the tree is the final string obtained by reading the leaves of the tree from left to right, ignoring the ε s (unless all the leaves are ε, in which case the yield is ε). The yield of the above tree is the string abbc, as expected. Ambiguity Consider the grammar, G = ({S}, {a, b}, {S asb, S bsa, S SS, S ε},s). abab can be derived from the grammar with two parse trees. S S a S b S S b S a a S b a S b ε ε ε Definition: A grammar G is ambiguous if there exists some string w L(G) for which there are two or more distinct derivation trees, or there are two or more distinct leftmost derivations, or there are two or more distinct rightmost derivations. Definition: An inherently ambiguous language is a language for which no unambiguous grammar exists.

Push Down Automata Definition: A nondeterministic pushdown automaton or NPDA is a 7-tuple M = (Q,,Γ,δ, q 0, z, F) Q, is a finite set of states, is the input alphabet, Γ is the stack alphabet, δ is the transition function, q 0 Q, is the initial state, z Γ is the stack start symbol, and F Q is a set of final states. The transition function δ is defined as Q x ( ε) x Γ Q x Γ* Definition: An instantaneous description of a pushdown automaton is a triplet (q, w, u), q is the current state of the automaton, w is the unread part of the input string, and u is the stack contents (written as a string, with the leftmost symbol at the top of the stack). Definition: A grammar is in Chomsky Normal Form if all productions are of the form A BC or A a Where A, B, and C are variables a is a terminal. Definition: A grammar is in Greibach Normal Form if all productions are of the form A ax Where a is a terminal x V*.

Converting from CFG to NPDA Idea: Any string of a context-free language has a leftmost derivation. So, set up the NPDA so that the stack contents "correspond" to this sentential form; every move of the NPDA represents one derivation step. Characters read abb ABz aabbb Stack Characters remaining Start state q 0 just gets things initialized. Create a transition from q 0 to q 1 to put the grammar's start symbol on the stack. δ (q 0, ε, z) {(q 1, Sz)} State q 1 does the bulk of the work, represent every derivation step as a move from q 1 to q 1. Use the transition from q 1 to q f to accept the string. δ (q 1, ε, z) {(q f, z)} Example: Consider the grammar G = ({S, A, B}, {a, b}, P, S), P = {S a, S aab, A aa, A a, B bb, B b}. Rearrange the production S aab as δ (q 1, a, S) {(q 1, AB)} Initial Step S a, S aab, A aa, A a, B bb, B b Accept Step δ (q 0, ε, z) {(q 1, Sz)} δ (q 1, a, S) {(q 1,ε)} δ (q 1, a, S) {(q 1, AB)} δ (q 1, a, A) {(q 1, A)} δ (q 1, a, A) {(q 1,ε)} δ (q 1, b, B) {(q 1, B)} δ (q 1, b, B) {(q 1,ε)} δ (q 1, ε, z) {(q f, z)}

For example, the derivation S Aab aab aabb aabb maps into the sequence of moves (q 0, aabb, z) (q 1, aabb, Sz) (q 1, abb, ABz) (q 1, bb, Bz) (q 1, b, Bz) (q 1, ε, z) (q 2, ε, ε)