• Aucun résultat trouvé

Formal Languages-Course 5.

N/A
N/A
Protected

Academic year: 2022

Partager "Formal Languages-Course 5."

Copied!
51
0
0

Texte intégral

(1)

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

(2)

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

(3)

Formal Languages-Course 5.

Context-free grammars : ambiguity

Context-free grammars : ambiguity

3 / 51

(4)

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

1

6= T

2

and 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

2

with 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

2

that end in the same terminal word w

- there are two different rightmost-derivations D

1

, D

2

that end in the same terminal word w

- there are two different construction-trees that have the same yield w .

4 / 51

(5)

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

n

b

m

| n ≥ m ≥ 1}.

D

1

: S

−→

R

aS

−→

R

aaSb

−→

R

aaabb D

2

: S

−→

R

aSb

−→

R

aaSb

−→

R

aaabb

5 / 51

(6)

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

n

b

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

(7)

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

n

b

m

| n ≥ m ≥ 1}.

This new grammar for the same language L is non-ambiguous.

Let w = a

n

b

m

with n ≥ m ≥ 1.

The only leftmost derivation for a

m+r

b

m

is D(r, m) : S −→

rR

a

r

S −→

1R

a

r

T −→

m−1R

a

r

a

m−1

Tb

m−1

−→

1R

a

r

a

m−1

abb

m−1

= a

m+r

b

m

.

7 / 51

(8)

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

(9)

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+r

b

m

is : r

1r

· r

2

· r

3m−1

· r

4

.

9 / 51

(10)

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

(11)

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

(12)

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

(13)

Formal Languages-Course 5.

Context-free grammars : ambiguity

context-free gramars : ambiguity

wbwisie can be seen, according to T

1

as : while cond

––begin ––while cond ––––i

––;

––i

––end or, according to T

2

as : while cond

––begin ––while cond ––––i

––––;

––––i

––end

13 / 51

(14)

Formal 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

0

s d

1

· · · s d

n−1

where each d

i

is a prime instruction.

14 / 51

(15)

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 −→

3

wbC e , wbC e −→

2

wbDsDe −→

3

wbwisi e, producing the structure :

while cond ––begin ––while cond ––––i

––;

––i ––end

15 / 51

(16)

Formal Languages-Course 5.

Context-free languages :closure properties

Context-free languages : closure properties

16 / 51

(17)

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

(18)

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

1

i, G

2

= hA, N

1

, R

1

, S

1

i. 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

(19)

Formal Languages-Course 5.

Context-free languages :closure properties

Closure properties

Let G

1

= hA, N

1

, R

1

, S

1

i, G

2

= hA, N

1

, R

1

, S

1

i.

product :

K = 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

).

star :

K

= hA, N

1

∪ {σ}, R, σi where R = R

1

∪ {σ→σS

1

, σ→ε}

L (K

, σ) = L (G

1

, S

1

)

.

19 / 51

(20)

Formal Languages-Course 5.

Context-free languages :closure properties

Closure properties

point 2 :Let

L = {a

n

b

n

c

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

N

b

N

c

N

, leading, by pumping, to a larger derivation-tree with frontier w

= a

N+k

b

N+ℓ

c

N

∈ / L or w

′′

= a

N

b

N+k

c

N+ℓ

∈ / L.

Let L

1

= {a

p

b

q

c

r

| p, q , r ∈ N \ {0}, p = q}, L

2

= {a

p

b

q

c

r

| p , q, r ∈ N \ {0}, q = r}.

L

1

, L

2

are context-free (exercise).

L = L

1

∩ L

2

is not context-free.

20 / 51

(21)

Formal Languages-Course 5.

Context-free languages :closure properties

Closure properties

point 2 :Let

L = {a

n

b

n

c

n

| n ≥ 1}.

L is not context-free.

L

3

= {a

p

b

q

c

r

| p, q, r ∈ N \ {0}, p = q},

L

4

= {a

p

b

q

c

r

| p, q, r ∈ N \ {0}, q = r}.

L

3

, L

4

are 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

(22)

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

q0

i with

N = {S

q

| q ∈ Q},

R : S

p

−→ xS

q

, for every (p , x, q) ∈ δ S

q

−→ ε for every p ∈ F .

22 / 51

(23)

Formal Languages-Course 5.

Context-free languages :closure properties

Closure properties

One can prove, by induction on |u | that : S

p

−→

∗R

u · S

q

⇔ p −→

uA

q, showing that

L (G , S

q0

) = L (A).

23 / 51

(24)

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

(25)

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

1

U

1

· w

2

U

2

· · · w

k

U

k

w

k+1

in 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+1

where

p −→

w1A

p

1

, q

1

−→

w2A

p

2

. . . q

k−1

−→

wkA

p

k

, q

k

−→

wk+1A

q.

25 / 51

(26)

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 −→

uA

q}.

It follows that :

L (H, σ) = L (G , S ) ∩ L (A).

26 / 51

(27)

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

(28)

Formal Languages-Course 5.

Context-free grammars :transformations

Context-free grammars : transformations

28 / 51

(29)

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 −→

R

u.

A non-terminal S is said useful iff

∃α, β, u ∈ A

, σ −→

R

αS β and S −→

R

u.

29 / 51

(30)

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

(31)

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 R

w }.

N

i+1

= {S ∈ N | ∃w ∈ (A ∪ N

i

)

, S −→

1R

w } ∪ N

i

. One can show that

N

= [

i≥1

N

i

.

The sequence N

i

is 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 / 51

(32)

Formal 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 productive, hence σ −→

R

u.

32 / 51

(33)

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

1

i 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

3

S

5

S

1

→ bS

2

S

2

S

1

→ aS

4

S

2

→ aS

3

S

3

S

2

→ bS

6

c

S

3

→ bS

2

S

3

→ aS

3

S

6

S

3

→ cS

6

S

4

→ bS

3

S

4

S

4

→ aS

4

S

4

S

4

→ b S

5

→ aS

4

S

5

S

5

→ bS

1

S

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

(34)

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

3

S

5

S

1

→ bS

2

S

2

S

1

→ aS

4

S

2

→ aS

3

S

3

S

2

→ bS

6

c

S

3

→ bS

2

S

3

→ aS

3

S

6

S

3

→ cS

6

S

4

→ bS

3

S

4

S

4

→ aS

4

S

4

S

4

→ b S

5

→ aS

4

S

5

S

5

→ bS

1

S

5

→ aS

5

.

S

6

→ aS

3

.

The new grammar G

has the set of productions R

: S

1

→ aS

4

S

4

→ aS

4

S

4

S

4

→ b S

5

→ aS

4

S

5

S

5

→ bS

1

S

5

→ aS

5

.

34 / 51

(35)

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

(36)

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

i

is 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 / 51

(37)

Formal 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

(38)

Formal Languages-Course 5.

Context-free grammars :transformations Cleaning context-free grammars

clean grammars :example

G

= hA, N

, R

, S

1

i where R

is the set of rules : S

1

→ aS

4

S

4

→ aS

4

S

4

S

4

→ b S

5

→ aS

4

S

5

S

5

→ bS

1

S

5

→ aS

5

.

M

1

= {S

1

}, M

2

= {S

1

, S

4

}, M

3

= M

2

N

′′

= {S

1

, S

4

}

G

′′

= hA, N

′′

, R

′′

, S

1

i where R

′′

is the set of rules : S

1

→ aS

4

S

4

→ aS

4

S

4

S

4

→ b

38 / 51

(39)

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

(40)

Formal Languages-Course 5.

Context-free grammars :transformations A simple transformation

A simple transformation

Notation : given languages L

1

, L

2

we note L

1

+ L

2

the union L

1

∪ L

2

. A set of grammar rules with same left-hand side :

S −→ w

1

, S −→ w

2

, . . . , S −→ w

n

is denoted by :

S −→ w

1

+ w

2

+ . . . + w

n

Given 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

(41)

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

n

fulfills 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

(42)

Formal Languages-Course 5.

Context-free grammars :transformations A simple transformation

A simple transformation

This grammar does not use U = S

n

anymore. Let us show that, for every i ∈ [1, n − 1] :

L (G

, S

i

) = L (G , S

i

).

1-

∀w ∈ σ(U ), U −→

1R

w Hence,

∀v ∈ (A ∪ N)

, ∀w ∈ σ(v), v −→

R

w this implies that

∀i ∈ [1, n − 1], ∀w ∈

i

X

j=1

σ(w

i,j

), S

i

−→

∗ R

w . (1)

By (1) R

⊆ −→

R

, implying that, for every i ∈ [1, n − 1]

L (G

, S

i

) ⊆ L (G , S

i

).

42 / 51

(43)

Formal 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

−→

R

w ⇒ S

i ∗

−→

R

w . Let us suppose that :

S

i

−→

1R

w

i,j

= α

1

S

n

α

2

S

n

· · · α

k

S

n

α

k+1

−→

d−1R

w , (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

(44)

Formal Languages-Course 5.

Context-free grammars :transformations A simple transformation

A simple transformation

Induction :d ≥ 2. Derivation (2) shows that

w = ˆ α

1

w

1

α ˆ

2

w

2

· · · α ˆ

k

w

k

α ˆ

k+1

for some words α ˆ

i

, w

i

∈ A

such that α

i

−→

aiR

α ˆ

i

, S

n

−→

diR

w

i

and ( P

k

i=1

a

i

+ d

i

) + a

k+1

= d − 1.Hence S

n

−→

1R

w

n,ji

di−1

−→

R

w

i

44 / 51

(45)

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

= α

1

w

n,j1

α

2

w

n,j2

· · · α

k

w

n,jk

α

k+1

∈ σ(w

i,j

) Combining the rule S

i

−→

1R

w

with derivations (3) give a derivation

S

i

−→

1R

w

= α

1

w

n,j1

α

2

w

n,j2

· · · α

k

w

n,jk

α

k+1

−→

R

w . Hence

L (G , S

i

) ⊆ L (G

, S

i

).

45 / 51

(46)

Formal Languages-Course 5.

Context-free grammars :transformations A simple transformation

A simple transformation : example

Example

Let G = hA, N, R, S

1

i with

A = {a, b, c, d }, N = {S

1

, S

2

, S

3

, S

4

}, and R consists of the rules :

S

1

→ aS

2

bS

2

+ ε S

2

→ cS

3

+ S

3

d S

3

→ cS

3

+ ε S

4

→ S

4

d + ε

Since S

2

does not appear in its right-hand side, we can remove S

2

and obtain : G = hA, N

, R

, S

1

i with N

= {S

1

, S

3

, S

4

},

46 / 51

(47)

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

3

d )b(cS

3

+ S

3

d ) + ε S

3

→ cS

3

+ ε

S

4

→ S

4

d + ε

which can be written as :

S

1

→ acS

3

bcS

3

+ acS

3

bS

3

d + aS

3

dbcS

3

+ aS

3

dbS

3

d + ε S

3

→ cS

3

+ ε

S

4

→ S

4

d + ε

47 / 51

(48)

Formal 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

(49)

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

2

with |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

(50)

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

0

G

1

G

2

. . . G

n

until we obtain a c.f. grammar G

n

where all rhs have length ≤ 2.

Since for every i ∈ [0, n − 1], the grammars G

i

, G

i+1

generate the same language, we have obtained a new grammar G

n

in weak quadradic normal form for the language L (G , σ).

50 / 51

(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

1

W

2

+ d , W

1

→ aSb, W

2

→ Sc , N = {S, W

1

, W

2

, W

3

, W

4

}, S → W

1

W

2

+ d , W

1

→ W

3

W

4

,

W

2

→ Sc, W

3

→ aS, W

4

→ b.

51 / 51

Références

Documents relatifs

In this paper we described an architecture for creating Service Front Ends that are able to adapt to different context of use. We described a set of modules that manage the

In Section 3 we show that for regular languages there is a dichotomy between polynomial and exponential antichain growth, and give a polynomial-time algorithm for distinguishing the

For non finite state languages, the set of congruence classes will be infinite, and thus we cannot have a finite fiducial set for the set of all strings in Sub(L), but we can have

Subject to this caveat, we can define a small reduction system that represents the same language, namely the set of all strings that reduces to the least string w(S) (w.r.t. ≺) in

Some applications of the slender languages to cryptography are shown in [1], Shallit [19] investigated slender regular languages in connection with numération Systems, the

In this section we prove the main resuit of this paper which states that if every strong itérative pair of a context-free language K is very degenerate, then K is a regular

Automatic language processing : in order to treat natural language text and extract its semantics or to produce some natural language sentence expressing some meaning, the

From now on, we accept as regular expressions, expressions using the usual symbols ∪ (instead of ⊕), · (instead of ⊗), using k-ary notation for the product and for the union