Simplifying Context Free Grammars
Last Updated :
30 Jan, 2025
A Context-Free Grammar (CFG) is a formal grammar that consists of a set of production rules used to generate strings in a language. However, many grammars contain redundant rules, unreachable symbols, or unnecessary complexities. Simplifying a CFG helps in reducing its size while preserving the generated language, making parsing more efficient.
Why Simplify a CFG?
Simplifying a CFG is essential for:
- Efficient Parsing: Smaller grammars require fewer computations.
- Better Understanding: Removing redundant parts makes it easier to analyze.
- Optimization: Improved computational efficiency in compilers and language processing.
Types of Redundant Productions and Their Removal
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. Null (Îŧ) 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.
Explore
Automata _ Introduction
Regular Expression and Finite Automata
CFG
PDA (Pushdown Automata)
Introduction of Pushdown Automata
5 min read
Pushdown Automata Acceptance by Final State
4 min read
Construct Pushdown Automata for given languages
4 min read
Construct Pushdown Automata for all length palindrome
6 min read
Detailed Study of PushDown Automata
3 min read
NPDA for accepting the language L = {anbm cn | m,n>=1}
2 min read
NPDA for accepting the language L = {an bn cm | m,n>=1}
2 min read
NPDA for accepting the language L = {anbn | n>=1}
2 min read
NPDA for accepting the language L = {amb2m| m>=1}
2 min read
NPDA for accepting the language L = {am bn cp dq | m+n=p+q ; m,n,p,q>=1}
2 min read
Construct Pushdown automata for L = {0n1m2m3n | m,n ⥠0}
3 min read
Construct Pushdown automata for L = {0n1m2n+m | m, n ⥠0}
2 min read
NPDA for accepting the language L = {ambncm+n | m,n ⥠1}
2 min read
NPDA for accepting the language L = {amb(m+n)cn| m,n ⥠1}
3 min read
NPDA for accepting the language L = {a2mb3m|m>=1}
2 min read
NPDA for accepting the language L = {amb2m+1 | m ⥠1}
2 min read
NPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}
3 min read
Construct Pushdown automata for L = {a2mc4ndnbm | m,n ⥠0}
3 min read
NPDA for L = {0i1j2k | i==j or j==k ; i , j , k >= 1}
2 min read
NPDA for accepting the language L = {anb2n| n>=1} U {anbn| n>=1}
2 min read
NPDA for the language L ={wÐ{a,b}* | w contains equal no. of a's and b's}
3 min read
Turing Machine
Decidability
TOC Interview preparation
TOC Quiz and PYQ's in TOC