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

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

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Context-Free Grammars: Normal Forms

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

CS 373: Theory of Computation. Fall 2010

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

Theory of Computation - Module 3

Context-Free Grammar

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

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

Homework 5 - Solution

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

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

CS375: Logic and Theory of Computing

Automata Theory CS F-08 Context-Free Grammars

CSCI Compiler Construction

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Properties of Context-free Languages. Reading: Chapter 7

Chap. 7 Properties of Context-free Languages

Properties of context-free Languages

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

CPSC 313 Introduction to Computability

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

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

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

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

Non-context-Free Languages. CS215, Lecture 5 c

NPDA, CFG equivalence

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

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

This lecture covers Chapter 7 of HMU: Properties of CFLs

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.

Properties of Context-Free Languages

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

EXAMPLE CFG. L = {a 2n : n 1 } L = {a 2n : n 0 } S asa aa. L = {a n b : n 0 } L = {a n b : n 1 } S asb ab S 1S00 S 1S00 100

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

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

Grammars and Context Free Languages

Ogden s Lemma for CFLs

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

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

INSTITUTE OF AERONAUTICAL ENGINEERING

Grammars and Context Free Languages

Solutions to Problem Set 3

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

Simplification and Normalization of Context-Free Grammars

CS20a: summary (Oct 24, 2002)

MA/CSSE 474 Theory of Computation

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.

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

Theory of Computer Science

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

The Pumping Lemma for Context Free Grammars

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

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,

Context Free Grammars: Introduction

SFWR ENG 2FA3. Solution to the Assignment #4

On the Sizes of Decision Diagrams Representing the Set of All Parse Trees of a Context-free Grammar

Address for Correspondence

Grammars and Context-free Languages; Chomsky Hierarchy

Chapter 16: Non-Context-Free Languages

Theory Of Computation UNIT-II

MTH401A Theory of Computation. Lecture 17

Formal Languages, Grammars and Automata Lecture 5

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

The View Over The Horizon

Recursive descent for grammars with contexts

Notes for Comp 497 (454) Week 10

Foundations of Informatics: a Bridging Course

Lecture 11 Context-Free Languages

Question Bank UNIT I

Einführung in die Computerlinguistik

Section 1 (closed-book) Total points 30

CSCI 1010 Models of Computa3on. Lecture 17 Parsing Context-Free Languages

Properties of Regular Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Theory of Computation Turing Machine and Pushdown Automata

Concordia University Department of Computer Science & Software Engineering

Context-Free Grammars and Languages. Reading: Chapter 5

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

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

CPS 220 Theory of Computation

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

C1.1 Introduction. Theory of Computer Science. Theory of Computer Science. C1.1 Introduction. C1.2 Alphabets and Formal Languages. C1.

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

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

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

Solution to CS375 Homework Assignment 11 (40 points) Due date: 4/26/2017

Finite Automata. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Context Free Grammars

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

CS375 Midterm Exam Solution Set (Fall 2017)

CYK Algorithm for Parsing General Context-Free Grammars

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

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

COSE212: Programming Languages. Lecture 1 Inductive Definitions (1)

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

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

MA/CSSE 474 Theory of Computation

Computational Models - Lecture 5 1

Homework #7. True False. d. Given a CFG, G, and a string w, it is decidable whether w ε L(G) True False

Transcription:

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

Normal Forms We want a cfg with either Chomsky or Greibach normal form Chomsky normal form A a, A BC Greibach normal form A ax, x V* 2

CFG with normal forms are easier for parsing The membership problem Given a grammar G and a string w, find the parsing tree for w if a parsing tree exists. w = x+y*z 3

-free languages A language that does not contain We consider CFG G such that L(G) is -free For any cfg G, there is G such that L(G )=L(G)-{ } 4

Transformation to normal forms: steps CFG G=(V, T, P, S) ( -free contextfree language) Remove (1) -productions (2) unit-productions (3) useless productions from P to get G Convert G to normal forms 5

A substitution rule For A B, A x 1 Bx 2, B y 1 y 2 y n is equivalent to A x 1 y 1 x 2 x 1 y 2 x 2 x 1 y n x 2, B y 1 y 2 y n Example A a aaa abbc, B abba b is equivalent to A a aaa ababbac abbc, B abba b 6

-production: A Remove -productions Nullable variable A: A * Steps 1. Find the nullable variable set V N 2. For each A x 1 x 2 x m, x i V T, For each combination x i, x j,, x k of variables in V N add A x 1 x i-1 x i+1 x j-1 x j+1... x k-1 x k+1 x m Note: don t add A, if all x i are in V N 7

Example S ABaC, A BC, B b, C D, D d Nullable set V N ={A, B, C} Add productions 8

Remove unit-productions unit-production: A B Steps Remove A A immediately Draw dependency graph for variables A and B with: A *B For A *B and B y 1 y 2 y n Add A y 1 y 2 y n Remove all A B, where A and B are in dependency graph 9

Example S Aa B, B A bb, A a bc B 1. Draw dependency graph 2. Add S bb a bc A bb B a bc 3. Remove S B, B A, A B 4. Finally S a bc bb Aa A a bc bb B a bc bb 10

Remove useless productions A variable A V is useful if S can generate some terminal string through it. That is, S * xay * w, w T* Example S asb AB Ba, A aa, B b Bb, C cb c S Ba ba. Thus, B is useful. S is useful. But, A and C are not useful (useless) 11

Two cases for useless variables Case 1: variables that cannot generate strings in T* S asb AB Ba, A aa, B b Bb, C cb c Algorithm (finding variables that generate strings) 1. V 1 ={} 2. For rule A x, x (T V 1 )*, add A to V 1 3. Repeat 2 until no rules can be added to V 1 V 1 ={S, B, C} S asb Ba, B b Bb, C cb c 12

Case 2: variables that cannot be reached from S S asb Ba, B b Bb, C cb c Algorithm: dependency graph S B C C is un-reachable from S. S asb Ba, B b Bb 13

Algorithm (removing useless productions) Input: G=(V, T, P, S) 1. Find the useless variables in Case 1 and remove related useless productions. 2. Find the useless (un-reachable) variables in Case 2 and remove the related useless productions 14

Chomosy normal form A cfg is in Chomsky normal form (CNF) if all productions are of form A BC, or A a Example S AS a, A SA b Every cfg G, with L(G), has an equivalent CNF grammar. 15

Converting into CNF 1. Apply the rules of removing -, unit-, and useless-productions 2. Convert the productions into the form A C 1 C 2 C n, or A a 3. Convert A C 1 C 2 C n into A C 1 D 1, D 1 C 2 D 2,, D n-2 C n-1 C n 16

Example S ABa, A aab, B Ac Step 2: Step 3: 17

Greibach normal form A cfg is in Greibach normal form (GNF) if all productions are of form A ab 1 B 2 B n, n 0 Example S abc, B aba, A a bbsc Every cfg G, with L(G), has an equivalent GNF grammar. 18

Example Example S AB, A aa bb b, B b Result Example S aab bbb bb, A aa bb b, B b S absb aa Result S absb aa, B b, A a 19

Parsing (membership) Question: Given a CFG G in Chomsky normal form and a string w, determine whether w L(G) Idea: the dynamic programming technique A large problem is decomposed into smaller problems Combine solutions to smaller problems into a solution for the large problem 20

Assume w=a 1 a 2 a n Main problem: V 1n ={V: V * w} w L(G) if and only S V 1n Use the dynamic programming technique V ij ={ V : V * a i a i+1 a j }: variables that can generate substring a i a i+1 a j Solve smaller problems V ik, V k+1,j, for k=i, i+1,, j-1 Combine them to compute V ij V ij = {A:A BC, B V ik, C V k+1,j, i k<j} 21

w = a 1 a 2 a 3 a i a i+1 a j-1 a j a n V ij contains the variables that generate a i a i+1 a j-1 a j a i a i+1 a k a k+1 a k+2 a j-1 a j V i i V i+1 i... V i k V k+1 j V i k+1 V k+2 j... 22

CYK Algorithm Input: G=(V, T, S, P) is in CNF and w=a 1 a 2 a n Compute V ij ={ A V : A * a i a i+1 a j } V 11, V 22,, V nn V 12, V 23,, V n-1n V 1n 1. Smallest problem: add A to V ii if A a i is a production in P 2. Bigger problem: add to A to V ij if For some k, i k j-1, A BC in P, B in V ik, C in V k+1 j 3. w L(G) if and only if S V 1n 24

Example S AB, A BB a, B AB b w=aabbb Steps V 11 ={A}, V 22 ={A}, V 33 ={B}, V 44 ={B}, V 55 ={B} V 12 =, V 23 ={S, B}, V 34 ={A}, V 45 ={A} V 13 ={S, B}, V 24 ={A}, V 35 ={S, B} V 14 ={A}, V 25 ={S, B} V 15 ={S, B} 25

Triangular table (n=5) V 1,5 V 1,4 V 2,5 V 1,3 V 2,4 V 3,5 V 1,2 V 2,3 V 3,4 V 4,5 V 1,1 V 2,2 V 3,3 V 4,4 V 5,5 23

Sum up Context-free grammars are used in designing programming languages, such as, C, PSACAL, etc. Membership problem in CFG is equivalent to the parsing problem in programming languages Normal forms are needed for automatically generating a parser for the programming language 26