Chomsky and Greibach Normal Forms

Similar documents
Einführung in die Computerlinguistik

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

The Pumping Lemma for Context Free Grammars

Computational Models - Lecture 5 1

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

CS375: Logic and Theory of Computing

Computational Models - Lecture 4

CS311 Computational Structures More about PDAs & Context-Free Languages. Lecture 9. Andrew P. Black Andrew Tolmach

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Computational Models - Lecture 3

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

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

Einführung in die Computerlinguistik

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

The View Over The Horizon

CS 373: Theory of Computation. Fall 2010

To make a grammar probabilistic, we need to assign a probability to each context-free rewrite

Computability Theory

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

Theory Of Computation UNIT-II

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

Context Free Grammars

Context Free Grammars: Introduction

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

Computational Models: Class 5

CISC4090: Theory of Computation

Introduction to Theory of Computing

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

Computational Models - Lecture 4 1

CISC 4090 Theory of Computation

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Context-Free Grammars: Normal Forms

Context-Free Grammar

Properties of context-free Languages

The Chomsky Hierarchy(review)

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

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

Computability and Complexity

CPS 220 Theory of Computation

Chomsky Normal Form and TURING MACHINES. TUESDAY Feb 4

Properties of Context-Free Languages

Context-Free Languages

Theory of Computation - Module 3

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Theory of Computation Turing Machine and Pushdown Automata

Concordia University Department of Computer Science & Software Engineering

Foundations of Informatics: a Bridging Course

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

Simplification and Normalization of Context-Free Grammars

Chapter 16: Non-Context-Free Languages

Context-Free Languages (Pre Lecture)

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

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

Pushdown Automata: Introduction (2)

AC68 FINITE AUTOMATA & FORMULA LANGUAGES JUNE 2014

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

(Co-)algebraic Automata Theory

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

A representation of context-free grammars with the help of finite digraphs

CS 275 Automata and Formal Language Theory

Theory of Computation (II) Yijia Chen Fudan University

Grammars (part II) Prof. Dan A. Simovici UMB

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

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

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

Chapter 4: Context-Free Grammars

This lecture covers Chapter 7 of HMU: Properties of CFLs

NPDA, CFG equivalence

In this chapter, we explore the parsing problem, which encompasses several questions, including:

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

Conflict Removal. Less Than, Equals ( <= ) Conflict

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

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

On Rice s theorem. Hans Hüttel. October 2001

Context-Free Grammars (and Languages) Lecture 7

Context Free Languages and Grammars

Context Sensitive Grammar

MTH401A Theory of Computation. Lecture 17

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Properties of Context-free Languages. Reading: Chapter 7

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

CS20a: summary (Oct 24, 2002)

Pushdown Automata (Pre Lecture)

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.

DD2371 Automata Theory

Peter Wood. Department of Computer Science and Information Systems Birkbeck, University of London Automata and Formal Languages

Chapter 6. Properties of Regular Languages

Comment: The induction is always on some parameter, and the basis case is always an integer or set of integers.

Chomsky Normal Form for Context-Free Gramars

Context-Free and Noncontext-Free Languages

CS481F01 Prelim 2 Solutions

How to write proofs - II

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

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

3515ICT: Theory of Computation. Regular languages

Rational graphs trace context-sensitive languages

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

6.8 The Post Correspondence Problem

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

Transcription:

Chomsky and Greibach Normal Forms Teodor Rus rus@cs.uiowa.edu The University of Iowa, Department of Computer Science Computation Theory p.1/25

Simplifying a CFG It is often convenient to simplify CFG One of the simplest and most useful simplified forms of CFG is called the Chomsky normal form Another normal form usually used in algebraic specifications is Greibach normal form Computation Theory p.2/25

Note Normal forms are useful when more advanced topics in computation theory are approached, as we shall see further Computation Theory p.3/25

Definition A context-free grammar G is in Chomsky normal form if every rule is of the form: A BC A a where a is a terminal, A, B, C are nonterminals, and B, C may not be the start variable (the axiom) Computation Theory p.4/25

Note The rule S ɛ, where S is the start variable, is not excluded from a CFG in Chomsky normal form. Computation Theory p.5/25

Theorem 2.6 Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea: Show that any CFG G can be converted into a CFG G in Chomsky normal form Conversion procedure has several stages where the rules that violate Chomsky normal form conditions are replaced with equivalent rules that satisfy these conditions Order of transformations:(1) add a new start variable, (2) eliminate all ɛ-rules, (3) eliminate unit-rules, (4) convert other rules Computation Theory p.6/25

Proof idea, continuation Check that the obtained CFG G define the same language as the initial CFG G. Computation Theory p.7/25

Proof Let G = (N, T, R, S) be the original CFG. Step 1: add a new start symbol S 0 s to N, and the rule S 0 S to R Note: this change guarantees that the start symbol of G does not occur on the rhs of any rule Computation Theory p.8/25

Step 2: eliminate ɛ-rules Repeat 1. Eliminate the ɛ rule A ɛ from R where A is not the start symbol 2. For each occurrence of A on the rhs of a rule, add a new rule to R with that occurrence of A deleted Example: replace B uav by B uav uv; replace B uavaw by B uavaw uvaw aavw uvw 3. Replace the rule B A, (if it is present) by B A ɛ unless the rule B ɛ has not been previously eliminated until all ɛ rules are eliminated Computation Theory p.9/25

Step 3: remove unit rules Repeat 1. Remove a unit rule A B R 2. For each rule B u R, add the rule A u to R, unless B u was a unit rule previously removed until all unit rules are eliminated Note: u is a string of variables and terminals Computation Theory p.10/25

Convert all remaining rules Repeat 1. Replace a rule A u 1 u 2... u k, k 3, where each u i, 1 i k, is a variable or a terminal, by: A u 1 A 1, A 1 u 2 A 2,..., A k 2 u k 1 u k where A 1,A 2,..., A k 2 are new variables 2. If k 2 replace any terminal u i with a new variable U i and add the rule U i u i until no rules of the form A u 1 u 2... u k with k 3 remain Computation Theory p.11/25

Example CFG conversion Consider the grammar G 6 whose rules are: S ASA ab A B S B b ɛ After first step of transformation we get: S 0 S S ASA ab A B S B b ɛ Computation Theory p.12/25

Removing ɛ rules Removing B ɛ: Removing A ɛ: S 0 S S ASA ab a A B S ɛ B b S 0 S S ASA ab a SA AS S A B S B b Computation Theory p.13/25

Removing unit rule Removing S S: Removing S 0 S: S 0 S S ASA ab a SA AS A B S B b S 0 ASA ab a SA AS S ASA ab a SA AS A B S B b Computation Theory p.14/25

More unit rules Removing A B: Removing A S: S 0 ASA ab a SA AS S ASA ab a SA AS A S b B b S 0 ASA ab a SA AS S ASA ab a SA AS A b ASA ab a SA AS B b Computation Theory p.15/25

Converting remaining rules S 0 AA 1 UB a SA AS S AA 1 UB a SA AS A b AA 1 UB a SA AS A 1 SA U a B b Computation Theory p.16/25

Note The conversion procedure produces several variables U i along with several rules U i a. Since all these represent the same rule, we may simplify the result using a single variable U and a single rule U a Computation Theory p.17/25

Greibach Normal Form A context-free grammar G = (V, Σ, R, S) is in Greibach normal form if each rule r R has the property: lhs(r) V, rhs(r) = aα, a Σ and α V. Note: Greibach normal form provides a justification of operator prefix-notation usually employed in algebra. Computation Theory p.18/25

Greibach Theorem Every CFL L where ɛ L can be generated by a CFG in Greibach normal form. Proof idea: Let G = (V, Σ, R, S) be a CFG generating L. Assume that G is in Chomsky normal form Let V = {A 1, A 2,..., A m } be an ordering of nonterminals. Construct the Greibach normal form from Chomsky normal form Computation Theory p.19/25

Construction 1. Modify the rules in R so that if A i A j γ R then j > i 2. Starting with A 1 and proceeding to A m this is done as follows: (a) Assume that productions have been modified so that for 1 i k, A i A j γ R only if i > j (b) If A k A j γ is a production with j < k, generate a new set of productions substituting for A j the rhs of each A j production (c) Repeating (b) at most k 1 times we obtain rules of the form A k A p γ, p k (d) Replace rules A k A k γ by removing left-recursive rules Computation Theory p.20/25

Removing left-recursion Left-recursion can be eliminated by the following scheme: If A Aα 1 Aα 2... Aα r are all A left recursive rules, and A β 1 β 2... β s are all remaining A-rules then chose a new nonterminal, say B Add the new B-rules B α i α i B, 1 i r Replace the A-rules by A β i β i B, 1 i s This construction preserve the language L. Computation Theory p.21/25

Conversion algorithm for (k=1; k<=m; k++) {for (j=1; j<=k-1; j++) {for (each A_k ---> A_j alpha) { for all rules A_j ---> beta add A_k ---> beta alpha remove A_k ---> A_j alpha } for (each rule A_k ---> A_k alpha) { add rules B_k ---> alpha alpha B_k remove A_k ---> A_k alpha } for (each rule A_k ---> beta, beta does not begin with A_k) add rule A_k ---> beta B_k } } Computation Theory p.22/25

More on Greibach NF See Introduction to Automata Theory, Languages, and Computation, J.E, Hopcroft and J.D Ullman, Addison-Wesley 1979, p. 94 96 Computation Theory p.23/25

Example Convert the CFG G = ({A 1, A 2, A 3 }, {a, b}, R, A 1 ) where R = {A 1 A 2 A 3, A 2 A 3 A 1 b, A 3 A 1 A 2 a} into Greibach normal form. Computation Theory p.24/25

Solution 1. Step 1: ordering the rules: (Only A 3 rules violate ordering conditions, hence only A 3 rules need to be changed) Following the procedure we replace A 3 rules by: A 3 A 3 A 1 A 3 A 2 ba 3 A 2 a 2. Eliminating left-recursion we get: A 3 ba 3 A 2 B 3 ab 3 ba 3 A 2 a, B 3 A 1 A 3 A 2 A 1 A 3 A 2 B 3 3. All A 3 rules start with a terminal. We use then to replace A 1 A 2 A 3. This introduces the rules B 3 A 1 A 3 A 2 A 1 A 3 A 2 B 3 4. Use A 1 production to make them start with a terminal Computation Theory p.25/25