Context-Free Grammar

Similar documents
Theory of Computation - Module 3

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

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

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

Context-Free Grammars: Normal Forms

Plan for 2 nd half. Just when you thought it was safe. Just when you thought it was safe. Theory Hall of Fame. Chomsky Normal Form

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

Theory Of Computation UNIT-II

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

CS375: Logic and Theory of Computing

Properties of Context-free Languages. Reading: Chapter 7

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

Context Free Grammars: Introduction. Context Free Grammars: Simplifying CFGs

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

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

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

CS 373: Theory of Computation. Fall 2010

Solution. S ABc Ab c Bc Ac b A ABa Ba Aa a B Bbc bc.

Automata Theory CS F-08 Context-Free Grammars

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

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

Context-Free Grammars and Languages

Properties of context-free Languages

Pushdown Automata. Reading: Chapter 6

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

SFWR ENG 2FA3. Solution to the Assignment #4

Theory of Computation Turing Machine and Pushdown Automata

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

MTH401A Theory of Computation. Lecture 17

CPSC 313 Introduction to Computability

Concordia University Department of Computer Science & Software Engineering

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Context Free Grammars: Introduction

Lecture 11 Context-Free Languages

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

CSE 355 Test 2, Fall 2016

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

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

Section 1 (closed-book) Total points 30

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

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

Chapter 4: Context-Free Grammars

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

Context-Free Grammars and Languages. Reading: Chapter 5

The Pumping Lemma for Context Free Grammars

Question Bank UNIT I

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

INSTITUTE OF AERONAUTICAL ENGINEERING

Context Free Grammars

CPS 220 Theory of Computation

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Context-Free Grammars (and Languages) Lecture 7

Finite Automata Theory and Formal Languages TMV026/TMV027/DIT321 Responsible: Ana Bove

Notes for Comp 497 (454) Week 10

Foundations of Informatics: a Bridging Course

Solutions to Problem Set 3

Computability and Complexity

This lecture covers Chapter 7 of HMU: Properties of CFLs

Einführung in die Computerlinguistik

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

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.

Chomsky and Greibach Normal Forms

Context Free Languages and Grammars

MA/CSSE 474 Theory of Computation

Simplification and Normalization of Context-Free Grammars

Chapter 5: Context-Free Languages

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

where A, B, C N, a Σ, S ϵ is in P iff ϵ L(G), and S does not occur on the right-hand side of any production. 3.6 The Greibach Normal Form

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

Computational Models - Lecture 4 1

Computational Models - Lecture 3

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

Computational Models - Lecture 4 1

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

FLAC Context-Free Grammars

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

Einführung in die Computerlinguistik

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

Grammars and Context Free Languages

CISC 4090 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},

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

Computational Models - Lecture 5 1

The View Over The Horizon

Properties of Context-Free Languages

CISC4090: Theory of Computation

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

Chap. 7 Properties of Context-free Languages

Context-free Grammars and Languages

Chomsky Normal Form for Context-Free Gramars

CS481F01 Prelim 2 Solutions

Miscellaneous. Closure Properties Decision Properties

Harvard CS 121 and CSCI E-207 Lecture 12: General Context-Free Recognition

Grammars and Context-free Languages; Chomsky Hierarchy

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

Grammars and Context Free Languages

Transcription:

Context-Free Grammar CFGs are more powerful than regular expressions. They are more powerful in the sense that whatever can be expressed using regular expressions can be expressed using context-free grammars, but they can also express languages that do not have regular expressions.

Context-Free Grammar

Example: Context-Free Grammar

Example Context-Free Grammar

Context-Free Grammar Construct a context free grammar for palindromes containing 0s and 1s.

Context-Free Grammar Construct a context free grammar for all integers (with sign) G ={V, Σ, R, S} V = {S, G, D, I}, Σ = {0, 1, 2, 3,., 9, +, -}

Derivation Trees

Derivation Trees

Derivation Trees

Derivation Trees

Derivation Tree

Ambiguity Consider the grammar for arithmetic expressions involving addition and multiplication operators: Consider the sentence ID+ID ID. This can be parsed in two different ways.

Ambiguity In the figure, the parse tree to the left gives the addition operator precedence over multiplication. In other words, an expression such as 3+5 9 is evaluated as (3+5) 9 with a result of 72. Whereas, the tree to the right, does what is considered the standard practice in programming languages, i.e. giving precedence over +. The previous example would be evaluated as 3+(5 9) resulting in 48. A context free grammar G is ambiguous if there exists some w L(G), which is ambiguous. If G is the grammar Show that G is ambiguous.

Ambiguity To prove that G is ambiguous, we need to find a w L(G), which is ambiguous. Consider the word abababa.

Ambiguity

What about a + a a? Removing Ambiguity

Removing Ambiguity

Simplification of CFG If G is a CFG, then we can construct a simplified equivalent CFG G with the help of following steps: 1. Eliminate all null productions to get G 1. 2. Eliminate all unit productions in G 1 to get G 2. 3. Construct a reduced Grammar G from G 2.

Simplification of CFG 1.Elimination of null productions Let G 1 =(V, Σ,P,S) be the GFG having NO null productions. Consider the Grammar G whose productions are given below. Construct a Grammar G 1 without null productions generating L(G)-{ε}

Simplification of CFG Step 1: Construction of the set W of all nullable variables W 1 ={A 1 V A 1 ε is a production in P}={A,B} W i+1 = W i {K V there exists a production K α with α W i * } W 2 ={A,B} {S} as S AB is a production with AB W 1 * = { S,A,B} W 3 = W 2 = W 2 Erasing from RHS Construction of P : D b, S as, S AB, S a, S A, S B.

Simplification of CFG 2.Elimination of unit productions A unit production in a context free grammar G is a production of the form A B where A and B are variables(non terminals) in G. For such a variable A the following steps have to be applied: Step1: Construction of the set of variables derivable from A. W 0 (A)={A} W i+1 (A)=W i (A) {B V C B is in P with C W i (A) }

Simplification of CFG Step2: Construction of A productions The A-productions in G 2 are either (a) The non unit production in G 1 or (b) A α whenever B α is in G 1 with B W(A) and α V. Now we can define G 2, where P 2 is constructed using step2 for every A V

Simplification of CFG Example: Let the productions in G 1 be S AB, A a, B C b, C D, D E and E a W(B)={B,C,D,E}, W(C)={C,D,E}, W(D)={D,E} The productions P 2 in G 2 are: S AB, A a, B a b, C a, D a and E a

Simplification of CFG 3.Construction of reduced Grammars: Many productions in P may not be useful for the purpose of derivation. It would be better to eliminate (1) variables that do not derive any terminal string and (2) symbols that are not reachable from the start symbol. If G is a CFG, then we can find an equivalent grammar G such that each variable in G derives some terminal string. Let G2=(V, Σ,P,S). We can define G =(V, Σ,P,S) as follows.

a) Construction of V : We define W i V by recursion. Simplification of CFG W 1 ={A V there exists a production A ω where ω Σ*} W i+1 =W i {A V there exists some production A α with α (Σ W i )*} At some point W k =W k+1. Then we get V =W k. b) Construction of P. P ={A α A, α (V Σ)*}

Example: Let G be Simplification of CFG S AB, A a, B b, B C, and E c Find G such that every variable in G derives some terminal string. Here we get V ={S,A,B,E}. So P = S AB, A a, B b and E c

Simplification of CFG If G=(V, Σ,P,S), we can construct an equivalent grammar G = (V, Σ,P,S) such that every symbol in V Σ is derivable from S. a) Construction of Wi W 1 ={S}. W i+1 =W i {X (V Σ) there exists a production A α with A W i and α containing the symbol X } At some point W k =W k+1. b) Construction of V, Σ, P. V =V W k, Σ = Σ W k, P ={A α A W k }

Simplification of CFG Example: Consider G=({S,A,B,E}, {a,b,c},p,s), where P consists of S AB, A a, B b and E c. W 3 ={S,A,B} {a,b} V = {S,A,B}, Σ ={a,b} P = S AB, A a, B b

Simplification of CFG Find reduced grammars equivalent to the following 1) S AB CA, B BC AB, A a, C ab b. 2) S aaa, A Sb bcc DaA, C abb DD, E ac, D ada. Answers: 1) S CA, A a, C b 2) S aaa, A Sb bcc, C abb

Normal Forms for CFG In a context free grammar, the R.H.S of a production can be any string of variables and terminals. When the production in G satisfy certain restrictions, then G is said to be in a normal form. 1. Chomsky Normal Form(CNF) In CNF we have restrictions on the length of R.H.S and the nature of symbols in the R.H.S A CFG G is said to be in CNF, if every production is of the form A a, or A BC, and S ε. When ε is in L(G) we assume that S does not appear on the R.H.S of any production.

Normal Forms for CFG Example: Consider a CFG whose productions are S AB ε, A a, B b Is it in CNF? YES Reduction to CNF Consider an example. Let G be S ABC ac, A a, B b, C c. Except S ABC ac, all other productions are in the form required for CNF. The terminal a in ac can be replaced by a new variable D. By adding a new production D a. So S ac becomes S DC.

Normal Forms for CFG S ABC is not in the required form. So it can be replaced by two productions: S AE and E BC. It is important to note that unit productions and null productions are to be removed before substitutions. Elimination of terminals on R.H.S: If there is a terminal a i on the R.H.S of a production, then add a new variable (non terminal) C ai a i Restricting the number of variables on R.H.S: Consider productions of the form A A 1 A 2.A m. We introduce new productions A A 1 C 1, C 1 A 2 C 2,, C m-2 A m-1 A m

Example1: Reduce the following grammar G to CNF: S aad, A ab bab, B b, D d. Example2: Reduce the following grammar G to CNF: S aabb, A aa a, B bb b. Normal Forms for CFG Answer: S C a C 1, A C a B C b C 2, C 1 AD, C 2 AB, B b, D d, C a a, C b b. Answer: S C a C 1, C 1 AC 2, C 2 C b B, A C a A, B C b B, C a a, C b b, A a, B b.

Normal Forms for CFG Greibach Normal Form(GNF): A grammar is in GNF if every production is of the form A aα, where α V*. And a Σ. S ε is in G, when ε is in L(G) and we assume that S does not appear on the R.H.S of any production. Example: S aab ε, A bc, B b, C c is in GNF.

Normal Forms for CFG Lemma1: Let G be a CFG, and A Bλ be a production in P. Assume that P also has the following productions: B β 1 β 2. β s We can define P 1 =(P - {A Bλ}) {A β i λ 1 i s } Then G 1 having P 1 is also a CFG equivalent to G. Example: Consider G with the following productions: A Bab, B aa bb aa AB G 1 which is equivalent to G can be constructed with the following productions. A aaab bbab aaab ABab, B aa bb aa AB

Normal Forms for CFG Lemma2: Let G contains productions of the form A Aα 1 Aα 2. Aα r β 1 β 2. β s (β i s do not start with A) Let Z be a new variable in G 1, where P 1 is defined as follows: A β 1 β 2. β s A β 1 Z β 2 Z. β s Z. Z α 1 α 2. α r Z α 1 Z α 2 Z. α r Z Example: Consider G with the following productions: A abd bdb c AB AD

Normal Forms for CFG Then G 1, equivalent to G contains the following productions: A abd bdb c A abdz bdbz cz Z B D Z BZ DZ

Reduction to GNF Normal Forms for CFG Step1: Check whether the given grammar is in CNF. If not in CNF, make it in CNF. Rename the variables as A 1,A 2,.A n. With S=A 1. Step2: A i productions should be of the form A i A j λ such that i < j. If there are productions of the form A i A i λ, apply Lemma2 to get rid of such productions. Otherwise apply Lemma1. Step3: Modify Z i productions. Apply Lemma1 to eliminate productions of the form Z i A k λ.

Normal Forms for CFG Example1: Construct a grammar in GNF equivalent to the grammar: S AA a, A SS b Answer: The given grammar is in CNF. Let S be A 1 and A be A 2. A 1 A 2 A 2 A 1 a A 2 A 1 A 1 A 2 b A 1 productions are in the required form. A 2 b is also in the required form.

Apply Lemma1 to A 2 A 1 A 1. Normal Forms for CFG We need to apply Lemma2 to A 2 A 2 A 2 A 1. Let Z 2 be the new variable. Now we can eliminate A 1 A 2 A 2 using Lemma1.

We apply Lemma1 to get Normal Forms for CFG

Normal Forms for CFG

Normal Forms for CFG Example2: Convert the following grammar into GNF. Rename The productions are :

Normal Forms for CFG

Normal Forms for CFG

Normal Forms for CFG