Professional Documents
Culture Documents
Unit 3: Types of Grammar
Unit 3: Types of Grammar
Types of grammar
According to Noam Chomosky, there are four types of grammars − Type 0, Type 1, Type
2, and Type 3. The following table shows how they differ from each other –
Take a look at the following illustration. It shows the scope of each type of grammar −
Page | 1
Type - 3 Grammar (regular languages)
Example
X→ε
X → a | aY
Y→b
Example
S→Xa
X→a
X → aX
X → abc
X→ε
Page | 2
Type - 1 Grammar (context-sensitive languages)
Example
AB → AbBc
A → bcA
B→b
Example
S → ACaB
Bc → acB
CB → DB
aD → Db
Page | 3
Derivation trees
A ‘derivation tree’ is an ordered tree in which the nodes are labeled with the left sides of
productions and the children of a node represent its corresponding right sides. A
derivation tree is the tree representation of the CFG (Context Free Grammar). It consists of
three types of nodes;
Root Nodes: It is a non terminal variable in the production of the grammar that
exists at the left side of the production rules or root of the tree is represented by
the start symbol of the Context free grammar (CFG) production rules.
Intermediate Nodes: All variables accepts root node considered as the
intermediate nodes. Except the start symbol of non-terminals in the production
rules becomes the part of intermediate node.
Leaves Nodes: Nodes having no childes considered as leaves. Each leaf node is
represented by a terminal.
Note: Derivation tree is also called Parse Tree, Production Tree, Generation Tree as well.
Let G = (V, T, S, P) be a CFG. An ordered tree is a derivation tree for G if and only if it has
the following properties:
Derivation trees play a very important role in parsing theory and in the proof of a strong
version of the pumping lemma for the context-free languages known as Ogden’s lemma.
Page | 4
Approaches of derivation tree:
1. Top-down Approach:
Starts with the starting symbol S
Goes down to tree leaves using productions
2. Bottom-up Approach:
Starts from tree leaves i.e. terminals of the strings
Proceeds upward to the root which is the starting symbol S
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:
S → AB
A → aaA | ε
B → Bb | ε
If a partial derivation tree contains the root S, it is called a sentential form. The above sub-
tree is also in sentential form.
Page | 5
Ambiguity in grammar
If a context free grammar G has more than one derivation tree for some string w ∈ L(G), it
is called an ambiguous grammar. There exist multiple right-most or left-most derivations
for some string generated from that grammar.
Problem
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 −
Derivation 2 –
X → X*X → X+X*X → a+ X*X → a+a*X → a+a*a
Parse tree 2 −
Page | 6
Since there are two parse trees for a single string "a+a*a", the grammar G is ambiguous.
Context free grammar is a formal grammar which is used to generate all possible strings in
a given formal language.
G= (V, T, P, S)
Where,
Page | 7
Conversion of grammar to automata machine
In CFG, the start symbol is used to derive the string. You can derive the string by
repeatedly replacing a non-terminal
terminal by the right hand side of the production, until all non-
non
terminal have been replaced by terminal symbols.
A -> aB/bA/b
B ->
> aC/bB
C ->
> aA/bC/a
Now see the output and you will understand what just happened.
Page | 8
Explanation
Note:
We have added ε because either you could continue the derivation or would like to stop it. So
to stop the derivation we have written ε
A -> aB
And then we will pick state B and then we will go for each output.
so we will get the below production.
B -> aB/bB/ε
Page | 9
So final we got right linear grammar as:
A -> aB
B -> aB/bB/ε
For example:
G1 = {S → AB, S → c, A → a, B → b}
G2 = {S → aA, A → a, B → c}
The production rules of Grammar G1 satisfy the rules specified for CNF, so the grammar
G1 is in CNF. However, the production rule of Grammar G2 does not satisfy the rules
specified for CNF as S → aZ contains terminal followed by non-terminal. So the grammar
G2 is not in CNF.
Step 1: Eliminate start symbol from the RHS. If the start symbol T is at the right-hand side
of any production, create a new production as:
S1 → S
Step 2: In the grammar, remove the null, unit and useless productions. You can refer to
the Simplification of CFG.
Step 3: Eliminate terminals from the RHS of the production if they exist with other non-
terminals or terminals. For example, production S → aA can be decomposed as:
Page | 10
S → RA
R→a
Step 4: Eliminate RHS with more than two non-terminals. For example, S → ASB can be
decomposed as:
S → RS
R → AS
For example:
The production rules of Grammar G1 satisfy the rules specified for GNF, so the grammar
G1 is in GNF. However, the production rule of Grammar G2 does not satisfy the rules
specified for GNF as A → ε and B → ε contains ε(only start symbol can generate ε). So the
grammar G2 is not in GNF.
If the given grammar is not in CNF, convert it into CNF. You can refer the following topic
to convert the CFG into CNF: Chomsky normal form
Page | 11
If the context free grammar contains left recursion, eliminate it. You can refer the following
topic to eliminate left recursion: Left Recursion
Step 3: In the grammar, convert the given production rule into GNF form.
If any production rule in the grammar is not in GNF form, convert it.
Page | 12
We hope you find these notes useful.
useful
Contribute study material and get list
listed on Hall of Fame and win
cash prize every week.
50 Points : Contributing Per Video.
50 Points : Contributing Per Page Notes Hand Written/ Typed.
50* Points : Contributing Project(If Project is Large then more Points
will be issued).
50 Points : Contributing Other material
NOTE:- Points will be issued only if the material provided by you is approved.
approved
If you have any queries or you want to submit your study notes
please write us at
gurujikenotesofficial@gmail.com
or
admin@gurujikenotes.com
And Fill this form
FORM