Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

UNIT 3

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 –

Grammar Grammar Language Automaton


Type Accepted Accepted

Type 0 Unrestricted Recursively Turing Machine


grammar enumerable
language

Type 1 Context-sensitive Context-sensitive Linear-bounded


grammar language automaton

Type 2 Context-free Context-free Pushdown


grammar language automaton

Type 3 Regular grammar Regular language Finite state


automaton

Take a look at the following illustration. It shows the scope of each type of grammar −

Page | 1
Type - 3 Grammar (regular languages)

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 (context-free languages)

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→Xa
X→a
X → aX
X → abc
X→ε

Page | 2
Type - 1 Grammar (context-sensitive languages)

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 (recursively enumerable languages)

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

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.

Formal Definition of a Derivation Tree:

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:

1. The root of the derivation tree is S.


2. Each and every leaf in the tree has a label from T ∪{ε}.
3. Each and every interior vertex (a vertex which is not a leaf) has a label from V.
4. If a vertex has label A∈ V, and its children are labeled (from left to right) a1, a2, .. ..
.. .., an, then P must contain a production of the form
A → a1, a2, .. .. .. .., an
5. A leaf labeled ε has no siblings, that is, a vertex with a child labeled ε can have no
other children.

Application of Derivation Tree:

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:

There are two different approaches to draw a 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

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:

Sentential Form and Partial Derivation Tree

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

Check whether the grammar G with production rules −


X → X+X | X*X |X| a
is ambiguous or not.

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

Context free grammar is a formal grammar which is used to generate all possible strings in
a given formal language.

Context free grammar G can be defined by four tuples as:

G= (V, T, P, S)

Where,

G describes the grammar

T describes a finite set of terminal symbols.

V describes a finite set of non-terminal symbols

P describes a set of production rules

S is the start symbol.

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.

Converting right linear grammar to Finite Automata is simple.


Follow the steps:

1. Start from the first production


2. And then for every
very left alphabet go to SYMBOL followed by it
3. Start State: It will be the first production's state
4. Final State: Take those states which end up with input alphabets. eg. State A and C
are below CFG

Here we are giving one right linear grammar

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

As you can see for state A, transitoin on 'a' is going on state B


And on 'b' it is going on state A itself...
This method will apply to all the states.

Conversion of automata machine to grammar


Converting finite automata to right lienar grammar is very simple
See below steps and example followed by it, we will understand the process.

1. Repeat the process for every state


2. Begin the process fromm start state
3. Write the production as the output followed by the state on which the transition is
going
4. And at the last add ε because that's is required to end the derivation

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 ε

So we will applt the above procedure


Pick start state and output is on symbol 'a' we are going on state B
So we will write as :

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/ε

Chomsky's Normal Form (CNF)


CNF stands for Chomsky normal form. A CFG(context free grammar) is in CNF(Chomsky
normal form) if all production rules satisfy one of the following conditions:

o Start symbol generating ε. For example, A → ε.


o A non-terminal generating two non-terminals. For example, S → AB.
o A non-terminal generating a terminal. For example, S → a.

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.

Steps for converting CFG into 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

Where S1 is the new start symbol.

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

Greibach Normal Form (GNF)


GNF stands for Greibach normal form. A CFG(context free grammar) is in GNF(Greibach
normal form) if all the production rules satisfy one of the following conditions:

o A start symbol generating ε. For example, S → ε.


o A non-terminal generating a terminal. For example, A → a.
o A non-terminal generating a terminal which is followed by any number of non-
terminals. For example, S → aASB.

For example:

 G1 = {S → aAB | aB, A → aA| a, B → bB | b}


 G2 = {S → aAB | aB, A → aA | ε, B → bB | ε}

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.

Steps for converting CFG into GNF

Step 1: Convert the grammar into CNF.

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

Step 2: If the grammar exists left recursion, eliminate it.

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

You might also like