The Wayback Machine - https://web.archive.org/web/20240607080328/https://www.geeksforgeeks.org/simplifying-context-free-grammars/
Open In App

Simplifying Context Free Grammars

Last Updated : 22 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The definition of context free grammars (CFGs) allows us to develop a wide variety of grammars. Most of the time, some of the productions of CFGs are not useful and are redundant. This happens because the definition of CFGs does not restrict us from making these redundant productions. 

By simplifying CFGs we remove all these redundant productions from a grammar , while keeping the transformed grammar equivalent to the original grammar. Two grammars are called equivalent if they produce the same language. Simplifying CFGs is necessary to later convert them into Normal forms. 

Types of redundant productions and the procedure of removing them are mentioned below.

1. Useless productions – The productions that can never take part in derivation of any string , are called useless productions. Similarly , a variable that can never take part in derivation of any string is called a useless variable. For eg. 
 

S -> abS | abA | abB
A -> cd
B -> aB
C -> dc                         

In the example above , production ‘C -> dc’ is useless because the variable ‘C’ will never occur in derivation of any string. The other productions are written in such a way that variable ‘C’ can never reached from the starting variable ‘S’. 

Production ‘B ->aB’ is also useless because there is no way it will ever terminate . If it never terminates , then it can never produce a string. Hence the production can never take part in any derivation. 

To remove useless productions , we first find all the variables which will never lead to a terminal string such as variable ‘B’. We then remove all the productions in which variable ‘B’ occurs. 
So the modified grammar becomes – 
 

S -> abS | abA
A -> cd
C -> dc

We then try to identify all the variables that can never be reached from the starting variable such as variable ‘C’. We then remove all the productions in which variable ‘C’ occurs. 
The grammar below is now free of useless productions – 
 

S -> abS | abA
A -> cd

2. ? productions – The productions of type ‘A -> ?’ are called ? productions ( also called lambda productions and null productions) . These productions can only be removed from those grammars that do not generate ? (an empty string). It is possible for a grammar to contain null productions and yet not produce an empty string. 

To remove null productions , we first have to find all the nullable variables. A variable ‘A’ is called nullable if ? can be derived from ‘A’. For all the productions of type ‘A -> ?’ , ‘A’ is a nullable variable. For all the productions of type ‘B -> A1A2…An ‘ , where all ’Ai’s are nullable variables , ‘B’ is also a nullable variable. 

After finding all the nullable variables, we can now start to construct the null production free grammar. For all the productions in the original grammar , we add the original production as well as all the combinations of the production that can be formed by replacing the nullable variables in the production by ?. If all the variables on the RHS of the production are nullable , then we do not add ‘A -> ?’ to the new grammar. An example will make the point clear. Consider the grammar – 
 

S -> ABCd                        (1)
A -> BC                                (2)
B -> bB | ?                        (3)    
C -> cC | ?                        (4)    

Lets first find all the nullable variables. Variables ‘B’ and ‘C’ are clearly nullable because they contain ‘?’ on the RHS of their production. Variable ‘A’ is also nullable because in (2) , both variables on the RHS are also nullable. So variables ‘A’ , ‘B’ and ‘C’ are nullable variables. 

Lets create the new grammar. We start with the first production. Add the first production as it is. Then we create all the possible combinations that can be formed by replacing the nullable variables with ?. Therefore line (1) now becomes ‘S -> ABCd | ABd | ACd | BCd | Ad | Bd |Cd | d ’.We apply the same rule to line (2) but we do not add ‘A -> ?’ even though it is a possible combination. We remove all the productions of type ‘V -> ?’. The new grammar now becomes – 
 

S -> ABCd | ABd | ACd | BCd | Ad  |  Bd  |Cd | d
A -> BC | B | C
B -> bB | b
C -> cC | c

3. Unit productions – The productions of type ‘A -> B’ are called unit productions. 
To create a unit production free grammar ‘Guf’ from the original grammar ‘G’ , we follow the procedure mentioned below. 

First add all the non-unit productions of ‘G’ in ‘Guf’. Then for each variable ‘A’ in grammar ‘G’ , find all the variables ‘B’ such that ‘A *=> B’. Now , for all variables like ‘A ’ and ‘B’, add ‘A -> x1 | x2 | …xn’ to ‘Guf’ where ‘B -> x1 | x2 | …xn ‘ is in ‘Guf’ . None of the x1 , x2 … xn are single variables because we only added non-unit productions in ‘Guf’. Hence the resultant grammar is unit production free. For eg. 
 

S -> Aa | B
A -> b | B
B -> A | a

Lets add all the non-unit productions of ‘G’ in ‘Guf’. ‘Guf’ now becomes – 
 

S -> Aa
A -> b
B -> a

Now we find all the variables that satisfy ‘X *=> Z’. These are ‘S*=>B’, ‘A *=> B’ and ‘B *=> A’. For ‘A *=> B’ , we add ‘A -> a’ because ‘B ->a’ exists in ‘Guf’. ‘Guf’ now becomes 
 

    
S -> Aa
A -> b | a
B -> a

For ‘B *=> A’ , we add ‘B -> b’ because ‘A -> b’ exists in ‘Guf’. The new grammar now becomes 
 

S -> Aa
A -> b | a
B -> a | b

We follow the same step for ‘S*=>B’ and finally get the following grammar – 
 

S -> Aa | b | a
A -> b | a
B -> a | b

Now remove B -> a|b , since it doesnt occur in the production ‘S’, then the following grammar becomes,

S->Aa|b|a
A->b|a

Note: To remove all kinds of productions mentioned above, first remove the null productions, then the unit productions and finally , remove the useless productions. Following this order is very important to get the correct result. 

 


Previous Article
Next Article

Similar Reads

Ambiguity in Context free Grammar and Context free Languages
Before reading this article, we recommend you to first read about Pushdown Automata and Context Free Languages. Suppose we have a context free grammar G with production rules : S -> aSb | bSa | SS | e Left Most Derivation (LMD) and Derivation Tree : Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal
5 min read
Ambiguity in Context free Grammar and Context free Languages
Prerequisite - Pushdown Automata and Context Free Languages . Suppose we have a context free grammar G with production rules: S->aSb|bSa|SS|? Left most derivation (LMD) and Derivation Tree: Leftmost derivation of a string from starting symbol S is done by replacing leftmost non-terminal symbol by RHS of corresponding production rule. For example
4 min read
Right and Left linear Regular Grammars
Regular grammar is a type of grammar that describes a regular language. A regular grammar is a mathematical object, G, which consists of four components, G = (N, E, P, S), where N: non-empty, finite set of non-terminal symbols,E: a finite set of terminal symbols, or alphabet, symbols,P: a set of grammar rules, each of one having one of the formsA ⇢
2 min read
Regular grammar (Model regular grammars )
Prerequisites: Chomsky hierarchy Type-3 grammar/regular grammar: Regular grammar generates regular language. They have a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a non-terminal. The productions must be in the form: A ⇢ xB A ⇢ x A ⇢ Bx where A, B ∈ Variable(V) and
5 min read
Converting Context Free Grammar to Chomsky Normal Form
Prerequisite - Simplifying Context Free Grammars A context free grammar (CFG) is in Chomsky Normal Form (CNF) if all production rules satisfy one of the following conditions: A non-terminal generating a terminal (e.g.; X->x) A non-terminal generating two non-terminals (e.g.; X->YZ) Start symbol generating ε. (e.g.; S-> ε) Consider the following gra
4 min read
Converting Context Free Grammar to Greibach Normal Form
Prerequisite - Context Free Grammars, Simplifying Context Free Grammars A context free grammar (CFG) is in Greibach Normal Form (GNF) if all production rules satisfy one of the following conditions: A non-terminal generating a terminal (e.g.; X->x)A non-terminal generating a terminal followed by any number of non-terminals (e.g.; X->xX1X2…XN)
4 min read
Check if the language is Context Free or Not
Prerequisite - Pumping Lemma, How to identify if a language is regular or not We typically face questions to identify which of the given languages are context free. In case of regular languages, it is comparatively easy to answer this, but for Context Free languages, it is tricky sometimes. Pumping Lemma provides us with ability to perform negative
4 min read
Regular Expression Vs Context Free Grammar
Regular Expressions are capable of describing the syntax of Tokens. Any syntactic construct that can be described by Regular Expression can also be described by the Context free grammar. Regular Expression: (a|b)(a|b|01) Context-free grammar: S --> aA|bA A --> aA|bA|0A|1A|e *e denotes epsilon. The Context-free grammar form NFA for the Regular
2 min read
Various Properties of context free languages (CFL)
Context-Free Language (CFL) is a language which is generated by a context-free grammar or Type 2 grammar(according to Chomsky classification) and gets accepted by a Pushdown Automata. Some very much important properties of a context-free language is: Regularity- context-free languages are Non-Regular PDA language. Closure properties : The context-f
2 min read
CYK Algorithm for Context Free Grammar
Prerequisite - Converting Context Free Grammar to Chomsky Normal Form CYK algorithm is a parsing algorithm for context free grammar. In order to apply CYK algorithm to a grammar, it must be in Chomsky Normal Form. It uses a dynamic programming algorithm to tell whether a string is in the language of a grammar. Algorithm : Let w be the n length stri
2 min read
Article Tags :