Formal Languages-Course 5.
Formal Languages-Course 5.
Géraud Sénizergues
Bordeaux university
18/05/2020
Master computer-science MINF19, IEI, 2019/20
1 / 51
Formal Languages-Course 5.
contents
1 Context-free grammars : ambiguity
2 Context-free languages :closure properties
3 Context-free grammars :transformations Cleaning context-free grammars A simple transformation
Quadratic normal form
2 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
Context-free grammars : ambiguity
3 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
Definition
A context-free grammar G = h A, N, R, σi is called ambiguous iff there exist derivations trees T
1, T
2, with root σ, such that T
16= T
2and fr (T
1) = fr (T
2) ∈ A
∗.
1- The grammar G is called non-ambiguous iff it is not ambiguous.
2- In words : G is ambiguous iff there exist two different derivation-trees T
1, T
2with the same frontier w ∈ A
∗.
3- Equivalently, G is ambiguous iff one of the following is true : - there are two different leftmost-derivations D
1, D
2that end in the same terminal word w
- there are two different rightmost-derivations D
1, D
2that end in the same terminal word w
- there are two different construction-trees that have the same yield w .
4 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
Example
Let G = hA, N, R, S i with
A = {a, b}, N = {S },
R : S −→ aS , S −→ aSb, S −→ ab.
L = L (G , S ) = {a
nb
m| n ≥ m ≥ 1}.
D
1: S
ℓ−→
RaS
ℓ−→
RaaSb
ℓ−→
Raaabb D
2: S
ℓ−→
RaSb
ℓ−→
RaaSb
ℓ−→
Raaabb
5 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
Example
Let G = hA, N, R, S i with A = {a, b}, N = {S }, R : S −→ aS , S −→ aSb, S −→ ab.
L = L (G , S ) = {a
nb
m| n ≥ m ≥ 1}.
T2
a a a b b
S
a a a a b b
S
S
S
S
S
T1
Figure – Two derivation-trees
6 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
Example
Let H = hA, N
′, R
′, Si with
A = {a, b}, N
′= {S , T }, R
′: S −→ aS , S −→ T .
T −→ aTb, T −→ ab.
L = L (H, S ) = {a
nb
m| n ≥ m ≥ 1}.
This new grammar for the same language L is non-ambiguous.
Let w = a
nb
mwith n ≥ m ≥ 1.
The only leftmost derivation for a
m+rb
mis D(r, m) : S −→
rR′a
rS −→
1R′a
rT −→
m−1R′a
ra
m−1Tb
m−1−→
1R′a
ra
m−1abb
m−1= a
m+rb
m.
7 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
T S
S T
ar am−1 ab bm−1
Figure – The derivation-tree
8 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
Example
Let H = hA, N
′, R
′, Si be the previous grammar. Let us name the rules :
r1 : S −→ aS, r2 : S −→ T . r3 : T −→ aTb, r4 : T −→ ab.
The only construction-tree for a
m+rb
mis : r
1r· r
2· r
3m−1· r
4.
9 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
yd(T) =arambm r1
r1
.. .
r1
r2
r3
r3
.. .
r3
r4
rnodes
m−1nodes
T
Figure – The construction-tree
10 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
Example
H = hA, N, P i whith set of rules :
C −→ CsC | bCe | wC | i
We already considered :
D
1: C
ℓ−→ wC
ℓ−→ wbCe
ℓ−→ wbCsC e
ℓ−→ wbwCsCe
ℓ
−→ wbwisCe
ℓ−→ wbwisi e But there is also :
D
2: C
ℓ−→ wC
ℓ−→ wbCe
ℓ−→ wbwCe
ℓ−→ wbwCsCe
ℓ−→
wbwisCe
ℓ−→ wbwisi e
11 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
The two corresponding derivation-trees are :
C C
C
C
C
b C e b C e
w w
i i
T1 T2
w C s C w C s C
i i
C
Figure – The two derivation-trees
12 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
wbwisie can be seen, according to T
1as : while cond
––begin ––while cond ––––i
––;
––i
––end or, according to T
2as : while cond
––begin ––while cond ––––i
––––;
––––i
––end
13 / 51Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
Example
H = hA, {C }, P, C i whith set of rules : C −→ CsC |bCe |wC |i
Another grammar (for the same language) could be : K = hA, {C , D}, R, C i whith set of rules :
C −→ DsC|D D −→ wD|bCe|i
Intuition : D generates the “ prime” instructions (those which cannot be factored as usv with u, v instructions)
C generates all instructions : c = d
0s d
1· · · s d
n−1where each d
iis a prime instruction.
14 / 51
Formal Languages-Course 5.
Context-free grammars : ambiguity
context-free gramars : ambiguity
K = hA, {C , D}, R, C i whith set of rules :
C −→ DsC | D D −→ wD | bCe | i
The leftmost derivation of wbwisie in this grammar is : C −→
3wbC e , wbC e −→
2wbDsDe −→
3wbwisi e, producing the structure :
while cond ––begin ––while cond ––––i
––;
––i ––end
15 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Context-free languages : closure properties
16 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
Theorem
Let A be a finite alphabet.
1- The set of context-free languages over A
∗is closed under the operations : union, product, star, cross.
2- If Card (A) ≥ 2, then the set of context-free languages over A
∗is not closed under complement ; it is also not closed under
intersection.
We give a constructive proof of point 1. Hence, from c.f. grammars generating languages L
1, L
2, one can construct c.f. grammars for the languages L
1∪ L
2, L
1· L
2, L
∗1.
17 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
sketch of proof of point 1
Let G
1= hA, N
1, R
1, S
1i, G
2= hA, N
1, R
1, S
1i. We assume that N
1∩ N
2= ∅.
union :
H = hA, N
1∪ N
2∪ {σ}, R, σi
where R = R
1∪ R
2∪ {σ→S
1, σ→S
2}
L (H, σ) = L (G
1, S
1) ∪ L (G
2, S
2).
18 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
Let G
1= hA, N
1, R
1, S
1i, G
2= hA, N
1, R
1, S
1i.
product :
K = hA, N
1∪ N
2∪ {σ}, R, σi where R = R
1∪ R
2∪ {σ→S
1S
2}
L (H, σ) = L (G
1, S
1) · L (G
2, S
2).
star :
K
∗= hA, N
1∪ {σ}, R, σi where R = R
1∪ {σ→σS
1, σ→ε}
L (K
∗, σ) = L (G
1, S
1)
∗.
19 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
point 2 :Let
L = {a
nb
nc
n| n ≥ 1}.
L is not context-free.
Intuitive idea : if it was, there would be a repetition on some branch of a derivation-tree for w = a
Nb
Nc
N, leading, by pumping, to a larger derivation-tree with frontier w
′= a
N+kb
N+ℓc
N∈ / L or w
′′= a
Nb
N+kc
N+ℓ∈ / L.
Let L
1= {a
pb
qc
r| p, q , r ∈ N \ {0}, p = q}, L
2= {a
pb
qc
r| p , q, r ∈ N \ {0}, q = r}.
L
1, L
2are context-free (exercise).
L = L
1∩ L
2is not context-free.
20 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
point 2 :Let
L = {a
nb
nc
n| n ≥ 1}.
L is not context-free.
L
3= {a
pb
qc
r| p, q, r ∈ N \ {0}, p = q},
L
4= {a
pb
qc
r| p, q, r ∈ N \ {0}, q = r}.
L
3, L
4are context-free (exercise).
L = L
3∩ L
4.
This shows that CF ({a, b, c }
∗) is not closed under intersection.
L = C(CL
3∪ CL
4).
This shows that CF ({a, b, c }
∗) is not closed under complement.
21 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
A corollary of the above theorem, point 1,is Proposition
Let A be a finite alphabet. Then every regular subset of A
∗is also context-free.
One can also give a direct construction of a c.f. grammar for every finite automaton : let A = hQ, A, δ, q
0, F i be some nfa .
Let G = hA, N, R, S
q0i with
N = {S
q| q ∈ Q},
R : S
p−→ xS
q, for every (p , x, q) ∈ δ S
q−→ ε for every p ∈ F .
22 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
One can prove, by induction on |u | that : S
p−→
∗Ru · S
q⇔ p −→
uAq, showing that
L (G , S
q0) = L (A).
23 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
Theorem
Let A be a finite alphabet.
For every languages L ∈ CF (A
∗) and R ∈ REG (A
∗), L ∩ R is context-free.
24 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
proof of the theorem :
Let G = hA, N, P , Si be a c.f. grammar and A = hQ, A, δ, q
0, F i be some nfa .
We build the c.f. grammar : H = hA, N
′, P
′, σi with N
′= {(p, v, q) | p , q ∈ Q, v ∈ N}.
For every rule U −→ w
1U
1· w
2U
2· · · w
kU
kw
k+1in P we insert in P
′the set of all rules
(p, U , q) −→ w
1(p
1, U
1, q
1)w
2(p
2, U
2, q
2) · · · w
k(p
k, U
k, q
k)w
k+1where
p −→
w1Ap
1, q
1−→
w2Ap
2. . . q
k−1−→
wkAp
k, q
k−→
wk+1Aq.
25 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
We add in P
′all the rules
σ −→ (q
0, S , q)
for q ∈ F . One can prove that, for every v ∈ N, p, q ∈ Q L (H, (p, v, q )) = L (G , v) ∩ {u ∈ A
∗| p −→
uAq}.
It follows that :
L (H, σ) = L (G , S ) ∩ L (A).
26 / 51
Formal Languages-Course 5.
Context-free languages :closure properties
Closure properties
Exercice
Is CF (A
∗) closed under reversal ?
Is CF (A
∗) closed under regular substitution ?
Is CF (A
∗) closed under context-free substitution ? (i.e.
substitutions σ such that, for every x ∈ A, σ({x}) ∈ CF (A
∗))
27 / 51
Formal Languages-Course 5.
Context-free grammars :transformations
Context-free grammars : transformations
28 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars
Let G = hA, N, R, σi be a context-free grammar. A non-terminal S ∈ N is said productive iff
∃u ∈ A
∗, S −→
∗ Ru.
A non-terminal S is said useful iff
∃α, β, u ∈ A
∗, σ −→
∗ RαS β and S −→
∗Ru.
29 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars
Proposition
Let G = h A, N, R, σi be a context-free grammar. The set N
′of productive non-terminals of G can be computed.
1- If σ / ∈ N
′, then L (G , σ) = ∅.
2- If σ ∈ N
′, then the grammar G
′= h A, N
′, R
′, σi with
R
′= R ∩ N
′× (A ∪ N
′)
∗has productive non-terminals only and generates the same language as G .
30 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars
Let G = hA, N, R, σi.
Point 1 :Let
N
1= {S ∈ N | ∃w ∈ A
∗, S −→
1 Rw }.
N
i+1= {S ∈ N | ∃w ∈ (A ∪ N
i)
∗, S −→
1Rw } ∪ N
i. One can show that
N
′= [
i≥1
N
i.
The sequence N
iis increasing and included in the finite set N, hence there exists k ≤ Card (N) such that
N
1⊂ N
2⊂ . . . ⊂ N
k= N
k+1. It follows that
N
′= [
i≥1
N
i= N
k 31 / 51Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars
Point 2 : If σ −→
∗R′u, since R
′⊆ R, also σ −→
∗ Ru.
If σ −→
∗Ru, every non-terminal used in this derivation is productive, hence σ −→
∗R′u.
32 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars :example
Consider the context-free grammar G := hA, N, R, S
1i where A = {a, b, c}, N = {S
1, S
2, S
3, S
4, S
5, S
6} and R consists of the following 15 rules :
S
1→ aS
3S
5S
1→ bS
2S
2S
1→ aS
4S
2→ aS
3S
3S
2→ bS
6c
S
3→ bS
2S
3→ aS
3S
6S
3→ cS
6S
4→ bS
3S
4S
4→ aS
4S
4S
4→ b S
5→ aS
4S
5S
5→ bS
1S
5→ aS
5.
S
6→ aS
3.
N
1= {S
4}, N
2= {S
1, S
4}, N
3= {S
1, S
4, S
5}, N
4= N
3. Hence the set of productive non-terminals is
N
′= N
3= {S
1, S
4, S
5}.
33 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars :example
N
′= {S
1, S
4, S
5}. In grammar G the productions using N \ N
′must be removed :
S
1→ aS
3S
5S
1→ bS
2S
2S
1→ aS
4S
2→ aS
3S
3S
2→ bS
6c
S
3→ bS
2S
3→ aS
3S
6S
3→ cS
6S
4→ bS
3S
4S
4→ aS
4S
4S
4→ b S
5→ aS
4S
5S
5→ bS
1S
5→ aS
5.
S
6→ aS
3.
The new grammar G
′has the set of productions R
′: S
1→ aS
4S
4→ aS
4S
4S
4→ b S
5→ aS
4S
5S
5→ bS
1S
5→ aS
5.
34 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars
Proposition
Let G
′= h A, N
′, R
′, σi be a context-free grammar with only productive non-terminals.
1- The set N
′′of useful non-terminals can be computed.
2- The grammar G
′′= h A, N
′′, R
′′, σi with
R
′′= R
′∩ (N
′′× (A ∪ N
′′)
∗) has useful non-terminals only and generates the same language as G
′.
35 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars
Let G
′= hA, N
′, R
′, σi.
Point 1 :Let M
1= {σ}.
M
i+1= {S ∈ N
′| ∃α, β ∈ (A ∪ N
′)
∗, ∃T ∈ M
i, T −→
1R′αSβ} ∪ M
i. One can show that
N
′′= [
i≥1
M
i.
The sequence M
iis increasing and included in the finite set N, hence there exists k ≤ Card (N) such that
M
1⊂ M
2⊂ . . . ⊂ M
k= M
k+1. It follows that
N
′′= [
i≥1
M
i= M
k 36 / 51Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars
Point 2 : If σ −→
∗R′′u , since R
′′⊆ R
′, also σ −→
∗R′u.
If σ −→
∗R′u, every non-terminal used in this derivation is useful, hence σ −→
∗R′′u.
37 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Cleaning context-free grammars
clean grammars :example
G
′= hA, N
′, R
′, S
1i where R
′is the set of rules : S
1→ aS
4S
4→ aS
4S
4S
4→ b S
5→ aS
4S
5S
5→ bS
1S
5→ aS
5.
M
1= {S
1}, M
2= {S
1, S
4}, M
3= M
2N
′′= {S
1, S
4}
G
′′= hA, N
′′, R
′′, S
1i where R
′′is the set of rules : S
1→ aS
4S
4→ aS
4S
4S
4→ b
38 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation
Proposition
Let G = hA, N, Ri be a context-free grammar and U ∈ N such that U does not occur in the right-hand side of the rules with left-hand side U.
One can transform G into a grammar G
′= hA, N \ {U }, R
′i such that, for every S ∈ N \ {U }, L (G
′, S ) = L (G, S).
39 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation
Notation : given languages L
1, L
2we note L
1+ L
2the union L
1∪ L
2. A set of grammar rules with same left-hand side :
S −→ w
1, S −→ w
2, . . . , S −→ w
nis denoted by :
S −→ w
1+ w
2+ . . . + w
nGiven a c.f. grammar G = hA, N, R, σi, where
N = {S
1, . . . , S
i, . . . , S
n}, its set of rules R can always be written under the form :
S
1−→
ℓ1
X
j=1
w
1,j, S
i−→
ℓi
X
j=1
w
i,j, S
n−→
ℓn
X
j=1
w
n,j.
40 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation
Let G = hA, N, Ri, be a grammar of the previous form and U = S
nfulfills the assumption that :
∀j ∈ [1, ℓ
n], w
n,j∈ (A ∪ {S
1, · · · , S
n−1})
∗. We define the substitution σ : P (A ∪ N)
∗→P(A ∪ N)
∗by :
∀x ∈ A, σ(x) = x, ∀S ∈ N \ {U }, σ(S) = S, σ(U) =
ℓn
X
j=1
w
n,j.
Let G
′= hA, N \ {U}, R
′, σi be defined by : for every 1 ≤ i ≤ n − 1 :
S
i−→
ℓi
X
j=1
σ(w
i,j)
(i.e. R
′consists of all the rules above with lhs and rhs in N
′= {S
1, . . . , S
n−1}).
41 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation
This grammar does not use U = S
nanymore. Let us show that, for every i ∈ [1, n − 1] :
L (G
′, S
i) = L (G , S
i).
1-
∀w ∈ σ(U ), U −→
1Rw Hence,
∀v ∈ (A ∪ N)
∗, ∀w ∈ σ(v), v −→
∗Rw this implies that
∀i ∈ [1, n − 1], ∀w ∈
ℓi
X
j=1
σ(w
i,j), S
i−→
∗ Rw . (1)
By (1) R
′⊆ −→
∗R, implying that, for every i ∈ [1, n − 1]
L (G
′, S
i) ⊆ L (G , S
i).
42 / 51Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation
2- Let us prove by induction on d that :for every i ∈ [1, n − 1], w ∈ A
∗:
S
i d−→
Rw ⇒ S
i ∗−→
R′w . Let us suppose that :
S
i−→
1Rw
i,j= α
1S
nα
2S
n· · · α
kS
nα
k+1−→
d−1Rw , (2) where α
i∈ (A ∪ {S
1, . . . , S
n−1})
∗.
Base : d = 1 i.e. w
i,j= w .
Then w
i,j= w hence w ∈ σ(w
i,j), hence S
i−→
1R′w .
43 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation
Induction :d ≥ 2. Derivation (2) shows that
w = ˆ α
1w
1α ˆ
2w
2· · · α ˆ
kw
kα ˆ
k+1for some words α ˆ
i, w
i∈ A
∗such that α
i−→
aiRα ˆ
i, S
n−→
diRw
iand ( P
ki=1
a
i+ d
i) + a
k+1= d − 1.Hence S
n−→
1Rw
n,jidi−1
−→
Rw
i44 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation
By induction hypothesis, since w
n,jk∈ (A ∪ {S
1, . . . , S
n−1})
∗and a
i< d , d
i< d
α
i−→
∗R′α ˆ
i, w
n,ji−→
∗R′w
i(3) Note that
w
′= α
1w
n,j1α
2w
n,j2· · · α
kw
n,jkα
k+1∈ σ(w
i,j) Combining the rule S
i−→
1R′w
′with derivations (3) give a derivation
S
i−→
1R′w
′= α
1w
n,j1α
2w
n,j2· · · α
kw
n,jkα
k+1−→
∗R′w . Hence
L (G , S
i) ⊆ L (G
′, S
i).
45 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation : example
Example
Let G = hA, N, R, S
1i with
A = {a, b, c, d }, N = {S
1, S
2, S
3, S
4}, and R consists of the rules :
S
1→ aS
2bS
2+ ε S
2→ cS
3+ S
3d S
3→ cS
3+ ε S
4→ S
4d + ε
Since S
2does not appear in its right-hand side, we can remove S
2and obtain : G = hA, N
′, R
′, S
1i with N
′= {S
1, S
3, S
4},
46 / 51
Formal Languages-Course 5.
Context-free grammars :transformations A simple transformation
A simple transformation : example
Example (continued)
R
′consists of the rules :
S
1→ a(cS
3+ S
3d )b(cS
3+ S
3d ) + ε S
3→ cS
3+ ε
S
4→ S
4d + ε
which can be written as :
S
1→ acS
3bcS
3+ acS
3bS
3d + aS
3dbcS
3+ aS
3dbS
3d + ε S
3→ cS
3+ ε
S
4→ S
4d + ε
47 / 51Formal Languages-Course 5.
Context-free grammars :transformations Quadratic normal form
w-Quadratic normal form
Proposition
Every c.f. grammar G = hA, N, R, σi can be transformed into a c.f.
grammar G
′= hA, N
′, R
′, σi such that 1- ∀(S , m) ∈ R
′, |m| ≤ 2
2- L (G , σ) = L (G
′, σ
′).
Some remarks :
such a grammar is called in weak quadradic normal form (quadratic would mean that : ∀(S, m) ∈ R
′, |m| = 2 or (S = σ and m = ε)).
48 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Quadratic normal form
w-Quadratic normal form
step 1 :Let G = hA, N, R, σi such that one rule S → w is such that |w | ≥ 3. We can decompose
w = w
1· w
2with |w
1| < |w |, |w
2| < |w |.
Let G
′= hA, N
′, R
′, σi be defined by : N
′= N ∪ {W
1, W
2}, R
′= R \ { S → w
1· w
2}
∪ {S → W
1· W
2, W
1→ w
1, W
2→ w
2}.
Since W
1(resp. W
2) does not appear in w
1(resp. w
2), applying twice the above transformation, G
′is mapped into G . Hence
L (G , σ) = L (G
′, σ).
49 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Quadratic normal form
w-Quadratic normal form
step 2 : As long as there remains some rhs with length ≥ 3, we apply step 1 :
G = G
0G
1G
2. . . G
nuntil we obtain a c.f. grammar G
nwhere all rhs have length ≤ 2.
Since for every i ∈ [0, n − 1], the grammars G
i, G
i+1generate the same language, we have obtained a new grammar G
nin weak quadradic normal form for the language L (G , σ).
50 / 51
Formal Languages-Course 5.
Context-free grammars :transformations Quadratic normal form
w-Quadratic normal form : example
Example
Let G = hA, N, R, S i where A = {a, b, c , d }, N = {S } and R consists of the following rules :
S → aSbSc + d We obtain successively :
N = {S, W
1, W
2}, S → W
1W
2+ d , W
1→ aSb, W
2→ Sc , N = {S, W
1, W
2, W
3, W
4}, S → W
1W
2+ d , W
1→ W
3W
4,
W
2→ Sc, W
3→ aS, W
4→ b.
51 / 51