Professional Documents
Culture Documents
Grammars PDF
Grammars PDF
[
∞
Also L+ = Li .
i=1
1
S ⇒ aSBc
2
⇒ aabcBc
3
⇒ aabBcc
4
⇒ aabbcc, a2 b2 c2 ∈ L(G)
T ∗ , the grammar is called a right linear grammar or type 3 grammar and the
put the restriction that the rules can be of the form A → aB, A → b, where
new nonterminals B1 , . . . , Bk .
S ⇒ aS (rule 1)
⇒ abS (rule 2)
⇒ abbS (rule 2)
⇒ abbaS (rule 1)
⇒ abbaaS (rule 1)
⇒ abbaabS (rule 2)
⇒ abbaab (rule 3)
a S c
a S c
a A c
a B
a B B
a B B b
b b
0
X
−1
−2
X1 . . . X m → Y 1 . . . Y n , 2 ≤ m ≤ n
Introduction to Formal Languages, Automata and Computability – p.31/74
contd..
It is replaced by the following set of context-sensitive
productions:
X1 . . . X m → Z1 X2 . . . X m
Z1 X 2 . . . X m → Z 1 Z2 X 3 . . . X m
..
.
Z1 Z2 . . . Zm−1 Xm → Z1 Z2 . . . Zm Ym+1 . . . Yn
Z1 Z2 . . . Zm Ym+1 . . . Yn → Y1 Z2 . . . Zm Ym+1 . . . Yn
..
.
Y1 Y2 . . . Ym−1 Zm Ym+1 . . . Yn → Y1 Y2 . . . Ym Ym+1 . . . Yn
where Zk , 1 ≤ k ≤ m are new nonterminals.
Introduction to Formal Languages, Automata and Computability – p.32/74
contd..
Each production that is not context-sensitive is to be
replaced by a set of context-sensitive productions as
mentioned above. Application of this set of rules has
the same effect as applying X1 . . . Xm → Y1 . . . Yn .
Hence a new grammar G0 thus obtained is
context-sensitive that is equivalent to G.
Example Let L = {an bm cn dm /n, m ≥ 1}.
The type 1 grammar generating this CSL is given by
G = (N, T, P, S) with N = {S, A, B, X, Y },
T = {a, b, c, d}
S ⇒ aAB ⇒ aaAXB
⇒ aaaXXB
⇒ aaaXXbY d
⇒ aaaXbXY d
⇒ aaabXXY d
⇒ aaabXY cd
⇒ aaabY ccd
⇒ aaabcccd.
S S S S
a S S S S a
a a a a
L1 = {an bn cp /n, p ≥ 1}
L2 = {an bm cm /n, m ≥ 1}.
L1 = {an bn cp /n, p ≥ 1}
L2 = {an bm cm /n, m ≥ 1}.
S4 = S3 S3 + a
= (a4 + 2a3 + a2 + a)2 + a
= a8 + 4a7 + 6a6 + 6a5 + 5a4 + 2a3 + a2 + a.
We can proceed like this using Si = Si−1 Si−1 + a
In Si , upto strings of length i, the coefficient of the
string will give the number of different derivation trees
it can have in G. For example, in S4 coefficient of a4 is
5 and a4 has 5 different derivation trees in G. The co-
efficient of a3 is 2 - the number of different derivation
trees for a3 is 2 in G. Introduction to Formal Languages, Automata and Computability – p.43/74
Simplification
Definition Let G = (N, T, P, S) be a CFG. A variable X in N is said
to be useful if and only if there is at least a string α ∈ L(G) such that
∗ ∗
S ⇒ α1 Xα2 ⇒ α,
α2 ⇒ w2 , w1 , w2 ∈ T ∗ . Thus
∗
i.e., A ⇒ w. Let w = w1 . . . wk
n−1
Y1 , Y2 , . . . , Yk =⇒G
G
and let X1 , X2 , . . . , Xm be those Yj ’s in order such that
Yj ⇒ wj , wj 6= . Clearly k ≥ 1 as w 6= . Hence
∗
A → X1 . . . Xm is a rule in G0 .
Introduction to Formal Languages, Automata and Computability – p.53/74
contd.
We can see that X1 . . . Xm ⇒G w as some Yj derive
∗
∗
A ⇒G X1 . . . Xm ⇒ w.
Conversely if A ⇒G0 w. Then to show that A ⇒G w
∗ ∗
A ⇒0 Y1 . . . Yk ⇒G w. For A ⇒0 Y1 . . . Yk the
∗
G G
corresponding equivalent derivation by G will be
A ⇒0 X1 . . . Xm ⇒G Y1 . . . Yk as some of the Xi0 are
∗
G
nullable. Hence A ⇒G Y1 . . . Yk ⇒G w1 . . . wk = w
∗ ∗
L(G) = L(G0 ).
A → A 1 B1
B1 → A 2 A 3 .
Similarly if A → A1 A2 . . . An ∈ P , it is replaced by
P 0 ={AL → BL CR , AR → BL CR |A → BC ∈ P }
∪ {S 0 → XL YR |S → XY ∈ P }
∪ {S 0 → a|S → a ∈ P }
∪ {AL → a, AR → a|A → a ∈ P, a ∈ T }.
N 0 = {S 0 , SL , SR , AL , AR , BL , BR }
T = {0, 1}
P 0 = {S 0 → AL BR |0, SL → AL BR |0,
SR → AL BR |0, AL → AL BR |0,
AR → AL BR |0, BL → BL AR |1,
BR → BL AR |1}.
Introduction to Formal Languages, Automata and Computability – p.73/74
Greibach Normal Form
Definition Let ∈/ L(G) and G = (N, T, P, S) be a
CFG. G is said to be in Greibach Normal Form
(GNF), if each rule in P rewrites a variable into a
word in T N ∗ i.e., each rule will be of the form
A → aα, a ∈ T , α ∈ N ∗ .