Context-free Grammar
Introduction
Context-free grammar, in short CFG, is type 2 grammar according to the Chomsky hierarchy. The language generated by the CFG is called context-free language (CFL). In the previous chapters, we have discussed about finite automata and regular expression which are related to regular grammar. Regular grammar is a subset of CFG. In this chapter, we shall learn about CFG, how to derive a language from a CFG, simplification of CFG, transformation of CFG into normal form, and the pumping lemma for CFG.
6.1 Definition of Context-free Grammar
According to the Chomsky hierarchy, context-free grammar, or in short CFG, is type 2 grammar.
In mathematical description, we can describe it as
Where all the production are in the form α → β where α ∈ V N , i.e., set of non-terminals and | α | \= 1 , i.e., there will be only one non-terminal at the left hand side (LHS) and β ∈ V N U Σ, i.e., β is a combination of non-terminals and terminals.
Before describing why this type of grammar is called context free, we have to know the definition of context. Non-terminal symbols are the producing symbols because they produce some extra symbols. So, the production rules are in the format of
If any symbol is present with the producing non-terminal at the LHS of the production rule, then that extra symbol is called context. Context can be of two types: (a) left context and (b) right context.
In CFG, at the LHS of each production rules, there is only one non-terminal. (No context is added with it.) For this reason, this type of grammar is called CFG.
6.1.1 Backus Naur Form (BNF)
This is a formal notation to describe the syntax of a given language. This notation was first introduced by John Backus and Peter Naur. This was first introduced for the description of the ALGOL 60 programming language.
The symbols used in BNF are as follows:
As an example,
This can be described as a number is a digit or a number followed by a digit.
A digit is any of the characters from 0 to 9.
From BNF comes the extended BNF which uses some notations of regular expression such as + or *.
As an example, the identifier of a programming language is denoted as
BNF is widely used in CFG.
Following are some examples of CFG.
Example 6.1
Construct a CFG for the language L = {WCW R | W ∈ (a, b)*}
Solution: W is a string of any combination of a and b. So, W R is also a string of any combination of a and b, but it is a string which is reverse of W. C is a terminal symbol like a, b. Therefore, if we take C as a mirror, we will be able to see the reflection of W in the W R part, for instance, C, abCba, abbaCabba, and so on. That means, there is something that is generating symbols by replacing which it adds the same symbol before C and after C. As W ∈ (a, b)*, the null symbols are also accepted in the place of a, b. That means, only C is accepted by this language set.
From the previous discussion, the production rules are:
S → aSb/bSb/C
The grammar becomes
G = {V N , Σ, P, S}
where
V N : {S}
Σ : {a,b,c}
P : S → aSb/bSb/C
S : {S}
Example 6.2
Construct a CFG for the regular expression (0 + 1)* 0 1*.
Solution: The regular expression in English language is as follows:
Any combination of 0 and 1 followed by a single 0 and ending with any number of 1.
In this regular expression, a single 0 is in between (0 + 1)* and 1*. That is, in the language set, only 0 can exist. For constructing the grammar for this regular expression, keep the single 0 as fixed. Then before and after this 0, consider two non-terminals A and B which can be replaced multiple times.
So, the production rules (P) of the grammar for constructing this regular expression are
The grammar is
G = {V N Σ, P, S}
where
V N : {S, A, B}
Σ : {0, 1}
S : {S}
Example 6.3
Construct a CFG for the regular expression (011 + 1)* (01)*.
Solution: The regular expression consists of two parts: (011 + 1)* and (01)*. Here, from the start symbol, two non-terminals, each of them producing one part, are taken. (011 + 1) can be written as 011/1. As * represent any combination, null string is also included in the language set.
From the previous discussion, the production rules (P) of the grammar are
The grammar is
G = {V N , Σ, P, S}
where
V N : {S, A, B, C, D}
Σ : {0, 1}
S : {S}
Example 6.4
Construct a CFG for a string in which there are equal number of binary numbers.
Solution: Binary numbers means 0 and 1. Any string that belongs to the language set consists of an equal number of 0 and 1. These numbers may come in any order, but in the string the number of 0 and 1 are equal.
Hence, the string may start with 0 or may start with 1. 0 can come after 0 or 1 can come after 0. 1 can come after 1 or 0 can come after 1.
The production rules (P) for this grammar are
S → 0S1/1S0/ε
The grammar is
G = {V N , Σ, P, S}
where
V N : {S}
Σ : {0, 1}
S : {S}
Example 6.5
Construct a CFG for the string {abba(baa) n aab(aaba) n | n ≥ 0}.
Solution: In English language, the expression can be described as ‘ abba followed by any number of baa followed by aab followed by any number of aaba , in which the number of baa and number of aaba are same.’ The value of n may be 0. So, the part ‘baa’ and ‘aaba’ may be omitted—which means the string abbaaab is also accepted by the language set. One thing to be noticed in the string is that the number of (baa) and number of (aaba) are the same.
The production rules (P) for the grammar of this language set are
The grammar is
G = {V N , Σ, P, S}
where
V N : {S, A}
Σ : {a, b}
S : {S}
Example 6.6
Construct a CFG for {a n b n c m d m | n, m ≥ 1}.
Solution: The string can be described like this: A string of equal number of a and b followed by an equal number of c and d.
In this string, the number of a and the number of b will be the same and the number of c and number of d will be the same. Break the string into two parts A and B. Both are non-terminals. A will generate the string a n b n and B will generate the string c m d m .
Now, the production rules (P) for the grammar of the language will be
The grammar will be
G = {V N , Σ, P, S}
where
V N : {S, A, B}
Σ : {a, b, c, d}
S : {S}
Example 6.7
Construct a grammar for the language {a n b m c m d n | m, n ≥ 1}.
Solution: In this string, the number of a and the number of d are the same, and the number of b and the number of c are the same. Here, a comes first followed by b, c, and d. But the problem of breaking the string into two parts is that here b and c come in the middle of a and d. In this case, if we take a non-terminal for generating b m c m , then the problem will be solved.
Now, the production rules (P) for the grammar of the language will be
The grammar will be
G = {V N , Σ, P, S}
where
V N : {S, A}
Σ : {a, b, c, d}
S : {S}
6.2 Derivation and Parse Tree
6.2.1 Derivation
In the process of generating a language from the given production rules of a grammar, the non-terminals are replaced by the corresponding strings of the right hand side (RHS) of the production. But if there are more than one non-terminal, then which of the ones will be replaced must be determined. Depending on this selection, the derivation is divided into two parts:
- Leftmost derivation: A derivation is called a leftmost derivation if we replace only the leftmost non-terminal by some production rule at each step of the generating process of the language from the grammar.
- Rightmost derivation: A derivation is called a rightmost derivation if we replace only the rightmost non-terminal by some production rule at each step of the generating process of the language from the grammar.
The following examples discuss the leftmost and rightmost derivations in detail.
Example 6.8
Construct the string 0100110 from the following grammar by using
- Leftmost derivation
- Rightmost derivation
Solution:
-
Leftmost Derivation
(The non-terminals that are replaced are underlined.)
-
Rightmost Derivation
(The non-terminals that are replaced are underlined.)
Example 6.9
Construct the string abbbb from the grammar given below by using
- Leftmost derivation
- Rightmost derivation
Solution:
-
Leftmost Derivation
-
Rightmost Derivation
6.2.2 Parse Tree
Parsing a string is finding a derivation for that string from a given grammar.
A parse tree is the tree representation of deriving a CFL from a given context grammar. These types of trees are sometimes called as derivation trees.
A parse tree is an ordered tree in which the LHS of a production represents a parent node and the RHS of a production represents a children node.
There are certain conditions for constructing a parse tree from a given CFG. Those are as follows.
- Each vertex of the tree must have a label. The label is a non-terminal or terminal or null (Λ).
- The root of the tree is the start symbol, i.e., S.
- The label of the internal vertices is a non-terminal symbol ∈ V N .
- If there is a production A → X 1 X 2 . . . . . X K , then for a vertex label A, the children of that node will be X 1 X 2 . . . . . X K .
- A vertex n is called a leaf of the parse tree if its label is a terminal symbol ∈ Σ or null (Λ).
The parse tree construction is possible only for CFG. This is because the properties of a tree match with the properties of CFG.
Here, we are describing the similar properties of a tree and a CFG.
- For a tree, there must be some root. For every CFG, there is a single start symbol.
- Each node of a tree has a single label. For every CFG at the LHS, there is a single non-terminal.
- A child node is derived from a single parent. For constructing a CFL from a given CFG, a non-terminal is replaced by a suitable string at the RHS (if for a non-terminal, there are multiple productions). Each of the characters of the string is generating a node. That is, for each single node there is a single parent.
For these similarities, a parse tree can be generated only for a CFG.
Note: A parse tree is not possible with context sensitive grammar because there are production such as bB → aa or AA → B, which means at the LHS there may be more than one symbol.
Consider the following examples.
Example 6.10
Find the parse tree for generating the string 0100110 from the following grammar.
Solution: For generating the string 0100110 from the given CFG, the leftmost derivation will be
For this derivation, the parse tree is given in Fig. 6.1.
Fig. 6.1 Parse Tree for Left Most Derivation
For generating the string 0100110 from the given CFG, the rightmost derivation will be
For this derivation, the parse tree is shown in Fig. 6.2.
Fig. 6.2 Parse Tree for Right Most Derivation
Note: When you are told to generate a parse tree, you can generate that by the leftmost derivation or the rightmost derivation.
Example 6.11
Find the parse tree for generating the string 11001010 from the given grammar.
Solution: For generating the string 11001010 from the given CFG the leftmost derivation will be
For this derivation the parse tree is given in Fig. 6.3.
Fig. 6.3 Parse Tree for Left Most Derivation
For generating the string 11001010 from the given CFG, the leftmost derivation is shown in Fig. 6.4.
Fig. 6.4 Parse Tree for Right Most Derivation
Example 6.12
Construct a parse tree for the string aabbaa from the following grammar.
Solution: For generating the string from the given grammar, the derivation will be
The derivation tree is given in Fig. 6.5.
Fig. 6.5 Parse Tree
6.3 Ambiguity in Context-free Grammar
For generating a string from a given grammar, we have to derive the string step by step from the production rules of the given grammar. For this derivation, we know two types of derivations: (i) leftmost derivation and (ii) rightmost derivation. Except these two, there is also another approach called the mixed approach. Here, particularly, the leftmost or rightmost is not maintained in each step, whereas any of the non-terminals present in the deriving string is replaced by a suitable production rule.
By this process, different types of derivation can be generated for deriving a particular string from a given grammar. For each of the derivations, a parse tree is generated.
The different parse trees generated from the different derivations may be the same or may be different.
A grammar of a language is called ambiguous if any of the cases for generating a particular string, more than one parse tree can be generated.
The following examples describe the ambiguity in CFG.
(Grammar will be given, and from there one has to consider a string which produces two derivation trees to prove that the grammar is ambiguous.)
Example 6.13
Check whether the grammar is ambiguous or not.
Solution: Consider the string ‘aaa’. The string can be generated in many ways. Here, we are giving two ways.
- S → aS → aAS → aaS → aaA → aaa
- S → A → AS → ASS → aAS → aaS → aaA → aaa
The parse trees for derivation (i) and (ii) are shown in Figs. 6.6 (i) and `
Fig. 6.6
Here, for the same string derived from the same grammar we are getting more than one parse tree. So, according to the definition, the grammar is an ambiguous grammar.
Example 6.14
Prove that the following grammar is ambiguous.
Solution: Let us take a string id + id*id.
The string can be generated in the following ways.
The parse trees for derivation (i) and (ii) are shown in Figs. 6.7 and 6.8 .
Fig. 6.7 Parse Tree for Derivation(i)
Fig. 6.8 Parse Tree for Derivation(ii)
As we are getting two parse trees for generating a string from the given grammar, the grammar is ambiguous.
Example 6.15
Prove that the following grammar is ambiguous.
Solution: Take a string abababb. The string can be generated in the following ways.
The parse trees for (i) and (ii) are given in Fig. 6.9.
Fig. 6.9 Parse Tree for Derivation (i) and (ii).
As we are getting two parse trees for generating a string from the given grammar, the grammar is ambiguous.
Example 6.16
Prove that the following grammar is ambiguous.
Solution: Take a string 000111. The string can be derived in the following ways.
The parse trees for (i) and (ii) are shown is Fig. 6.10.
Fig. 6.10 Parse Tree for Derivation (i) and (ii)
As we are getting two parse trees for generating a string from the given grammar, the grammar is ambiguous.
In relation to the ambiguity in CFG, there are few more definitions. These are related to a CFL. These are:
- Ambiguous CFL: A CFG G is said to be ambiguous if there exists some w ∈ L(G) that has at least two distinct parse trees.
- Inherently Ambiguous CFL: A CFL L is said to be inherently ambiguous if all its grammars are ambiguous.
- Unambiguous CFL: If L is a CFL for which there exists an unambiguous grammar, then L is said to be unambiguous .
(Even if one grammar for L is unambiguous, then L is an unambiguous language.)
Ambiguous grammar creates problem. Let us take an example.
For a grammar G, the production rule is
E → E + E/E*E/a.
From here, we have to construct a + a*a.
The string can be generated in two different ways
So, for these two cases two parse trees are generated as shown in Fig. 6.11.
Fig. 6.11 Parse Tree for Derivation (i) and (ii)
So, the grammar is ambiguous.
In the place of ‘a’, put ‘2’. So, the derivations will be
Up to this step, both of them seem the same. But the real problem is in the parse tree as shown in Fig. 6.12.
Fig. 6.12 Annotated Parse Tree for (i) and (ii)
The correct result is 2 + 2*2 = 2 + 4 = 6 (according to the rules of mathematics * has higher precedence over +).
From here, we can decide that ambiguity is bad for programming language.
There is no particular rule to remove ambiguity from a CFG. Sometimes, ambiguity can be removed by hand. In the previous case, ambiguity can be removed by setting priority to the operators + and *. If * is set a higher priority than +, then ambiguity can be removed.
More bad news is that some CFL have only ambiguous grammar. This means, in no way the ambiguity can be removed. This type of ambiguity is called inherent ambiguity.
6.4 Left Recursion and Left Factoring
6.4.1 Left Recursion
A context-free grammar is called left recursive if a non-terminal ‘A’ as a leftmost symbol appears alternatively at the time of derivation either immediately (called direct left recursive) or through some other non-terminal definitions (called indirect/hidden left recursive).
In other words, a grammar is left recursive if it has a non-terminal ‘A’ such that there is a derivation.
6.4.1.1 Direct Left Recursion
Let the grammar be A → Aα/β, where α and β consists of terminal and/or non-terminals but β does not start with A.
At the time of derivation, if we start from A and replace the A by the production A → Aα, then for all the time A will be the leftmost non-terminal symbol.
6.4.1.2 Indirect Left Recursion
Take a grammar in the form
where α, β, γ, and δ consist of terminal and/or non-terminal.
At the time of derivation, if we start from A, replace the A by the production A → Bα, and after that replace the B by the production B → Aβ, then A will appear again as a leftmost non-terminal symbol. This is called the indirect left recursion.
In general, a grammar in the form
is the indirect left recursive grammar.
Note: The left recursive grammar is to be converted into a non-left recursive grammar because the top down parsing technique cannot handle left recursion. The top down parsing is a part of compiler design.
Immediate left recursion creates problems in top down parsing of syntax analysis in the compiler design. So, immediate left recursion must be removed.
Immediate left recursion can be removed by the following process. This is called the Moore’s proposal. For a grammar in the form
A → Aα | β, where β does not start with A,
the equivalent grammar after removing left recursion becomes
In general, for a grammar in the form
the equivalent grammar after removing left recursion is
For indirect left recursion removal, there is an algorithm.
Arrange non-terminals in some order: A 1 · · · A n
Consider the following examples to make the Moore’s proposal clear.
Example 6.17
Remove the left recursion from the following grammar.
Solution: In the grammar there are two immediate left recursions E → E + T and T → T * F.
By using Moore’s proposal the left recursion E → E + T is removed as
E → TE′ and E′ → + TE′/∈
The left recursion T → T*F is removed as
The CFG after removing the left recursion becomes
Example 6.18
Remove the left recursion from the following grammar.
Solution : The grammar has indirect left recursion. Rename S as A 1 and A as A 2 . The grammar is
For i = 1, j = 1, there is no production in the form A 1 → A 1 α.
For i = 2, j = 1, there is a production in the form A 2 → A 1 α. The production is A 2 → A 1 c. According to the algorithm for removal of indirect left recursion, the production becomes
A 2 → A 2 ac/bc
This left recursion for the production A 2 → A 2 ac/bc/d is removed and the production rules are
The actual non-left recursive grammar is
Example 6.19
Remove the left recursion from the following grammar.
Solution: The grammar has indirect left recursion. Rename S as A 1 and A as A 2 . The grammar is
For i = 1, j = 1, there is no production in the form A 1 → A 1 α.
For i = 2, j = 1, there is a production in the form A 2 → A 1 α. The production is A 2 → A 1 d.
According to the algorithm for removal of indirect left recursion, the production becomes
A 2 → A 2 ad/bd
The A 2 production is A 2 → A 2 c/A 2 ad/bd/f.
This left recursion for the production A 2 → A 2 c/A 2 ad/bd/f is removed and the production rules are
The actual non-left recursive grammar is
6.4.2 Left Factoring
Let us assume that in a grammar there is a production rule in the form A → αβ 1 /αβ 2 /. . ./αβ n . The parser generated from this kind of grammar is not efficient as it requires backtracking. To avoid this problem, we need to left factor the grammar.
Let the string that we need to construct be αβ 2 . The parser is a computer program. It will check only one symbol at a time. So, first it will take ‘α’. For getting ‘α’, there are n number of productions. But which production is to be chosen? Let us assume that it has chosen A → αβ 1 . After traversing ‘α’, it will take the next symbol ‘β 2 ’ in the string. But from the current situation it is not possible to get ‘β 2 ’. So, the parser needs to traverse back again. This is called backtracking. For this reason, we need to left factor a grammar.
To left factor a grammar, we collect all productions that have the same LHS and begin with the same symbols on the RHS. We combine the common strings into a single production and then append a new non-terminal symbol to the end of this new production. Finally, we create a new set of productions using this new non-terminal for each of the suffixes to the common production.
After left factoring, the previous grammar is transformed into:
The following is one example related to the removal of backtracking.
Example 6.20
Left Factor the following grammar.
Solution : In the previous grammar, the RHS productions abB and aB both start with ‘a’. So, they can be left factored. In the same way, cdg, cdeB, and cdfB all start with ‘cd’. So, they can also be left factored.
The left factored grammar is
6.5 Simplification of Context-free Grammar
CFG may contain different types of useless symbols, unit productions, and null productions. These types of symbols and productions increase the number of steps in generating a language from a CFG. Reduced grammar contains less number of non-terminals and productions, so the time complexity for the language generating process becomes less from the reduced grammar.
CFG can be simplified in the following three processes.
-
Removal of useless symbols
- Removal of non-generating symbols
- Removal of non-reachable symbols
- Removal of unit productions
- Removal of null productions
6.5.1 Removal of Useless Symbols
Useless symbols are of two types:
- Non-generating symbols are those symbols which do not produce any terminal string.
- Non-reachable symbols are those symbols which cannot be reached at any time starting from the start symbol.
These two types of useless symbols can be removed according to the following process.
- Find the non-generating symbols, i.e., the symbols which do not generate any terminal string. If the start symbol is found non-generating, leave that symbol. Because the start symbol cannot be removed, the language generating process starts from that symbol.
- For removing non-generating symbols, remove those productions whose right side and left side contain those symbols.
- Now find the non-reachable symbols, i.e., the symbols which cannot be reached, starting from the start symbol.
- Remove the non-reachable symbols such as rule (b).
[There is no hard and fast rule of which symbol (non-generating or non-reachable) will be removed first but, in most of the cases, the non-generating symbol is removed first, and then the non-reachable symbol is removed. But depending upon the situation, this can be changed.]
Example 6.21
Remove the useless symbols from the given CFG.
Solution: There are two types of useless symbols: non-generating and non-reachable symbols. First, we are finding non-generating symbols.
Those symbols which do not produce any terminal string are non-generating symbols.
Here, C is a non-generating symbol as it does not produce any terminal string.
So, we have to remove the symbol C. To remove C, all the productions containing C as a symbol (LHS or RHS) must be removed. By removing the productions, the minimized grammar will be
Now, we have to find non-reachable symbols, the symbols which cannot be reached at any time starting from the start symbol. There is no non-reachable symbol in the grammar. So, the minimized form of the grammar by removing useless symbols is
Example 6.22
Remove the useless symbols from the given CFG.
Solution: First, we are going to find non-generating symbols, the non-terminals which do not produce any terminal string.
Here, B is not producing any terminal string, so this is a non-generating symbol. To remove B from the production rules of the given grammar, all the productions containing B as a symbol will be removed. By removing B from the grammar, the modified production rules will be
Now we have to find non-reachable symbols, the symbols which cannot be reached starting from the start symbol.
Here, in (1), the symbol A cannot be reached by any path at any time starting from the start symbol S. So, A is a non-reachable symbol. To remove A, all the productions containing A as a symbol will be removed. By removing A from the production rules of the grammar, the minimized grammar will be
Example 6.23
Remove the useless symbol from the given grammar.
Solution : First, we are going to remove the non-generating symbols, the non-terminals which do not produce any terminal string. Here, all the symbols produce terminal strings. Thus, there is no non-generating symbol.
So, we have to remove the non-reachable symbols.
Here, non-reachable symbols are B and C. But B is a generating symbol, and so B cannot be removed. By removing C, the production rules of the grammar become
This is the minimized grammar.
Example 6.24
Reduce the following CFG by removing the useless symbols from the grammar.
Solution: First, we are going to remove the non-generating symbols from the production rules. Here, B is not producing any terminal string, and so it is a non-generating symbol. By removing B, the grammar becomes
Now, we have to find the non-reachable symbols, the non-terminals which cannot be reached starting from the start symbol. Here, symbol A cannot be reached by any path starting from the start symbol. So, A will be removed. By removing A, the production becomes
This is the reduced format of the grammar by removing the useless symbols.
6.5.2 Removal of Unit Productions
Production in the form non-terminal → single non-terminal is called unit production.
Unit production increases the number of steps as well as the complexity at the time of generating language from the grammar. This will be clear if we take an example.
Let there be a grammar
S → AB, A → E, B → C, C → D, D → b, E → a
From here, if we are going to generate a language, then it will be generated by the following way
The number of steps is 6.
The grammar, by removing unit production and as well as minimizing, will be
S → AB, A → a, B → b
From this, the language will be generated by the following way
S → AB → aB → ab
The number of steps is 3.
6.5.2.1 Procedure to Remove Unit Production
There is an algorithm to remove unit production from a given CFG.
Example 6.25
Remove the unit production from the following grammar.
S → AB, A → E, B → C, C → D, D → b, E → a
Solution: In this grammar unit, the productions are A → E, B → C, and C → D. If we want to remove the unit production A → E from the grammar, first we have to find a non-unit production in the form E → {Some string of terminal or non-terminal or both}. There is a production E → a. So, the production A → a will be added to the production rule. And the modified production rule will be
S → AB, A → a, B → C, C → D, D → b, E → a
For removing another unit production B → C, we have to find a non-unit production
C → {Some string of terminal or non-terminal or both}. But there is no such production found.
For removing a unit production C → D we have found a non-unit production D → b. So, the production C → b will be added to the production rule set and C → D will be removed. The modified production rule will be
S → AB, A → a, B → C, C → b, D → b, E → a
Now the unit production B → C will be removed by B → b as there is a non-unit production C → b. The modified production rule will be
S → AB, A → a, B → b, C → b, D → b, E → a
[This is the grammar without unit production, but it is not minimized. There are useless symbols (non-reachable symbols) C, D, and E. So, if we want to minimize the grammar, these symbols will be removed. By removing the useless symbols, the grammar will be S → AB, A → a, B → b . ]
Example 6.26
Remove the unit production from the following grammar.
S → AB, A → a, B → C, C → D, D → b
Solution: The unit productions in the grammar are
B → C, C → D
To remove unit production B → C, we have to search for a non-unit production.
C → {Some string of terminal or non-terminal or both}. There is no such production rule found. So, we are trying to remove the unit production C → D.
There is a non-unit production D → b. So, the unit production C → D will be removed by including a production C → b.
The modified production rules will be
S → AB, A → a, B → C, C → b, D → b
There is a unit production B → C that exists. This unit production can be removed by including a production B → b to the grammar.
The modified production rules will be
S → AB, A → a, B → b, C → b, D → b
In this grammar, no unit production exists.
(This grammar is unit production free, but not minimized. There are useless symbols in the form of non-reachable symbols. Here, the non-reachable symbols are C and D, which cannot be reached starting from the start symbol S. By removing the useless symbols, the grammar will become S → AB, A → a, B → b.)
Example 6.27
Remove the unit production from the following grammar.
Solution : The unit productions in the grammar are S → Y and X → S. To remove unit production S → Y, we have to find a non-unit production where Y → {Some string of terminal or non-terminal or both}. There is a non-unit production Y → Yb/b. The unit production S → Y will be removed by including production S → Yb/b in the production.
The modified production rules will be
In the previous production rules, there is a unit production X → S. This production can be removed by including the production X → aX/Yb/b to the production rules.
The modified production rules will be
(This is also minimized grammar.)
Example 6.28
Remove the unit production from the following grammar.
Solution : In the previous grammar, there is a unit production A → B. To remove the unit production, we have to find a non-unit production Y → {Some string of terminal or non-terminal or both}. There is a non-unit production B → abB/b/bb . So, the unit production A → B can be removed by including the production A → abB/b/bb to the production rule.
The modified production rule will be
(This is also minimized grammar.)
6.5.3 Removal of Null Productions
A production in the form NT → ∈ is called null production.
If Λ (null string) is in the language set generated from the grammar, then that null production cannot be removed.
That is, if we get, S
∈, then that null production cannot be removed from the production rules.
6.5.3.1 Procedure to Remove Null Production
Step I: If A → ∈ is a production to be eliminated, then we look for all productions whose right side contains A.
Step II: Replace each occurrence of A in each of these productions to obtain the non-∈ production.
Step III: These non-null productions must be added to the grammar to keep the language generating power the same.
Example 6.29
Remove the ∈ production from the following grammar.
Solution: In the previous production rules, there is a null production A → ∈. The grammar does not produce null string as a language, and so this null production can be removed.
According to first step we have to look for all productions whose right side contains A. There is one S → aA.
According to the second step, we have to replace each occurrence of ‘A’ in each of the productions. There is only one occurrence of ‘A’ in S → aA. So, after replacing, it will become S → a.
According to step three, these productions will be added to the grammar.
So, the grammar will be
Now, in the new production rules, there is no null production.
Example 6.30
Remove the ∈ production from the following grammar.
Solution : In the previous grammar, there is a null production X → ∈. In the language set produced by the grammar, there is no null string, and so this null production can be removed.
For removing this null production, we have to look for all the productions whose right side contains X. There are two such productions in the grammar S → aX and S → bX.
We have to replace each occurrence of ‘X’ in each of the productions to obtain a non-null production. In both the productions, X has occurred only once. So, after replacing, the productions will become S → a and S → b, respectively.
These productions must be added to the grammar to keep the language generating power the same. So, after adding these productions, the grammar will be
Example 6.31
Remove the ∈ production from the following grammar.
Solution: In the previous grammar, there are two null productions B → ∈ and C → ∈. The grammar does not produce any null string. So, these null productions can be removed to obtain a non-null production.
To remove the null production B → ∈, we have to find the productions whose right side contains B. There are two productions in the grammar S → ABaC and A → BC.
We have to replace each occurrence of B in each of the productions by null to obtain a non-null production. In the previous two cases, B has occurred only once. After replacing B by null, the productions will become S → AaC and A → C. These productions will be added to the grammar to keep the language generating power the same.
To remove the null production C → ∈, we have to find those productions whose right side contains C. There are two productions S → ABaC and A → BC.
We have to replace each occurrence of C in each of the productions by null to obtain a non-null production. In the previous two cases, C has occurred only once. After replacing C by null, the productions will become S → ABa and A → B. These productions will be added to the grammar to keep the language generating power the same.
In the previous two productions S → ABaC and A → BC, both B and C are there. But we have replaced B and C in two steps. If simultaneously B and C are replaced by null, the productions will become S → Aa and A → ∈, respectively. These productions will also be added to keep the language generating power the same.
So, after removing the two null productions B → ∈ and C → ∈, the modified grammar will become
Again, in this grammar, there is a null production A → ∈. By removing the null production according to the previous way, the modified grammar will become
The grammar is not in minimized format because there are three unit productions A → C, A → B, and C → D. By removing the unit productions from the grammar, the minimized grammar will become
Again, in the grammar, there is a non-reachable symbol D. By removing the non-reachable symbol, the minimized grammar will be
Example 6.32
Remove the ∈ production from the following grammar.
Solution: In the grammar, there are two null productions, A → ∈ and B → ∈. The grammar does not produce any null string. So, these null productions can be removed to obtain a non-null production.
To remove the null production A → ∈, we have to find all the productions whose RHS contains A. There are two productions S → ABAC and A → aA. Among them in the production S → ABAC, there are two A and one B. We have to replace each occurrence of A in each of the productions by null to obtain the non-null production. By replacing, we get S → ABC, S → BAC, S → BC (first rightmost, then leftmost, and then both), and A → a (for A → aA).
To remove the null production B → ∈, we have to find all the productions whose right side contains B. There are two productions S → ABAC and B → bB, each of them having a single occurrence of B. For obtaining non-null productions, we have to replace each occurrence of B in each of the productions by null. By replacing, we get
S → AAC and B → b.
In the production S → ABAC, there are both A and B. But, we have replaced A and B in two separate steps. If A and B are replaced simultaneously by null, the productions will become
S → AC, S → C
So, after removing the two null productions A → ∈ and B → ∈, the modified grammar will become
There is no null production in the grammar.
Example 6.33
Remove the ∈ production from the following grammar.
Solution: In the previous grammar, there is a null production A → ∈. But in the language set generated by the grammar, there is a null string which can be generated by the following way S → A → ∈ . As null string is in the language set, and so the null production cannot be removed from the grammar.
6.6 Linear Grammar
A grammar is called linear grammar if it is context free, and the RHS of all productions have at most one non-terminal.
As an example, S → abSc/∈, the grammar of (ab) n c n , n ≥ 0.
There are two types of linear grammar:
- Left linear grammar: A linear grammar is called left linear if the RHS non-terminal in each productions are at the left end. In a linear grammar if all productions are in the form A → Bα or A → α, then that grammar is called left linear grammar. Here, A and B are non-terminals and α is a string of terminals.
- Right linear grammar: A linear grammar is called right linear if the RHS non-terminal in each productions are at the right end. In a grammar if all productions are in the form A → αB or A → α, then that grammar is called right linear grammar. Here A and B are non-terminals and α is a string of terminals.
A linear grammar can be converted to regular grammar. Take the following example.
Example 6.34
Convert the following linear grammar into Regular Grammar.
Solution: Consider two non-terminals B and C with production B → aS and C → bA. The grammar becomes
Still the production A → bb is not a regular grammar. Replace b by a non-terminal D with production D → b. The grammar becomes
Now the grammar is regular.
For a left linear grammar, there exists a right linear grammar and vice versa. Grammar in one form can be converted into another form. The following section describes the process of conversion.
6.6.1 Right Linear to Left Linear
Step I: Generate the regular expression from the given grammar.
Step II: Reverse the regular expression obtained.
Step III: Construct the finite automata of the RE in Step II
Step IV: Generate the regular right linear grammar from the finite automata.
Step V: Reverse the right side of each production of the right linear grammar obtained in Step IV. The resulting grammar is the equivalent left linear grammar.
6.6.2 Left Linear to Right Linear
Step I: Reverse the right side of every production of the left linear grammar.
Step II: Construct the regular expression of the grammar obtained in Step I.
Step III: Reverse the constructed regular expression.
Step IV: Construct the finite automata from the RE obtained in Step III.
Step V: Construct the right linear grammar from the finite automata obtained in Step IV.
Example 6.35
Convert the following right linear grammar to left linear grammar.
Solution:
-
A → 0A → 00A → . . . . → 0*00
S → 10A + 1 → (100*00 + 1)
The regular expression generated by the grammar is (100*00 + 1).
-
Reversing the regular expression, we get (1 + 000*01).
-
The finite automata obtained from the expression in step II is
-
The constructed right linear grammar is
Here, E is a useless symbol. Eliminate all productions of E. The new productions are
-
Reversing the right side of each production of the right linear grammar,
This is the equivalent left linear grammar to the right linear grammar.
Example 6.36
Convert the following left linear grammar to right linear grammar.
Solution:
-
Reversing the right side of every production, we get
-
A → (00)*00 S → (01)*S/1(00)*00
S → (01)* 1(00)*00
The regular expression is (01)*1(00)*00.
-
Reversing the regular expression, we get 00(00) * 1(01) * .
-
The finite automata constructed from the expression in step III is
-
The right linear grammar from the finite automata is
This is the equivalent right linear grammar.
6.7 Normal Form
For a grammar, the RHS of a production can be any string of variables and terminals, i.e., (V N ∪ Σ) * . A grammar is said to be in normal form when every production of the grammar has some specific form. That means, instead of allowing any member of (V N ∪ Σ) on the RHS of the production, we permit only specific members on the RHS of the production. But these restrictions should not hamper the language generating power of the grammar.
When a grammar is made in normal form, every production of the grammar is converted in some specific form. These help us to design some algorithm to answer certain questions, such as if a CFG is converted into Chomsky normal form (CNF), one can easily answer whether a particular string is generated by the grammar or not. A polynomial time algorithm called CYK algorithm can be constructed to check this. For a CFG converted into CNF, the parse tree generated from the CNF is always a binary tree. Similarly, if a CFG is converted into Greibach normal form (GNF), then a PDA accepting the language generated by the grammar can easily be designed.
In this section we shall learn mainly two types of normal forms: (a) CNF and (b) GNF.
6.7.1 Chomsky Normal Form
A CFG is said to be in CNF if all the productions of the grammar are in the following form.
A CFG can be converted into CNF by the following process.
Step I:
- Eliminate all the ∈-production.
- Eliminate all the unit production.
- Eliminate all the useless symbols.
Step II:
- if (all the productions are in the form NT → string of exactly two NTs or NT → Single terminal) Declare the CFG is in CNF. And stop.
- else ( follow step III and/or IV and/or V).
Step III: (Elimination of terminals on the RHS of length two or more.)
Consider any production of the form
NT → T 1 T 2 . . . . T n
where n ≥ 2 (T means terminal and NT means non-terminal).
For a terminal T i , introduce a new variable (non-terminal) say NT 1 and a corresponding production NT i → T i . Repeat this for every terminal on the RHS so that every production of the grammar has either a single terminal or two or more variables.
Step IV: (Restriction of the number of variables on the RHS to two.)
Consider any production of the form
NT → NT 1 NT 2 . . . NT n
where n ≥ 3.
The production NT → NT 1 NT 2 . . . NT n is replaced by new productions
Here the A i ’s are new variables.
Step V: (Conversion of the string containing terminals and non-terminals to the string of non-terminal on the RHS)
Consider any production of the form
NT → T 1 T 2 . . . . T n NT 1 . . . . NT n ,
where n ≥ 1.
For the part T 1 T 2 ….T n , follow Step III.
Check the resultant string by Step II. If not in CNF, follow Step IV.
By this process, the generated new grammar is in CNF.
Example 6.37
Convert the following grammar into CNF.
Solution: The productions S → bA, S → aB, A → bAA, A → aS, B → aBB, B → bS are not in CNF. So, we have to convert these into CNF. Let us replace terminal ‘a’ by a non-terminal C a and terminal ‘b’ by a non-terminal C b . Hence, two new productions will be added to the grammar
C a → a and C b → b
By replacing a and b by new non-terminals and including the two productions, the modified grammar will be
In the modified grammar, all the productions are not in CNF. The productions A → C b AA and B → C a BB are not in CNF, because they contain more than two non-terminals at the RHS. Let us replace AA by a new non-terminal D and BB by another new non-terminal E. Hence, two new productions will be added to the grammar D → AA and E → BB. So, the new modified grammar will be
Example 6.38
Convert the following grammar into CNF.
Solution: All the productions in the grammar are not in CNF. We have to convert them into CNF keeping the language generating power the same.
Let us replace ‘a’ by D a , ‘b’ by D b , and ‘c’ by D c . So, three new productions D a → a, D b → b, and D c → c are added to the grammar. By replacing ‘a’ by D a , ‘b’ by D b , and ‘c’ by D c and adding three productions, the modified grammar becomes
S → ABDa and A → DaDaDb are not in CNF. Take two non-terminals E and F. Replace AB by E and DaDa by F. So, two new productions E → AB and F → DaDa will be added to the grammar. By replacement and adding the productions, the modified grammar will be
In the previous grammar, all the productions are in the form of Non-terminal → string of exactly two non-terminals or non-terminal → single terminal. So, the grammar is in CNF.
Example 6.39
Convert the following grammar into CNF.
where Σ = {+, *, id}.
Solution: Except E → id, all the other productions of the grammar are not in CNF. In the grammar E → E + E and E → E * E, there are two terminals + and *. Take two non-terminals C and D for replacing + and *, respectively. Two new productions will be added to the grammar. By replacing + and *, the modified production rules will be
In the production rules E → ECE and E → EDE, there are three non-terminals. Replace EC by another non-terminal F and ED by G. So, two new productions F → EC and G → ED will be added to the grammar. By replacing and adding the new productions, the modified grammar will be
In the previous grammar, all the productions are in the form of non-terminal → string of exactly two non-terminals or non-terminal → single terminal. So, the grammar is in CNF.
Example 6.40
Convert the following grammar into CNF.
Solution: In the grammar, there are two null productions A → ∈ and B → ∈. First, these productions must be removed, and after that the grammar can be converted into CNF. After removing the null production A → ∈, the modified grammar becomes
After removing the null production B → ∈, the modified grammar becomes
Now, this grammar can be converted into CNF. In the grammar, except A → b and B → a, all the productions are not in CNF. Let us take two non-terminals C a and C b which will replace a and b, respectively. So, two new productions C a → a and C b → b will be added to the grammar. After replacing and adding new productions, the modified grammar is
In this modified grammar, S → CaC b AB, S → Ca CbA, S → Ca C b B, and A → C b AB are not in CNF. Let us take two other non-terminals D and E, which will be replaced in the place of CaC b and AB, respectively. So, two new productions D → CaC b and E → AB will be added to the grammar. By replacing and adding two new productions, the modified grammar is
In the previous grammar, all the productions are in the form of non-terminal → string of exactly two non-terminals or non-terminal → single terminal. So, the grammar is in CNF.
6.7.2 Greibach Normal Form
A grammar is said to be in GNF if every production of the grammar is of the form
In one line, it can be said that all the productions will be in the form
Non-terminal → (single terminal)(non-terminal)*
(* means any combination of NTs including null)
Application: If a CFG can be converted into GNF, then from that GNF the push down automata (machine format of a CFG) accepting the CFL can easily be designed.
Before going into the details about the process of converting a CFG into GNF, we have to know two lemmas which are useful for the conversion process.
Lemma I: If G = (V N , Σ, P, S) is a CFG, and if A → Aα and A → β 1 /β 2 /. . . /β n belongs to the production rules (P) of G, then a new grammar G′ = (V N , Σ, P′, S) can be constructed by replacing A → β 1 /β 2 /. . . /β n in A → Aα, which will produce
A → β 1 α/β 2 α/. . . /β n α
This production belongs to P ′ in G ′ .
It can be proved that L(G) = L(G′)
Lemma II: Let G = (V N , Σ, P, S) be a CFG and the set of ‘A’ production which belongs to P be A → Aα 1 /Aα 2 /. . . /Aα m /β 1 /β 2 /. . . /β n .
Introduce a new non-terminal X. Let G′ = (V′ N , Σ, P′, S), where (V′ N = (V N ∪ X) and P′ can be formed by replacing the A productions by
It can be proved that L(G) = L(G′).
(The main production was A → Aα j /β i . If we replace A → Aα j in the production A → Aα j , then the production will become A → Aα j α j . Then replace A by β i . The production will become A → β i α j α j .
If we replace A → β i in the production A → Aα j , the production will become A → β i α j .
Take another non-terminal X, which will be replaced in the place of the string after α j in both the productions A → β i α j α j and A → β i α j . So, the A productions will be A → β i and A → β i X and X productions will be X → α j or X → α j α j ….α j , i.e., X → α j X. So, the lemma II is correct.)
6.7.2.1 Process for Conversion of a CFG into GNF
Step I: Eliminate the null productions and the unit productions from the grammar and convert the grammar into CNF.
Step II: Rename the non-terminals of V N as (A 1 , A 2 . . . . . . . A n ) with the start symbol A 1 .
Step III: Using lemma I modify the productions such that the LHS variable subscript is less than the RHS starting variable subscript, that is, in mathematical notation, it can be said that all the productions will be in the form A i → A j V where i ≤ j.
Step IV: By repeating the applications of Lemma I and Lemma II , all the productions of the modified grammar will come into GNF.
Example 6.41
Convert the following grammar into GNF.
Solution:
Step I: There are no unit productions and no null production in the grammar. The given grammar is in CNF.
Step II: In the grammar, there are two non-terminals S and A. Rename the non-terminals as A 1 and A 2 , respectively. The modified grammar will be
Step III: In the grammar, A 2 → A 1 A 1 is not in the format A i → A j V where i ≤ j. Replace the leftmost A 1 at the RHS of the production A 2 → A 1 A 1 . After replacing the modified A 2 , production will be
A 2 → A 2 A 2 A 1 /aA 1 /b
The production A 2 → aA 1 /b is in the format A → β i and the production A 2 → A 2 A 2 A 1 is in the format of A → Aα j So, we can introduce a new non-terminal B 2 and the modified A 2 production will be (according to Lemma II)
And the B 2 productions will be
Step IV: All A 2 productions are in the format of GNF. In the production A 1 → A 2 A 2 /a, A → a is in the prescribed format. But the production A 1 → A 2 A 2 is not in the format of GNF. Replace the leftmost A 2 at the RHS of the production by the previous A 2 productions. The modified A 1 productions will be
A 1 → aA 1 A 2 /bA 2 /aA 1 B 2 A 2 /bB 2 A 2
The B 2 productions are not in GNF. Replace the leftmost A 2 at the RHS of the two productions by the A 2 productions. The modified B 2 productions will be
For the given CFG, the GNF will be
Example 6.42
Convert the following CFG into GNF.
Solution:
Step I: In the grammar, there is no null production and no unit production. The grammar also is in CNF.
Step II: In the grammar, there are three non-terminals S, X, and Y. Rename the non-terminals as A 1 , A 2 , and A 3 , respectively. After renaming, the modified grammar will be
Step III: In the grammar, the production A 3 → A 1 A 2 is not in the format A i → A j V where i ≤ j.
Replace the leftmost A 1 at the RHS of the production A 3 → A 1 A 2 by the production A 1 → A 2 A 3 . The production will become A 3 → A 2 A 3 A 2 , which is again not in the format of A i → A j V where i ≤ j. Replace the leftmost A 2 at the RHS of the production A 3 → A 2 A 3 A 2 by the production A 2 → A 3 A 1 /b. The modified A 3 production will be
A 3 → A 3 A 1 A 3 A 2 /bA 3 A 2 /a
The production A 3 → bA 3 A 2 /a is in the format of A → β i and the production A 3 → A 3 A 1 A 3 A 2 is in the format of A → Aα j So, we can introduce a new non-terminal B and the modified A 3 production will be (according to Lemma II)
And B productions will be
Step IV: All the A 3 productions are in the specified format of GNF.
The A 2 production is not in the specified format of GNF. Replacing A 3 productions in A 2 productions, the modified A 2 production becomes
A 2 → bA 3 A 2 A 1 /aA 1 /bA 3 A 2 BA 1 /aBA 1 /b
Now, all the A 2 productions are in the prescribed format of GNF.
The A 1 production is not in the prescribed format of GNF. Replacing A 2 productions in A 1 , the modified A 1 productions will be
All the A 1 productions are in the prescribed format of GNF.
But the B productions are still not in the prescribed format of GNF. By replacing the leftmost A 1 at the RHS of the B productions by A 1 productions, the modified B productions will be
Now, all the B productions of the grammar are in the prescribed format of GNF.
So, for the given CFG, the GNF will be
Example 6.43
Convert the following CFG into GNF.
Solution:
Step I: In the previous grammar, there is no unit production and no null production. But all productions are not in CNF. Let us take two non-terminals D a and D b which will be placed in the place of ‘a’ and ‘b’, respectively. So, two new productions D a → a and D b → b will be added to the grammar
Now all the productions are in CNF.
Step II: There are six non-terminals in the grammar. Rename the non-terminals as A 1 , A 2 ..A 6 . After replacing, the modified productions will be
The productions for A 1 , A 2 , and A 3 are all in the format A i → A j V where i ≤ j. Replace A 6 and A 4 in the productions A 3 → A 6 A 3 and A 3 → A 4 A 4 by A 6 → b and A 4 → c, respectively. The modified A 3 productions will be
A 3 → bA 3 /cA 4 /b
All the productions are now in the format of GNF.
Replace A 5 and A 6 in the productions A 2 → A 5 A 3 and A 2 → A 6 A 2 by A 5 → a and A 6 → b, respectively. The modified A 2 productions will be
A 2 → aA 3 /bA 2 /a
All the productions are now in the format of GNF.
The A 1 productions A 1 → A 2 A 3 /A 3 A 4 are not in the format of GNF. Replace A 2 at the RHS of the production A 1 → A 2 A 3 . The modified production will be
A 1 → aA 3 A 3 /bA 2 A 3 /aA 3
Replace A 3 at the RHS of the production A 1 → A 3 A 4 . The modified production will be
A 1 → bA 3 A 4 /cA 4 A 4 /bA 4
So, for the given CFG, the GNF will be
6.8 Closure Properties of Context-free Language
A set is closed (under an operation) if and only if the operation on two elements of the set produces another element of the set. If an element outside the set is produced, then the operation is not closed.
Closure is a property which describes the application of the property on any two elements of the set; the result is also included in the set.
6.8.1 Closed Under Union
Let L 1 be a CFL for the CFG G 1 = (V N1 , Σ 1 , P 1 , S 1 ) and L 2 be a CFL for the CFG G 2 = (V N2 , Σ 2 , P 2 , S 2 ). We have to prove that L = L 1 ∪ L 2 is also in CFL.
Let us construct a grammar G = (V N , Σ, P, S) using the two grammars G 1 and G 2 as follows.
From the previous discussion, it is clear that the language set generated from the grammar G contains all the strings that are derived from S 1 as well as S 2 . So, it is proved that L = L 1 ∪ L 2 .
6.8.2 Closed Under Concatenation
Let L 1 be a CFL for the CFG G 1 = (V N1 , Σ 1 , P 1 , S 1 ) and L 2 be a CFL for the CFG G 2 = (V N2 , Σ 2 , P 2 , S 2 ). We have to prove that L = L 1 L 2 is also in CFL.
Let us construct a grammar G = (V N , Σ, P, S) using the two grammars G 1 and G 2 as follows.
From the previous discussion, it is clear that the language set generated from the grammar G contains all the strings that are derived from S 1 as well as S 2 . So, it is proved that L = L 1 L 2 .
6.8.3 Closed Under Star Closure
Let L be a CFL for the CFG G 1 = (V N1 , Σ 1 , P 1 , S 1 ). We have to prove that L* is also in CFL.
Let us construct a grammar G = (V N , Σ, P, S) using the grammar G 1 as follows.
Obviously, G is a context-free grammar. It is easy to see that L(G) = L*.
6.8.4 Closed Under Intersection
Consider two languages L 1 = {a n+1 b n+1 c n , where n, 1 ≥ 0}
and
L 2 = {a n b n c n+k , where n, k ≥ 0}.
We can easily show that the two languages L 1 and L 2 are context free (by constructing grammar).
Consider L = L 1 ∩ L 2 .
So, L = a n+1 b n+1 c n ∩ a n b n c n+k = a n b n c n , where n ≥ 0.
a n b n c n is a context sensitive language not a context free. As one instance is proved not to be context free then we can decide that context free languages are not closed under intersection.
6.8.5 Not Closed Under Complementation
From the set theory, we can prove L 1 ∩ L 2 = L 1 ∪ L 2 . (D’ Morgan’s Law)
If the union of the complements of L 1 and L 2 are closed, i.e., also context free, then the LHS will also be context free. But we have proved that L 1 ∩ L 2 is not context free. We are getting a contradiction here. So, CFLs are not closed under complementation.
6.8.6 Every Regular Language is a Context-free Language
From the recursive definition of regular set, we know that Ø and ∈ are regular expressions. If R is a regular expression, then R + R (union), R.R (concatenation), and R* (Kleene star) are also regular expressions. A regular expression R is a string of terminal symbols. Ø and ∈ are also CFLs, and we know that CFLs are closed under union, concatenation, and Kleene star. Therefore, we can write that every regular language is a CFL.
6.9 Pumping Lemma for CFL
We have become familiar with the term ‘pumping lemma’ in the regular expression chapter. The pumping lemma is also related to a CFL. The pumping lemma for CFL is used to prove that certain sets are not context free. Every CFL fulfills some general properties. But if a set or language fulfills all the properties of the pumping lemma for CFL, it cannot be said that the language is context free. But the reverse is true, i.e., if a language breaks the properties it can be said that the language is not context free.
Before discussing the statement of the pumping lemma, we need to prove the following lemma.
‘Let G be a CFG in CNF and T be a derivation tree that belongs to G. If the length of the longest path in T is less than or equal to k, then the yield of T is of length less than or equal to 2 k−1 ’.
Fig. 6.13
Proof: We will prove this by the method of induction on k. k is the longest path for all trees with root label A. (called A-Tree). When the longest path of an A-tree is of length 1, the root has only one son and that is the terminal symbol. When the root has two sons, the labels are variable (the CFG is in CNF). So, the yield of T is of length 1. We have got a base for induction.
Let k > 1, and let T be an A-tree with the longest path of length less than or equal to k. The grammar is in CNF, and so the root of T, that is, A, has exactly two sons with labels A 1 and A 2 . The length of the longest path of the two subtrees with the two sons as root is less than or equal to k − 1. (Refer to Fig. 6.13 .)
If w 1 and w 2 are the yields of T 1 and T 2 , respectively, by induction
| w 1 | ≤ 2 k−2 and | w 2 | ≤ 2 k−2
Let w be the yield of T, so w = w 1 w 2 .
| w | = | w 1 w 2 | ≤ 2 k−2 + 2 k−2 = 2 k−1
The yield of T is of length less than or equal to 2 k−1 .
Pumping Lemma for CFL: Let L be a CFL. Then, we can find a natural number n such that
- Every z ∈ L with z ≥ n can be written as w = uvwxy, for some strings u,v,w,x,y.
- | vx | ≥ 1
- | vwx | ≤ n
- uv k wx k y ∈ L for all k ≥ 0
Proof: We can prove that if G is a CFG, then we can find a CFG G 1 having no null productions such that L(G 1 ) = L(G) − {Λ}.
If the null string (Λ) belongs to the language set L, we will consider L − {Λ} and will construct the CNF of the grammar G generating L − {Λ} (if a null string does not belong to L, we will construct the CNF of G generating L).
Let the number of non-terminals (V N ) of the grammar be m. | V N | = m and n = 2 m . To prove that n is the required number, we start with a string z where z ∈ L, | z | ≥ 2 m . Construct a derivation tree or parse tree T of the string z. If the length of the longest path in T is almost m, then we can write the length of the yield of T, i.e., | z | ≤ 2 m−1 . But | z | ≥ 2 m > 2 m−1 . So, T has a path, say Γ of length greater than or equal to m + 1. The path length of greater than or equal to m + 1 needs at least m + 2 vertices, where the last vertex is the leaf. Thus, in Γ, all the labels except the last one are variables. As | N | = m, in m + 1 labels of the parse tree T, some labels are repeated.
Choose the repeated label as follows. We start with the leaf of Γ and travel upwards. We stop when we get some label repeated (let us assume that we have got label A as repeated, first).
Let v 1 and v 2 be the vertices with repeated label (say, A) obtained at the time of upward traversal from the leaf of Γ. Between v 1 and v 2 , let v 1 be nearer to the root. In Γ, the portion of the path from v 1 to leaf has only one label repeated (say A), and so its length is almost m + 1.
Let T 1 and T 2 be two subtrees taking the root as v 1 and v 2 , respectively. Let z 1 and w be the yields of T 1 and T 2 , respectively. As Γ is the longest path in T, v 1 to the leaf is the longest path of T 1 and its length is almost m + 1. So, the length of z 1 (yield of T 1 ), i.e., | z 1 | ≤ 2 m .
Fig. 6.14 Derivation for Pumping Lemma for CFL
As z is the yield of T and T 1 is a proper subtree of T, the yield of T 1 is z 1 . So, the yield of T, i.e., z, can be written as z = uz 1 y. (Refer to Fig. 6.14 .)
As z 1 is the yield of T 1 and T 2 is a proper subtree of T 1 , the yield of T 2 is w. So, the yield of T 1 , i.e., z 1 can be written as z 1 = vwx. T 2 is a proper subtree of T 1 , and so | vwx | > | w |. So, | vx | ≥ 1. Thus, we can write z = uvwxy with | vwx | ≤ n and | vx | ≥ 1. This proves (i) to (iii) of the pumping lemma.
As T is a tree with root S (S tree), and T 1 and T 2 are trees with root B (B tree), we can write S ⇒ uAy, A ⇒ vAx and A ⇒ w.
As S ⇒ uAy ⇒ uwy, uv 0 wx 0 y ∈ L. For k ≥ 1, S ⇒ uAy ⇒ uv k Ax k y ⇒ uv k wx k y ∈ L. By this, the theorem is proved.
We use the pumping lemma for CFL to show that certain sets, such as L, are not context free. We assume that the set is context free. By applying the pumping lemma, we get a contradiction.
This proof can be done in the following ways.
Step I: Assume that L is context free. Find a natural number such that | z | ≥ n.
Step II: So, we can write z = uvwxy for some strings u,v,w,x,y.
Step III: Find a suitable k such that uv k wx k y ∉ L. This is a contradiction, and so L is not context free.
Consider the following examples to get the application of the pumping lemma for CFL.
Example 6.44
Show that L = {a n b n c n where n ≥ 1} is not context free.
Solution:
Step I: Assume that the language set L is a CFL. Let n be a natural number obtained by using the pumping lemma.
Step II: Let z = a n b n c n . So, | z | = 3n > n. According to the pumping lemma for CFL, we can write z = uvwxy, where | vx | ≥ 1.
Step III: uvwxy = a n b n c n . As 1 ≤ | vx | ≤ n, (| vwx | ≤ n, so | vx | ≤ n) v or x cannot contain all the three symbols a, b, and c. So, v or x will be in any of the following forms.
- Contain only a and b, i.e., in the form a i b j .
- Or contain only b and c, i.e., in the form b i c j .
- Or contain only the repetition of any of the symbols among a, b, and c.
Let us take the value of k as 2. v 2 or x 2 will be in the form a i b i a i b i (as v is a string here aba is not equal to a 2 b or ba 2 ) or b i c j b i c j . So, uv 2 wx 2 y cannot be in the form a m b m c m . So, uv 2 wx 2 y ∉ L.
If v or x contains repetition of any of the symbols among a, b, and c, then v or x will be any of the form of a i , b i , or c i . Let us take the value of k as 0. uv 0 wx 0 y = uwy. In the string, the number of occurrences of one of the other two symbols in uvy is less than n. So, uv 2 wx 2 y ∉ L.
Example 6.45
Prove that the language L = {a i 2 /i ≥ 1 } is not context free.
Solution:
Step I: Assume that the language set L is a CFL. Let n be a natural number obtained by using the pumping lemma.
Step II: Let z = a i 2 . So, | z | = 2 i . Let 2 i > n. According to the pumping lemma for CFL, we can write z = uvwxy, where | vx | ≥ 1 and | vwx | ≤ n.
Step III: The string z contains only ‘a’, and so v and x will also be a string of only ‘a’. Let v = a p and x = a q , where (p + q) ≥ 1. Since n ≥ 0 and uvwxy ≥ 2 i , | uv n wx n y | = | uvwxy | + | v n-1 x n-1 | = 2 i + (p + q) (n − 1). As uv n wx n y ∈ L, | uv n wx n y | is also a power of 2, say 2 j .
(p + q) may be even or odd, but 2(p + q) is always even. However, 2(p + q) + 1 is odd, which cannot be a power of 2. Thus, L is not context free.
Example 6.46
Prove that L = {0 p /where p is prime} is not a CFL.
Solution:
Step I: Suppose L = L(G) is context free. Let n be a natural number obtained from the pumping lemma for CFL.
Step II: Let p be a number > n, z = 0 p ∈ L. By using the pumping lemma for CFL, we can write z = uvwxy, where | vx | ≥ 1 and | vwx | ≤ n.
Step III: Let k = 0. From the pumping lemma for CFL, we can write uv 0 wx 0 y, i.e., uwy ∈ L. As uvy is in the form 0 p , where p is prime, | uwy | is also a prime number. Let us take it as q. Let | vx | = r. Then, | uv q wx q y | = q + qr = q(1 + r). This is not prime as it has factors q(1 + r) including 1 and q(1 + r). So, uv q wx q y ∉ L. This is a contradiction. Therefore, L is not context free.
6.10 Ogden’s Lemma for CFL
If L is a context-free language, then there exists some positive integer p such that for any string w ∈ L with at least p positions marked, then w can be written as
w = uvxyz
with strings u, v, x, y, and z, such that
- x has at least one marked position,
- either u and v both have marked positions or y and z both have marked positions,
- vxy has at most p marked positions, and
- uv i xy i z is in L for every i ≥ 0.
Ogden’s lemma can be used to show that certain languages are not context free, in cases where the pumping lemma for CFLs is not sufficient. An example is the language, {a i b j c k d l : i = 0 or j = k = l}. It is also useful to prove the inherent ambiguity of some languages.
6.11 Decision Problems Related to CFG
Decision problems are the problems which can be answered in a ‘yes’ or ‘no’. There are a number of decision problems related to CFG. The following are some of them.
- Whether a given CFG is empty or not. (Does it generate any terminal string?)
- Whether a given CFG is finite? (Does it generate up to a certain length word?)
- Whether a particular string w is generated by a given CFG. (Membership)
- Whether a given CFG is ambiguous.
- Whether two given CFG have a common word.
- Whether two different CFG generate the same language.
- Whether the complement of a given CFL is also a CFL.
In the following section, we are discussing the first three decision algorithms for CFG.
6.11.1 Emptiness
In a CFG {V N , Σ, P, S }, if S generates a string of terminal, then it is non-empty; otherwise, the corresponding CFL is empty.
The following algorithm checks whether a CFG is empty or not:
While (there exist a production in the form NT i → T/String of T)
{
Replace each NT at the right hand side of each production by corresponding T or string of T.
}
if (S generates a string of terminal)
return non-empty.
else
empty.
Example 6.47
Check whether the following CFG is empty or not.
Solution: C → d is in the form NT → T. Replacing C → d in A → aBC, B → bC, and D → CD, we get
B → bd is in the form NT → string of terminals. Replacing B → bd in S → AB and A → aBd, we get
A → abdd is in the form NT → String of terminals. Replacing A → abdd in S → Abd, we get
As S generates a string of terminals, the CFG is non-empty.
Example 6.48
Check whether the following CFG is empty or not.
Solution: C → b is in the form NT → T. Replacing C → b in A → BC, B → aC, we get
B → ab is in the form NT → String of T. Replacing B → ab in S → ABD, A → Bb, D → BD, we get
A → abb is in the form NT → String of T. Replacing A → abb in S → AabD, we get
‘While’ part is complete, S does not produce any terminal string. Thus, the CFG is empty.
6.11.2 Finiteness
A language L generated from a given CFG is finite if there are no cycles in the directed graph generated from the production rules of the given CFG. The longest string generated by the grammar is determined by the derivation from the start symbol.
(The number of vertices of the directed graph is the same as the number of non-terminals in the grammar. If there is a production rule S → AB, the directed graph is as shown in Fig. 6.15 .)
Fig. 6.15
6.11.2.1 Infiniteness
A language L generated from a given CFG is infinite if there is at least one cycle in the directed graph generated from the production rules of the given CFG.
The following examples describe this in detail.
Example 6.49
Verify whether the languages generated by the following grammar are finite or not. If finite, find the longest string generated by the grammar.
Soution:
-
The grammar is not in CNF. Removing the unit productions, the grammar becomes
Now the grammar is in CNF. The directed graph for the grammar is shown in Fig. 6.16 .
Fig. 6.16
The graph does not contain any loop. So, the language generated by the CFG is finite. The derivation from the grammar is S → AB → BCB → aaa.
Thus, the length of the longest string is 3.
-
In the grammar, there is a unit production A → B. By removing the unit production, the grammar becomes
The grammar is in CNF. The non-terminal transitional graph for the grammar is shown in Fig. 6.17 .
Fig. 6.17
The graph contains loops. So, the language generated by the CFG is infinite.
6.11.3 Membership Problem
Membership problem decides whether a string w is generated by a given CFG.
This is proved by the CYK algorithm. This algorithm was proposed by John Cocke, Daniel Younger, and Tadao Kasami. According to the algorithm, the string of terminals is generated from length 1 to length of w, which is also the string to be checked for membership. If w ∊ the set of string generated, then w is a member of the strings generated by the CFG. For this, we need to convert the given grammar into CNF. With the help of an example we are showing this.
Example 6.50
Let the grammar converted to CNF is
Check whether baba is a member of the CFL generated by the CFG.
Solution: Start producing strings of length 1.
| String
|
Producing NT
| | --- | --- | |
A
|
A
| |
A
|
B
| |
A
|
C a
| |
B
|
C b
|
Produce strings of length 2
| String
|
Producing NT
| | --- | --- | |
ba
|
S (S → C b A)
| |
aa
|
S (S → C a B)
| |
aa
|
D (D → AA)
| |
aa
|
E (E → BB)
|
Produce strings of length 3. This may be the first one produced by an NT and the last two produced by another NT or vice versa.
| String
|
Producing NT
| | --- | --- | |
baa
|
A (A → C b D)
| |
aba
|
A (A → C a S)
| |
aaa
|
A (A → C a S)
| |
aaa
|
B (B → C a E)
| |
bba
|
B (B → C b S)
| |
baa
|
B (B → C b S)
|
Produce strings of length 4. This may be the First one produced by an NT and the last three produced by another NT or the first two produced by an NT and the last two produced by another NT or the first three produced by an NT and the last one produced by another NT. Two–two combination is not available for this grammar as there is no production combining two of S, D, and E.
| String
|
Producing NT
| | --- | --- | |
abaa
| D (D → AA, A → a, A → C b D) | |
baaa
| D (D → AA, A → C b D, A → a) | |
aaba
| D (D → AA, A → a, A → C a S) | |
abaa
| D (D → AA, A → C a S, A → a) | |
aaaa
| D (D → AA, A → C a S, A → a) | |
aaaa
| D (D → AA, A → a, A → C a S) | |
aaaa
| E (E → BB) | |
aaaa
| E (E → BB) | |
abba
| E (E → BB) | |
bbaa
| E (E → BB) | |
abaa
| E (E → BB) | |
baaa
| E (E → BB) | |
aaaa
| S (S → C a B) | |
abba
| S (S → C a B) | |
abaa
| S (S → C a B) | |
bbaa
| S (S → C b A) | |
baba
| S (S → C b A) | |
baaa
| S (S → C b A) |
The string in bold is ‘baba’. Thus, baba is a member of the CFL generated by the CFG.
6.12 CFG and Regular Language
According to the Chomsky hierarchy, we know that a regular grammar is a subset of CFG. Thus, for every regular language, there exists a CFG. In this section, we shall discuss two theorems related to this
Theorem 1: Every regular language is generated by a CFG.
Proof: Assume that L is regular. Thus, there exists a DFA M = {Q, Σ, δ, q 0 , F} accepting L. From the DFA M, we can generate a CFG G = {V N , Σ, P, S } using the following rules.
- Every state ∈ Q of M is treated as a non-terminal ∈ V N . The start state S = q 0 (initial state of DFA).
- For a transitional function δ(q 1 , a) → q 2, where q 1 , q 2 ∈ Q and a ∈ Σ, add a production q 1 → aq 2 in P.
- If q 2 is a final state, add q 1 → aq 2 and q 1 → a in P.
All there productions have a single non-terminal at the LHS. Thus, it is context free.
Theorem 2: The language generated by a regular CFG is a regular language. Or every regular grammar generates a regular language.
Proof: Let G = {V N , Σ, P, S } be a regular CFG. From this grammar, construct a NDFA M = {Q, Σ, δ, q 0 , F} using the following rules.
-
For each production in the form < NT 1 > → <i/p> <NT 2 >, add a transitional function
δ(<NT 1 >, <i/p>) → <NT 2 >
-
For each production in the form <NT 1 > → <i/p>, add a transitional function
δ(<NT 1 >, <i/p>) → final state
We know that a language accepted by an NDFA is regular expression. Thus, it is proved.
Every regular grammar is some right linear grammar. Already it is given that a right linear grammar can be converted to a left linear grammar. Thus, it can be proved that if L is a regular set, then L is generated by some left linear or some by some right linear grammar.
6.13 Applications of Context-free Grammar
The compiler is a program that takes a program written is the source language as input and translates it into an equivalent program in the target language. Syntax analysis in an important phase in the compiler design. In this phase, mainly grammatical errors called syntax errors are checked. The syntax analyzer (parser) checks whether a given source program satisfies the rules implied by a context-free grammar or not. If it satisfies, the parser creates the parse tree of that program. Otherwise, the parser gives the error messages.
In C language, an identifier is described by the following CFG.
The definition of an identifier in a programming language is
Let a programmer declare the following variables
int capital;
int r_o_i;
int year;
int 1st_year_interest;
then the syntax error will be shown in the fourth line as it does not match with the language produced by the grammar.
For the iteration statements (loop) also, there are CFGs.
The CFG is used to develop extensive markup language (XML). XML is a markup language much like HTML. XML is used as a database and can be applicable to share and store data. It can even be used to construct new languages such as WML.
What We Have Learned So Far
-
According to the Chomsky hierarchy, context-free grammar, in short CFG, is type 2 grammar.
-
In every production of a CFG, at the left hand side there is a single non-terminal.
-
A derivation is called a leftmost derivation if only the leftmost non-terminal is replaced by some production rule at each step of the generating process of the language from the grammar.
-
A derivation is called a rightmost derivation if only the rightmost non-terminal is replaced by some production rule at each step of the generating process of the language from the grammar.
-
Parsing a string is finding a derivation for that string from a given grammar.
-
A parse tree is an ordered tree in which the left hand side of a production represents a parent node and the right hand side of a production represents the children nodes.
-
A grammar of a language is called ambiguous if from any of the cases for generating a particular string, more than one parse tree can be generated.
-
Ambiguity in grammar creates problem at the time of generating languages from the grammar.
-
There is no hard and fast rule for removing ambiguity from a CFG. Some ambiguity can be removed by hand. Some CFGs are always ambiguous. Those are called inherently ambiguous.
-
CFG may contain different types of useless symbols, unit productions, and null productions. These types of symbols and productions increase the number of steps in generating a language from a CFG.
-
Reduced grammar contains less number of non-terminals and productions, and so the time complexity for the language generating process becomes less from reduced grammar.
-
Useless symbols are of two types, non-generating symbols and non-reachable symbols.
-
Non-generating symbols are those symbols which do not produce any terminal string.
-
Non-reachable symbols are those symbols which cannot be reached at any time starting from the start symbol.
-
Production in the form non-terminal → single non-terminal is called unit production. Unit production increases the number of steps as well as the complexity at the time of generating the language from the grammar.
-
A production in the form NT → ∈ is called null production. If Λ (null string) is in the language set generated from the grammar, then that null production cannot be removed.
-
When a grammar is made in the normal form, every production of the grammar is converted in some specific form. These help us to design some algorithm to answer certain questions.
-
A CFG is called in CNF if all the productions of the grammar are in the following form.
-
A grammar is said to be in Greibach normal form (GNF) if every production of the grammar is of the following form.
-
Context-free languages are closed under union, concatenation, and star closure.
-
Context-free languages are not closed under intersection and complementation.
-
The pumping lemma and the Ogden’s lemma for context-free language are used to prove that certain sets are not context free.
-
A language L generated from a given CFG is finite if there are no cycles in the directed graph generated from the production rules of the given CFG.
-
A language L generated from a given CFG is infinite if there is at least one cycle in the directed graph generated from the production rules of the given CFG.
Solved Problems
-
Construct a CFG for the following
- Set of string of 0 and 1 where consecutive 0 can occur but no consecutive 1 can occur.
- Set of all (positive and negative) odd integers.
- Set of all (positive and negative) even integers.
Solution:
-
There will be two non-terminals, S and A. S will produce production S → 0S. As two consecutive 0 can appear, there will be a production S → 0. In the language set, 01 can appear. So, there will be production S → 1.
The string can start with 1, and so there may be a production S → 1S. But the consecutive 1 will not appear, and so the production will be S → 1A. ‘A’ can produce 0 but not 1. So, there will be a production A → 0.
If the string starts with 1, then the production rule S → 1A is applied. As in the language set, there will be no consecutive 1, and so A may produce A → 0A. But if this is the production, then there will be no chance of an occurring 1 in the string except at the last. So, the production will be A → 0S.
So, the grammar is
-
An integer is odd if its rightmost entry contains any one of ‘1’, ‘3’, ‘5’, ‘7’, or ‘9’. If the integer is positive, then its leftmost entry contains a ‘+’ sign; if negative, then its leftmost entry contains a ‘−’ sign. The production rules are
The set of non-terminals V N are {S, <sign>, <integer>, <integer1>, <digit>, <digit1>}.
-
An integer is odd if its rightmost entry contains any one of ‘2’, ‘4’, ‘6’, ‘8’, or ‘0’. If the integer is positive, then its leftmost entry contains a ‘+’ sign; if negative, then its leftmost entry contains a ‘−’ sign. The production rules are
The set of non-terminals V N are {S, <sign>, <integer>, <integer1>, <digit>, <digit1>}.
-
Construct the string aaabbabbba from the grammar
By using
- Leftmost derivation
- Rightmost derivation.
Solution:
-
Leftmost Derivation
-
Rightmost Derivation
-
Show the derivation tree for the string ‘aabbbb’ with the following grammar.
Solution:
-
Let G be the grammar S → aB/ba, A → a/aS/bAA, B → b/bS/aBB. For the string aaabbabbba find a
- leftmost derivation
- rightmost derivation
- parse tree.
[WBUT 2009]
Solution:
-
Leftmost Derivation
-
Rightmost Derivation
-
Parse Tree
-
Check whether the following grammar is ambiguous or not.
S → a/Sa/bSS/SSb/SbS
Solution: Let us take a string baababaa. Let us try to generate the string by deriving from the given CFG.
Derivation 1: The string is derived by the leftmost derivation, i.e., only the leftmost non-terminal is replaced by a suitable non-terminal.
Derivation 2: The string is derived by the rightmost derivation, i.e., only the rightmost non-terminal is replaced by a suitable non-terminal.
The parse tree for the first derivation is as follows:
The parse tree for the second derivation is as follows:
For the string baababaa, two different parse trees are constructed. So, the given CFG is ambiguous.
-
Check whether the following grammar is ambiguous or not.
S → SS/a/b
Solution: Let us take a string abba. Let us try to generate the string by deriving from the given CFG.
Derivation 1: The string is derived by the leftmost derivation, i.e., only the leftmost non-terminal is replaced by a suitable non-terminal.
Derivation 2: The string is derived by the leftmost derivation, i.e., only the leftmost non-terminal is replaced by a suitable non-terminal.
The parse tree for the first derivation is as follows:
The parse tree for the second derivation is as follows:
For the string abba, two different parse trees are constructed. So, the given CFG is ambiguous.
-
Consider G whose productions are S → aAS/a, A → SbA/SS/ba. Show that S → aabbaa by constructing a derivation tree, by rightmost derivation, whose yield is aabbaa.
[WBUT 2008]
Solution: S is the start symbol of the grammar
-
Show that the grammar G with production
is ambiguous.
[JNTU 2007]
Solution: Consider a string abababb. The language can be constructed in two ways.
-
S → abSb → aAaAbb → ababSbb → abababb
-
S → aAb → abSb → ababSbb → abababb
As there are two parse trees for generating a single string, we can say that the grammar is ambiguous.
-
-
Check whether the following grammar is ambiguous.
Solution: Let us take a language ibtibtaea.
The language can be constructed in two ways.
-
(replacing all ‘C’ by b and all ‘S’ by a) -
(replacing all ‘C’ by b and all ‘S’ by a)
So, for generating the language, two parse trees can be generated. The parse trees are as follows:
As there are two parse trees for generating a single string, we can say that the grammar is ambiguous.
-
-
Simplify the following CFG.
Solution : Simplifying a CFG means
-
Removing useless symbols.
-
Removing unit production.
-
Removing null production.
Simplify the CFG by the following steps.
Remove Null Production: In the grammar, there is a null production B → ∈ . The grammar does not produce any null string. So, the null production can be removed. To remove the null productions, find all the productions whose right side contains a non-terminal B. There are two productions, S → AbB and S → aaB. To remove the null production, replace each occurrence of B by ∈ and add that new production to the grammar to keep the language generating power the same.
The grammar after removing the null production becomes
Remove Unit Production: In the grammar, there are three unit productions, A → D, D → E, and E → F. To remove the unit production E → F, we have to find a non-unit production F → {Some string of terminal or non-terminal or both}. There is such a production F → aS. Remove E → by that non-unit production F → aS.
After removing the unit production E → F, the grammar becomes
By the same process, after removing all the unit productions, the grammar becomes
Remove Useless Symbols: In a grammar, there are two types of useless symbols.
- Non-generating symbol
- Non-reachable symbol
In the grammar, all are generating symbols, and so we have to find the non-reachable symbol.
In the grammar D, E, and F are non-reachable symbols because they are not reached any way starting from the start symbol. After removing the useless symbols, the grammar becomes
-
-
Find a reduced grammar equivalent to the grammar G, whose productions are the following:
[UPTU 2004]
Solution: Reduction of a CFG is done by
- Removing useless symbols.
- Removing unit production.
- Removing null production.
Removing useless symbol : In the production rules, there is no non-reachable symbol. But B is a non-generating symbol as it does not generate any terminal string. Removing ‘B’, the modified production rules become
In the modified production rules, there is no unit production or null production. Thus, it is the reduced grammar equivalent to G.
-
S → AB, A → a, B → C/b, C → D, D → E, E → a. Remove the unit productions.
[WBUT 2008]
Solution: In the previous grammar, B → C, C → D, and D → E are unit productions.
To remove the unit production D → E, find all the productions whose right hand side contains a terminal symbol. There is a production E → a, and so the D production becomes D → a. The modified production rules become
To remove the unit production C → D, we need to find a production whose right hand side contains a terminal symbol. There is a production D → a, and so the C production become C →
- The modified production rules become
By the same process, the B production becomes B → a. The modified productions with no unit productions become
-
Remove all the unit productions and all the → productions from the following CFG.
Solution: The production A → ∈ is a ∈ production. The language set produced by the grammar does not contain any null string. Therefore, this ∈ production can be removed. For removing the ∈ production, we have to find all the productions whose right hand side contains A. The productions are S → aA and A → aaA. Replacing A by ∈, the modified productions become
-
Find a reduced grammar equivalent to the following grammar.
Solution: The original grammar is
First, we have to remove useless symbols. Here, C is non-reachable symbol. So, the production C → aB is removed.
After removal of the non-reachable symbol, the modified production rules become
Here, the symbol A is not producing any terminal string, and so it is the non-generating symbol. But the symbol cannot be removed as it is attached with the starting symbol production
S → aAa
So, the reduced grammar is
-
Convert the following grammar into CNF.
Solution: A CFG is called a CNF if all the productions of the grammar are in the following form:
The previous grammar does not contain any useless symbol, unit production, or null production. So, the grammar can be directly converted to CNF.
In the previous grammar, S → bA, S → aB, B → bAA, B → aS, A → aBB, and B → bS are not in CNF. So, these productions are needed to be converted into CNF. But the productions A → b and B → a are in CNF.
Consider two extra non-terminals C a and C b and two production rules C a → a and C b → b. Replace ‘a’ by C a and ‘b’ by C b in the previous productions. The modified production rule becomes
In the grammar, A → C a BB and B → C b AA are not in CNF as they contain a string of three non-terminals at the right hand side of the production. Introduce two production rules X → BB and Y → AA and replace BB by X and AA by Y. The modified production rule becomes
Here, all the productions are in the specified format of CNF. The CFG is converted to CNF.
-
Convert the following grammar into CNF.
Solution: The productions S → aAbB, A → aA, and B → bB are not in CNF. Consider two productions C a → a and C b → b. The modified production becomes
Replacing C a A by A and C b B by B, the modified production becomes
Now the grammar is in CNF.
-
Convert the following grammar into CNF.
Solution: The CFG is not simplified as it contains useless symbols and unit productions. First, we have to simplify the CFG and then it can be converted into CNF.
In the grammar, E is the useless symbol (non-generating symbol) as it does not produce any terminal symbol. So, the production A → E is removed. The modified grammar becomes
There are two unit productions in the grammar. After removing the unit productions, the grammar becomes
In the grammar, except S → a, all other productions are not in CNF.
Consider four extra non-terminals C a , C b , C c , C d and two production rules C a → a and C b → b, C c → c and C d → d. Replace ‘a’ by C a and ‘b’ by C b , c by C c and d by C d in the previous productions. The modified production rule becomes
Here, S → C c CD, C → C c CD, and D → C a C b C d are not in CNF. Introduce two production rules X → CD and Y → C b C d and replace CD by X and C b C d by Y. The modified production rule becomes
Here, all the productions are in the specified format of CNF. The CFG is converted to CNF.
-
Convert the following grammar into CNF.
Solution: The grammar contains two non-terminal symbols E and T and four terminal symbols +, (,), and a. The grammar contains a unit production E → T. First, the unit production has to be removed. After removing the unit production E → T, the modified grammar becomes
In the previous grammar, except E → a and T → a all the other productions are not in CNF.
Introduce three non-terminals A, B, and C and three production rules A → +, B → (and C →) and an appropriate terminal by appropriate non-terminals. The modified production rules become
In the previous grammar, E → EAT, E → BEC, and T → BEC are not in CNF. Consider two non-terminals X and Y and two production rules X → AT and Y → EC. The modified production rules become
Here, all the productions are in the specified format of CNF. The CFG is converted to CNF.
-
Convert the following grammar into CNF.
Solution: The grammar contains unit production A → B. After removing the unit production, the grammar is
Except S → a and B → b, the productions of the grammar are not in CNF.
Consider two non-terminals C a and C b and two production rules C a → a and C b → b and replace ‘a’ by C a and ‘b’ by C b in the appropriate production. The modified production rules become
Consider three non-terminals X, Y, and Z and three production rules X → BC b , Y → AC b , and Z → C a A. Replace the appropriate group of non-terminals in the production by an appropriate new non-terminal. The production rule becomes
-
Convert the following CFG into an equivalent grammar in CNF.
Solution: In CNF, all the productions will be in the form
In the previous grammar, expect A → a and B → b all the productions are not in CNF.
Consider two productions C a → a and C b → b.
The modified grammar becomes
Replace C a A by X 1 , C b B by X 2 , C a C b by X 3 , and AB by X 4 , The modified grammar becomes
The previous grammar is in CNF.
-
Convert the following grammar into CNF.
Solution: The grammar contains ∈ production. First, we have to Remove the ∈ productions.
Removing the ∈ productions, the modified grammar becomes
The grammar contains the unit production S → C. Removing the unit production, the grammar becomes
Now, the grammar can be converted to CNF.
Introduce two new productions C a → a and C b → b. Replacing C a and C b in appropriate positions in the grammar, the modified grammar becomes
AC can be replaced by S in the ‘S’ productions for the cases S → ACD/AAC, and AAC can be replaced by S for the production S → AACD. Introduce three non-terminals E, F, and G for C a A, C a D, and C b D, respectively. Including these changes, the modified grammar becomes
Now, the grammar is in CNF.
-
Convert the following grammar into GNF.
Solution: The grammar is not in CNF. So, it has to be converted into CNF. Introduce two non-terminals C a , C b and two production rules C a → a, C b → b.
Introduce two non-terminals X and Y and two production rules X → BC a and Y → AC b .
The production rule becomes
Step I: In the grammar, there is no null production and no unit production. The grammar also is in CNF.
Step II: In the grammar, there are seven non-terminals S, A, B, C a , C b , X, and Y. Rename the non-terminals as A 1 , A 2 , A 3 , A 4 , A 5 , A 6 , and A 7 , respectively. After renaming the non-terminals, the modified grammar will be
Step III: In the previous production, A 6 → A 3 A 4 and A 7 → A 2 A 5 are not in the form A i → A j V, where i ≤ j.
Using Lemma I, replace A 3 → A 5 A 7 /b in the production A 6 → A 3 A 4 . The rule becomes
A 6 → A 5 A 7 A 4 /bA 4
Still A 6 → A 5 A 7 A 4 is not in the form A i → A j V, where i ≤ j. Using Lemma I, replace A 5 → b in the production. The modified production is A 6 → bA 7 A 4 /bA 4 , which is in GNF.
Step IV: Using Lemma I, replace A 2 → A 4 A 6 /a in the production A 7 → A 2 A 5 . The production rule becomes A 7 → A 4 A 6 A 5 /aA 5 .
Still A 7 → A 4 A 6 A 5 is not in the form A i → A j V, where i ≤ j. Using Lemma I, replace A 4 → a in the production.
The modified production is A 7 → aA 6 A 5 /aA 5 , which is in GNF.
Lemma II can be applied on the productions A 2 → A 4 A 6 /a and A 3 → A 5 A 7 /b.
Applying Lemma II on A 2 → A 4 A 6 /a, we get
X → A 6 /A 6 X are not in GNF. Replacing A 6 → bA 7 A 4 /bA 4 in the production, the productions X → bA 7 A 4 /bA 7 A 4 X/bA 4 /bA 4 X are in GNF.
Applying Lemma II on A 3 → A 5 A 7 /b, we get
Y → A 7 /A 7 Y are not in GNF. Replacing A 7 → aA 6 A 5 /aA 5 in the production, the productions Y → aA 6 A 5 /aA 6 A 5 Y/aA 5 /aA 5 Y are in GNF.
A 1 → A 2 is not in GNF. Replacing A 2 → a/aX in the production, we get A 1 → a/aX, which is in GNF.
The grammar converted into GNF is
-
Convert the following grammar to GNF.
- S → aSa/aSb/∈
- S → aSB/aSbS/∈
[WBUT 2010]
Solution: (It can be done by Lemma I and II, but it is done here in a simpler way.)
-
S → aSa/aSb/∈
Introduce two productions C a → a and C b → b, and the modified productions are
-
S → aSB/aSbS/∈
Introduce a production C b → b, and the modified productions are
-
Convert the following grammar into GNF.
Solution: In the grammar, the production A 3 → A 1 A 2 is not in the form A i → A j V where i ≤ j.
Using Lemma 1, replace A 1 → A 2 A 3 in the production A 3 → A 1 A 2 . The rule becomes
A 3 → A 2 A 3 A 2 /a
Still it is not in the form A i → A j V, where i ≤ j. Replacing A 2 → A 3 A 1 /b in A 3 → A 2 A 3 A 2 /a, we get A 3 → A 3 A 1 A 3 A 2 /bA 3 A 2 /a.
Now, the modified grammar is
The production A 3 → bA 3 A 2 /a is in the format A → β i , and the production A 3 → A 3 A 1 A 3 A 2 is in the format of A → Aα j . So, we can introduce a new non-terminal B 3 , and the modified A 3 production is (according to Lemma II)
And the B 3 productions will be
All A 3 productions are in GNF. Replacing A 3 by all A 3 productions in A 2, we get the following productions.
Now, all A 2 productions are in GNF. Replacing A 2 by all A 2 productions in A 2, we get the following productions.
Now, all A 1 productions are in GNF. Replacing A 1 by all A 1 productions in two B 3 productions, we get the following productions.
Now, the grammar becomes
-
Remove the left recursion from the given grammar.
Solution: The grammar has indirect left recursion. To remove the left recursion, rename A as A 1 and B as A 2 . The modified production rules become
For i = 1 and j = 1, there is no production in the form A 1 → A 1 α.
For i = 2 and j = 1, there is a production in the form A 2 → A 1 α. The production is A 2 → A 1 d. According to the algorithm for removal of indirect left recursion, the production becomes
A 2 → A 2 ad/bd
Now, the A 2 production is A 2 → A 2 c/A 2 ad/bd/e
A 2 → A 2 c has immediate left recursion. After removing the left recursion, the production becomes
A 2 → A 2 ad has immediate left recursion. After removing the left recursion, the production becomes
The actual non-left recursive grammar is
-
Construct the equivalent finite automata from the following regular grammar.
Solution: In the grammar, there are two non-terminals S and A. So, in the finite automata, there are three states. For the production S → aS, the transitional diagram is
For the production S → bA, the transitional diagram is
For the production S → b, the transitional diagram is
For the ‘S’ production, the complete transitional diagram is
For the ‘A’ production, the transitional diagram is
The complete transitional diagram for the previous grammar is
-
Construct the equivalent finite automata from the following regular grammar.
Solution: The grammar accepts null string. So, A is the final state. For the production rule A → ∈, the transitional diagram is
For the production S → Aa, the transitional diagram is
For the production A → Sb/Ab, the transitional diagram is
The complete transitional diagram is
-
Using this lemma, prove that L = {a i b j | j = i 2 } is not a CFL.
[WBUT 2009 (IT)]
Solution:
Step I: Assume that the language set L is a CFL. Let n be a natural number obtained by using the pumping lemma.
Step II: Let z = a i b 2i . So, | z | = 3i . Let 3i > n. According to the pumping lemma for CFL, we can write z → uvwxy, where | vx | → 1 and | vwx | ≤ n.
Step III: The string z contains ‘a’ and ‘b’, and so v and x will be in any of the following forms.
- Contain only a, i.e., in the form a x
- Contain only b, i.e., in the form b y
- Contain the repetition of ‘a’ and ‘b’, i.e., in the form a x b y
For case (i), if we take k = 2, then uv 2 wx 2 y cannot be in the form of a i b j | j = i 2 ,
For case (ii), if we take k = 2, then uv 2 wx 2 y cannot be in the form of a i b j | j = i 2 ,
For case (iii), if we take k = 0, then uv 2 wx 2 y cannot be in the form of a i b j | j = i 2 ,
For the three cases, we are getting a contradiction, and so L = {a i b j | j = i 2 } is not a CFL.
-
Verify whether the languages generated by the following grammar are finite or not. If finite, find the longest string generated by the grammar.
Solution:
-
The grammar is not in CNF. The grammar converted to CNF is
In the grammar, there are four non-terminals. For this reason, in the directed graph for the grammar, there are four nodes. The directed graph is
The graph contains loop. So, the language generated by the CFG is infinite.
-
The grammar is not in CNF. To make the grammar in CNF, the productions are as follows
In the grammar, there are six non-terminals. For this reason, in the directed graph for the grammar, there are six nodes.
The directed graph for the grammar is
The directed graph does not contain any loop. So, the grammar is finite. The length of the string generated by the grammar is 6.
-
Multiple Choice Questions
-
Context-free language is _________ language.
- Type 0
- Type 1
- Type 2
- Type 3
-
Parsing a string from a given grammar means
- Finding a derivation
- Finding a leftmost derivation
- Finding a rightmost derivation
- Finding a derivation tree.
-
A grammar is called ambiguous if
- It generates more than one string
- It generates both leftmost and rightmost derivation for a given string
- It generates more than one parse tree for a given string
- It fulfills both (b) and (c)
-
Which is not true for ambiguous grammar?
- Ambiguity creates problem in generating a language from a given grammar
- All ambiguity can be removed.
- Inherent ambiguity cannot be removed
- Some ambiguity can be removed by hand
-
Non-generating symbols are those symbols which
- Do not generate any string of non-terminals
- Do not generate any null string
- Do not generate any string of terminal and non-terminals
- Go not generate any string of terminals
-
Useless symbols in CFG are
- Non-generating symbols and non-reachable symbols
- Null alphabets and null string
- Non-terminal symbols
- All of these
-
Which of the following is a unit production?
- (String of NT) → (String of NT)
- (Single NT) → (String of NT)
- (Single NT) → (Single NT)
- (String of NT) → (Single NT)
-
Which is true for the following CFG?
- Null production can be removed
- Null production cannot be removed
- As A does not produce null, null cannot be removed
- Both (b) and (c)
-
Which of the following production is in CNF? (more specific)
- (NT) → (String of NT)
- (NT) → (String of terminal and non-terminal)
- (NT) → (String of terminal)
- (NT) → (String of exactly two NT)
-
Which of the following production is in GNF? (more specific)
- (NT) → (Single T)(String of NT)
- (NT) → (Single NT)(String of T)
- (NT) → (String of terminal and non-terminal)
- (NT) → (String of NT)
-
Which of the following is common in both CNF and GNF?
- (NT) → (Single T)(String of NT)
- (NT) → (String of exactly two NT)
- (NT) → (String of NT)
- (NT) → (Single T)
-
Context-free language is not closed under
- Union
- Concatenation
- Complementation
- Star closure
Answers:
- c
- a
- c
- b
- d
- a
- c
- b
- d
- a
- d
- c
GATE Questions
-
The intersection of CFL and RE is always
- CFL
- RE
- CSL
- CFL or CSL
-
Which of the following is in GNF?
- A → BC
- A → a
- A → Ba
- A → aaB
-
Consider an ambiguous grammar G and its disambiguated version D. Let the language recognized by the two grammar be denoted by L(G) and L(D), respectively.
Which of the following is true?
- L(D) ⊂ L(G)
- L(D) ⊃ L(G)
- L(D) = L(G)
- L(D) is empty
-
Consider a CFG with the following productions
S is the start symbol, A and B are non-terminals, and 0 and 1 are the terminals. The language generated by the grammar is
- {0 n 10 2n | n ≥ 1}
- {0 i 10 j 10 k | i j, k ≥ 0} ∪ {0 n 10 2n | n ≥ 1 }
- {0 i 10 j | i, j ≥ 0 } ∪ {0 n 10 2n | n ≥ 1 }
- The set of all strings over {0, 1} containing at least two 0’s
-
If G is a CFG and w is a string of length l in L(G), how long is a derivation of w in G, if G is a Chomsky normal form?
- 2l
- 2l + 1
- 2l − 1
- l
-
Context-free languages are
- closed under union
- closed under complementation
- closed under intersection
- closed under Kleene closure
-
Which of the following features cannot be captured by context-free grammar?
- Syntax of if-then-else statements
- Syntax of recursive procedures
- Whether a variable has been declared before use
- Variable names of arbitrary length
-
Let Σ = {0, 1}, L = Σ* and R = {0 n 1 n such that n > 0 }, then the language L ∪ R and R are respectively.
- regular, regular
- not regular, regular
- regular, not regular
- not regular, not regular
-
The grammar whose productions are
is ambiguous because
-
the sentence
if a then if b then c : = d -
the leftmost and rightmost derivations of the sentence
if a then if b then c: = d
give rise to two different parse trees -
the sentence
if a then if b then c: = d else c: = f
has more than two parse trees -
the sentence
if a then if b then c: = d else c: = f
has two parse trees
-
the sentence
-
If L 1 is a context-free language and L 2 is a regular language, which of the following is/are false?
- L 1 − L 2 is not context free
- L 1 ∩ L 2 is context free
- ~L 1 is context free
- ~L 2 is regular
-
A grammar that is both left and right recursive for a non-terminal, is
- Ambiguous
- Unambiguous
- Information is not sufficient to decide whether it is ambiguous or unambiguous.
- None of the above
-
Let L denote the language generated by the grammar S → 0S0/00. Which of the following is true?
- L = 0 +
- L is regular but not 0+
- L is context free but not regular
- L is not context free
-
Consider the following two statements:
Which of the following statements is correct?
- Only S 1 is correct
- Only S 2 is correct
- Both S 1 and S 2 are correct
- None of S 1 and S 2 is correct
-
Consider the following languages:
- Only L 1 and L 2
- only L 2 , L 3, and L 4
- Only L 3 and L 4
- Only L 3
-
Which of the following grammar rules violate the requirements of an operator grammar? P, Q, and R are non-terminals, and r, s, and t are terminals.
-
P → Q R
-
P → Q s R
-
P → ∈
-
P → Q t R r
-
(i) only
-
(i) and (iii) only
-
(ii) and (iii) only
-
(iii) and (iv) only
-
-
Consider the languages:
Which one of the following statements is false?
- L 1 ∩ L 2 is a context-free language
- L 1 ∪ L 2 is a context-free language
- L 1 and L 2 are context-free languages
- L 1 ∩ L 2 is a context-sensitive language
-
Consider the following statements about the CFG
G = {S → SS,S → ab,S → ba,S → ∈}
- G is ambiguous
- G produces all strings with equal number of a’s and b’s
- G can be accepted by a deterministic PDA.
Which of the following combination expresses all the true statements about G?
- (i) only
- (i) and (iii) only
- (ii) and (iii) only
- (i), (ii), and (iii)
-
Consider the grammar
Which of the following strings is generated by the grammar?
- aaaabb
- aabbbb
- aabbab
- abbbba
-
For the correct answer of the previous question, how many derivation trees are there?
- 1
- 2
- 3
- 4
-
Which of the following statements are true?
-
Every left-recursive grammar can be converted to a right-recursive grammar and vice versa.
-
All ∈ productions can be removed from any context-free grammar by suitable transformations.
-
The language generated by a CFG, all of whose productions are of the form X → w or X → wY (where w is a string of terminals and Y is a non-terminal), is always regular.
-
The derivation trees of strings generated by a CFG in CNF are always binary trees.
-
(i), (ii), (iii), and (iv)
-
(ii), (iii), and (iv) only
-
(i), (iii) and (iv) only
-
(i), (ii) and (iv) only
-
-
Match the following:
- E−P, F−R, G−Q, H−S
- E−R, F−P, G−S, H−Q
- E−R, F−P, G−Q, H−S
- E−P, F−R, G−S, H−Q
-
Which one of the following statements is false?
- There exist context-free languages such that all the CFGs generating them are ambiguous.
- An unambiguous CFG always has a unique parse tree for each string of the language generated by it.
- Both deterministic and non-deterministic push down automata always accept the same set of languages.
- A finite set of string from one alphabet is always a regular language.
Answers:
- a
- b
- a
- c
- c
- a, d
- b
- d
- d
- c
- c
- b
- a
- d
- b
- a
- d
- c
- b
- c
- c
- a
Hints:
5. See the example of conversion of E → E+E/E*E/id. The derivation for id + id*id is E → FE → FGE → ECGE → ECEDE → idCEDE → id + EDE → id + idDE → id + id*E → id + id*id (total 9 steps).
8. Σ* is regular, and so it is a CFL. 0n1n is not regular but context free. The union of two context-free languages is context free.
12. The string is even number of ‘0’. The grammar can be S → 00S/00 which is regular.
15. Operator grammar is a CFG with no ∈ production where no two consecutive symbols on the right sides productions are variables.
17. Consider L = abab. It can be generated by
-
-
S → SS → abab ---
Thus, it is ambiguous. II and III are true.
18. S → aB → aaBB → aabB → aabbS → aabbaB → aabbab
19. Another derivation is S → aB → aaBB → aaBb → aabSb → aabbAb → aabbab
20. The language set which contains ∈ may contain a production S → ∈. This cannot be removed. Thus, II is false.
21. H is matched with S. Let there be a pseudocode
Int i, j; //Declaration
i = i + j; //used
ijCij (Before C is declaration and after C is use)
If the code is
Int i, j; //Declaration
k = i + j; //used
ijCkij (Before C is declaration and after C is use)
k is used but not declared.
Fill in the Blanks
-
According to the Chomsky hierarchy, CFG is type _____________ grammar.
-
The grammar where production rules are in the format of
{A string consists of at least one non-terminal} → {A string of terminals and/or non-terminals} is _____________ grammar in particular.
-
The grammar S → aSb/bSb/C produces the string _____________.
-
Finding a derivation for a string from a given grammar is called _____________.
-
The tree representation of deriving a context-free language from a given context grammar is called _____________.
-
A parse tree construction is only possible for _____________ grammar.
-
The root of the parse tree of a given context-free language is represented by the _____________ of the corresponding CFG.
-
A CFG G is said to be _____________ if there exists some w ∈ L(G) that has at least two distinct parse trees.
-
A CFL L is said to be _____________ if all its grammars are ambiguous.
-
The CFG where a non-terminal ‘A’ as a leftmost symbol appears alternatively at the time of derivation either immediately or through some other non-terminal definitions is called _____________ grammar.
-
To avoid the problem of backtracking, we need to perform _____________.
-
In a CFG, the symbols which do not produce any terminal string is called _____________.
-
In a CFG, the symbols which cannot be reached at any time starting from the start symbol are called _____________.
-
In a CFG, non-generating symbols and non-reachable symbols are both called _____________ symbol.
-
In a CFG, the production in the form non-terminal → single non-terminal is called _____________.
-
In a CFG, a production in the form NT → ∈ is called _____________.
-
Normalizing a CFG should not hamper the _____________ power of the grammar.
-
A CFG where all the productions of the grammar are in the form
Non-terminal → string of exactly two non-terminals
Non-terminal → single terminal
is called _____________ normal form.
-
A CFG where all the productions of the grammar are in the form
Non-terminal → (single terminal)(string of non-terminals)
Non-terminal → single terminal
is called _____________ normal form.
-
Context-free languages are not closed under _____________ and _____________.
-
______________is used to prove that certain sets are not context free.
-
a n b n c n , where n ≥ 1, is not _____________ language but _____________ language.
-
If the length of the longest path of the directed graph generated from a CFG is n, then the longest string generated by the grammar is _____________.
-
A language L generated from a given CFG is finite if there are no _____________ in the directed graph generated from the production rules of the given CFG.
Answers:
- Two
- context-free
- WCW R | W∈ (a, b)*
- parsing
- parse tree
- context-free
- start symbol
- ambiguous
- inherently ambiguous
- left recursive
- left factoring
- non-generating symbols
- non-reachable symbols
- useless
- unit production
- null production
- language generating
- Chomsky
- Greibach
- intersection, complementation
- Pumping lemma for CFL
- context free, context sensitive
- 2 n
- cycles
Exercise
-
Construct a CFG for the following.
- a n a m , where n > 0 and m = n + 1
- a n ba m , where m, n > 0
- a n b n c m , where n > 0 and m = n + 1
- L = (011 + 1)*(01)*
- L = {Set of all integers}
-
- Construct the string 0110001 from the grammar
 By using 1. Leftmost derivation 2. Rightmost derivation-
Construct the string baaabbba from the grammar
By using
- Leftmost derivation
- Rightmost derivation
-
- Find the parse tree for generating the string abaabaa from the given grammar.
-
Find the parse tree for generating the string aabbaa from the given grammar.
-
Show that the following grammars are ambiguous.
-
S → abSb/aAb/a
A → bS/aAAb
-
E → E+E/E*E/id
-
S → aB/bA
A → aS/bAA/a
B → bS/aBB/b
-
-
Remove the useless productions from the following grammar
-
S → AB/a
A → b
-
S → AB/AC
A → 0A1/1A0/0
B → 11A/00B/AB
C → 01C0/0D1
D → 1D/0C
-
-
Remove the unit production from the following grammar:
-
S → SaA
A → aB/B/b
B → bC/C/a
C → ab
-
S → Aa/B
B → A/bb
A → a/bc/B
-
-
Remove the null production from the following grammar
-
S → aAB
A → Bb
B → ∈
-
S → aA
A → bB
B → b
B → ∈
-
-
Simplify the following CFG.
-
- Convert the following left linear grammar into right linear grammar.
-
Convert the following right linear grammar into left linear grammar.
-
Convert the following linear grammar into regular grammar.
-
Convert the following grammar into CNF.
-
S → AB
A → aA/aB → ab/bB/b
-
S → aSa/SSa/a
-
-
Convert the following grammar into GNF.
-
S → Abb/a
A → aaA/B
B → bAb
-
S → aSb/bSa/a/b
-
-
Construct a DFA equivalent to the regular grammar.
-
S → aS/bA/b
A → aA/bS/a
-
S → bA/aB
A → bA/aS/a
B → aB/bS/b
-
-
Prove that L = a n b n c 2n is not context free by using the pumping lemma for CFL.
-
Verify whether the languages generated by the following grammar are finite or not.
-
S → aA
A → BC
B → SC/b
C → B/a
-
S → AB
A → C/a
B → AC/b
-
-
Remove the left recursion from the following grammar and then perform left factoring.
-
Generate the string id + id*id from the grammar
where the precedence of operator is given as follows.
Are you getting any ambiguity in the grammar?