toc2
toc2
CFG.
Representation Technique
Root vertex − Must be labeled by the start symbol.
Vertex − Labeled by a non-terminal symbol.
Leaves − Labeled by a terminal symbol or ε.
If S → x x …… x is a production rule in a CFG, then the parse tree / derivation tree will
1 2 n
be as follows −
The derivation or the yield of a parse tree is the final string obtained by concatenating
the labels of the leaves of the tree from left to right, ignoring the Nulls. However, if all
the leaves are Null, derivation is Null.
Example
Let a CFG {N,T,P,S} be
N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε
One derivation from the above CFG is “abaabb”
S → SS → aSbS → abS → abaSb → abaaSbb → abaabb
Sentential Form and Partial Derivation Tree
A partial derivation tree is a sub-tree of a derivation tree/parse tree such that either all
of its children are in the sub-tree or none of them are in the sub-tree.
Example
If in any CFG the productions are −
S → AB, A → aaA | ε, B → Bb| ε
the partial derivation tree can be the following −
If a partial derivation tree contains the root S, it is called a sentential form. The above
sub-tree is also in sentential form.
Solution
Let’s find out the derivation tree for the string "a+a*a". It has two leftmost derivations.
Derivation 1 − X → X+X → a +X → a+ X*X → a+a*X → a+a*a
Parse tree 1 −
Union
Concatenation
Kleene Star operation
Union
Let L and L be two context free languages. Then L ∪ L is also context free.
1 2 1 2
Example
Union of L and L , L = L ∪ L = { a b } ∪ { c d }
1 2 1 2
n n m m
Concatenation
If L and L are context free languages, then L L is also context free.
1 2 1 2
Example
Union of the languages L and L , L = L L = { a b c d }
1 2 1 2
n n m m
Kleene Star
If L is a context free language, then L* is also context free.
Example
Kleene Star L = { a b }*
1
n n
CFG Simplification
In a CFG, it may happen that all the production rules and symbols are not needed for
the derivation of strings. Besides, there may be some null productions and unit
productions. Elimination of these productions and symbols is called simplification of
CFGs. Simplification essentially comprises of the following steps −
Reduction of CFG
Removal of Unit Productions
Removal of Null Productions
Reduction of CFG
CFGs are reduced in two phases −
Phase 1 − Derivation of an equivalent grammar, G’, from the CFG, G, such that each
variable derives some terminal string.
Derivation Procedure −
Step 1 − Include all symbols, W , that derive some terminal and initialize i=1.
1
Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that each
symbol appears in a sentential form.
Derivation Procedure −
Step 1 − Include the start symbol in Y and initialize i = 1.
1
Step 2 − Include all symbols, Y , that can be derived from Y and include all production
i+1 i
Problem
Solution
Phase 1 −
T = { a, c, e }
W = { A, C, E } from rules A → a, C → c and E → aA
1
W = { A, C, E } U { S } from rule S → AC
2
W = { A, C, E, S } U ∅
3
G’ = { { A, C, E, S }, { a, c, e }, P, {S}}
where P: S → AC, A → a, C → c , E → aA | e
Phase 2 −
Y ={S}
1
Y = { S, A, C } from rule S → AC
2
Y = { S, A, C, a, c }
4
G” = { { A, C, S }, { a, c }, P, {S}}
where P: S → AC, A → a, C → c
Removal Procedure −
Removal Procedure
Take a look at the following illustration. It shows the scope of each type of grammar −
Type - 3 Grammar
Type-3 grammars generate regular languages. Type-3 grammars must have a single
non-terminal on the left-hand side and a right-hand side consisting of a single terminal
or single terminal followed by a single non-terminal.
The productions must be in the form X → a or X → aY
where X, Y ∈ N (Non terminal)
and a ∈ T (Terminal)
The rule S → ε is allowed if S does not appear on the right side of any rule.
Example
X → ε
X → a | aY
Y → b
Type - 2 Grammar
Type-2 grammars generate context-free languages.
The productions must be in the form A → γ
where A ∈ N (Non terminal)
and γ ∈ (T ∪ N)* (String of terminals and non-terminals).
These languages generated by these grammars are be recognized by a non-
deterministic pushdown automaton.
Example
S → X a
X → a
X → aX
X → abc
X → ε
Type - 1 Grammar
Type-1 grammars generate context-sensitive languages. The productions must be in
the form
αAβ→αγβ
where A ∈ N (Non-terminal)
and α, β, γ ∈ (T ∪ N)* (Strings of terminals and non-terminals)
The strings α and β may be empty, but γ must be non-empty.
The rule S → ε is allowed if S does not appear on the right side of any rule. The
languages generated by these grammars are recognized by a linear bounded
automaton.
Example
AB → AbBc
A → bcA
B → b
Type - 0 Grammar
Type-0 grammars generate recursively enumerable languages. The productions have
no restrictions. They are any phase structure grammar including all formal grammars.
They generate the languages that are recognized by a Turing machine.
The productions can be in the form of α → β where α is a string of terminals and
nonterminals with at least one non-terminal and α cannot be null. β is a string of
terminals and non-terminals.
Example
S → ACaB
Bc → acB
CB → DB
aD → Db
A→a
A → BC
S→ε
where A, B, and C are non-terminals and a is terminal.
B …B . Repeat this step for all productions having two or more symbols in the right
2 n
side.
Step 5 − If the right side of any production is in the form A → aB where a is a terminal
and A, B are non-terminal, then the production is replaced by A → XB and X → a.
Repeat this step for every production which is in the form A → aB.
Problem
Solution
(1) Since S appears in R.H.S, we add a new state S and S →S is added to the
0 0
A → B | S, B → b
After removing A→ B, the production set becomes −
S → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
0
A→S|b
B→b
After removing A→ S, the production set becomes −
S → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA
0
A → b |ASA | aB | a | AS | SA, B → b
(4) Now we will find out more than two variables in the R.H.S
Here, S → ASA, S → ASA, A→ ASA violates two Non-terminals in R.H.S.
0
Hence we will apply step 4 and step 5 to get the following final production set which is
in CNF −
S → AX | aB | a | AS | SA
0
S→ AX | aB | a | AS | SA
A → b |AX | aB | a | AS | SA
B→b
X → SA
(5) We have to change the productions S → aB, S→ aB, A→ aB
0
S→ AX | YB | a | AS | SA
A → b A → b |AX | YB | a | AS | SA
B→b
X → SA
Y→a