Sie sind auf Seite 1von 12

CHAPTER 5

REGULAR GRAMMER

Regular Grammar
The concept of regular grammar is already introduced in chapter 5. Here we will study the
equivalence and conversion of regular grammar with respect to Finite Automata.

Regular grammar is a one of the types of Chomsky hierarchy which is used to represent regular
language. There are certain restrictions on productions of regular grammar, such as:
The left hand side of production should contain exactly one non terminal.
The right hand side of production can contain at most one non terminal and that
can appear as either leftmost or rightmost symbol.

Based on the position of non terminal on right hand side of the production, the regular grammar
is classified as
1. Left-Linear Grammar (GL)
2. Right-Linear Grammar (GR).

Left-Linear Grammar (GL)

If the non terminal is a leftmost symbol on the right hand side of production then the
grammar is called as Left-Linear Grammar.

The production S is allowed only for start symbol S and does not appear on right hand
side of any production.

For example, consider the following grammar:


G={(S,A,B),(0,1),P,S}
where, Productions P are:
SB0 | A1
BA1
AA0 | 1

Right-Linear Grammar (GR)


If the non terminal is a rightmost symbol on the right hand side of production then the
grammar is called as Right-Linear Grammar.

The production S is allowed only for start symbol S and does not appear on right hand
side of any production.

For example, consider the following grammar:


G={(S,A,B),(0,1),P,S}
where, Productions P are:
S0B | 1A
B1A
A0A | 1
Converting Right Linear Grammar to Left Linear Grammar
Following steps are performed during conversion:
1. Draw transition graph for given right linear grammar.
In this graph:
Vertices are labeled with Non-terminals or and transitions are labeled with
Terminals or
Initial state will be represented by S and final state will be represented by [].
2. Exchange position of initial state and final state.
3. Reverse directions of all transitions
4. Rewrite the grammar in left linear fashion.

Example 1 Convert given right linear grammar to left linear grammar


SbB
B bC
BaB
Ca
Bb
Solution:
Step1 : Draw Transition Graph
Represent vertices with all Non-terminals with s as initial state and [] as final state.
For production SbB, add transition from S to B with label b.
Similarly, for B bC, add transition from S to C with label b.
for BaB, add self transition B with label a.
for Ca, add transition from C to [] with label a.
and for Bb, add transition from B to [] with label b.

Thus the transition graph will be:

Step 2: Exchange position of initial state and final state.


Step 3 :Reverse directions of all transitions

Step 4: Rewrite the grammar in left linear fashion.


From figure 5.3 we can rewrite the grammar in left linear fashion as:
SCa
SBb
CBb
BBa
Bb

Example 2: Convert given right linear grammar to left linear grammar


S0A | 1B
A0C|1A|0
B1B|1A|1
C0|0A
Solution:
Step1 : Draw Transition Graph
Similar to previous example draw the transition diagram as shown in below figure

Step 2: Exchange position of initial state and final state.


Step 3 :Reverse directions of all transitions

Step 4: Rewrite the grammar in left linear fashion.


From figure 5.6 we can rewrite the grammar in left linear fashion as:
SB1|A0|C0
AA1|B1|C0|0
BB1|1
CA0

Converting Left Linear Grammar to Right Linear Grammar


In order to convert Left Linear Grammar to Right Linear Grammar, the same method used in
previous section is applied.
Following steps are performed during conversion:
1. Draw transition graph for given left linear grammar.
In this graph:
Vertices are labeled with Non-terminals or and transitions are labeled with
Terminals or
Initial state will be represented by S and final state will be represented by [].
2. Exchange position of initial state and final state.
3. Reverse directions of all transitions
4. Rewrite the grammar in right linear fashion.
Example 1: Convert following grammar to right linear grammar
SB1|A0|C0
AA1|B1|C0|0
BB1|1
CA0

Solution:
Step1 : Draw Transition Graph
Draw the transition diagram for the given grammar as shown below.
Step 2: Exchange position of initial state and final state.

Step 3 :Reverse directions of all transitions

Step 4: Rewrite the grammar in left linear fashion.


From figure 5.9 we can rewrite the grammar in right linear fashion as:
SB1|A0|C0
AA1|B1|C0|0
BB1|1
CA0
Equivalence of Regular Grammar and Finite Automata
We have already discussed Regular grammar in previous section. Now we will see the
equivalence between regular grammar and finite automata.
Regular grammars can generate regular sets which can be accepted by finite automata. Similarly
left and right linear grammar can be represented using transition graphs which are nothing but
finite automata.

Right Linear Grammar and Finite Automata


Suppose G=(V,T,P,S) is a right linear grammar, we can construct NFA with -moves, as
M=(Q,T,,[S],{[]}) that simulates derivation in G.
Q consist of symbols [] such that is S or a suffix of some right hand side of production in P.
is defined as
if A is a non terminal then (A,) = where A is a production. That is show transition
with from A to its right hand side of production.
if a is a terminal and is in (T*UT*V) then ([a],a)=a
This NFA with -moves can be converted to DFA by direct method.

Example
Find equivalent DFA accepting the regular language defined by RLG given as
S0A|1B
A0C|1A|0
B1B|1A|1
C0|0A
Solution-
Let [S] is initial state and [] is a final state.
As S0A and S1B, add -transitions from state S to state 0A and from state S to 1B.
Similarly we add remaining -transitions like:
from state A to state [0C]
from state A to state [1A]
from state B to state [1B]
from state B to state [1A]
from state C to state [0A]
from state A to state [0]
from state B to state [1]
from state C to state [0]
[0A] is in the form of [a] so add transition from [0A] to A on symbol 0. Similarly we add
remaining transitions like
from state [0C] to state [C] on symbol 0
from state [1A] to state [A] on symbol 1
from state [1B] to state [B] on symbol 1
from state [0A] to state [A] on symbol 0
Thus the transition diagram for above NFA is

Figure NFA with -moves

The DFA can be obtained from above NFA -moves as follows:


New Names x y=-moves of x (y,0) (y,1)
A S 0A, 1B, S A B
B A A, 0C, 0, 1A C A
C C C, 0, 0A, A --
D A A, 0C, 0, 1A, C A
E B B, 1, 1A, 1B -- , A, B
F AB A, B, , 0C, 0, 1A, 1B C , A, B
start with start symbol S and find -closure and then the transitions for 0 and 1. Repeat same
step for every new step. The DFA is

Figure- DFA
The final DFA is
Figure- DFA with relabeling

The transition table is given by


0 1
Q
A B D
B C B
*C E --
D -- F
*E C B
*F C F

Example 2
Construct DFA for given RLG
SbB
BbC|aB|b
Ca
Solution-
The NFA with -moves is as follows
Figure- NFA with -moves

for every production add -transition and by applying rule obtain above NFA with -moves. The
DFA can be constructed as follows:
New Name x y=-closure of x (y,a) (y,b)
A S S, bB -- B
B B B, bC, aB, b B C,
C C C, , a --
D -- --

Figure- Final DFA Figure- DFA with relablling

The transition table is given as



a b
Q
A -- B
B B C
*C D --
*D -- --
Left Linear Grammar and Finite Automata
Suppose G=(V,T,P,S) is a right linear grammar, then construct the right linear grammar G by
adding P which consists of productions of G with right side reversed. From this right linear
grammar, construct the NFA with -moves. Then traverse all transitions and interchange the
positions of initial states and final state to get NFA with -moves equivalent to original grammar.
Convert this NFA to DFA by usual method discussed earlier.

Example
Construct DFA from given Left Linear Grammar
SS10|0
Solution-
Reverse the right hand side of all productions to obtain right linear grammar as
S01S|0
Construct NFA with -moves from above right linear grammar

Now reverse all transitions and interchange positions of S and , we get

convert this NFA to DFA


New Name x y=-closure of x (y,0) (y,1)
A S S 0 --
B 0 0, -- 1S
C 1S 1S 01S --
D 01S 01S, -- 1S

0 1
Q

A B --

*B -- C

C D --

*D -- C

Figure- Final DFA


State B and D are equivalent so it can be reduced and final DFA is


0 1
Q

A B --

*B -- C

C B --

Example
Construct DFA from given Left Linear Grammar
SCa|Bb
CBa
BBa|b
Solution-
The Right Linear Grammar from grammar by reversing right side is
SaC|bB
CaB
BaB|b

NFA with -moves


reverse transitions and interchange S and

NFA with -moves to DFA


New Name x y=-closure of x (y,a) (y,b)
A S S -- b
B b b, B aB bB
C aB aB, C, B aB, aC --
D aB, aC, bB aB, aC, , B,C aB, aC --

Das könnte Ihnen auch gefallen