Tải bản đầy đủ (.ppt) (38 trang)

Automata and Formal Language (chapter 6) ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (85.49 KB, 38 trang )

Simplification of Context-Free
Grammars
Quan Thanh Tho

2
Simplification of Context-Free
Grammars

Some useful substitution rules.
• Removing useless productions.

Removing λ-productions.

Removing unit-productions.
3
Some Useful Substitution Rules
G = (V, T, S, P)
A → x
1
Bx
2
∈ P
B → y
1
| y
2
| | y
n
∈ P
L(G) = L(G^)
G^ = (V, T, S, P^)


A → x
1
y
1
x
2
| x
1
y
2
x
2
| | x
1
y
n
x
2
∈ P^
4
Example
G = ({A, B}, {a, b}, A, P)
A → a | aaA | abBc
B → abbA

| b
G^ = (V, T, S, P^)
A → a

| aaA


| ababbAc | abbc
5
Example 6.1
G = (V, T, S, P)
A → Ax
1
| Ax
2
| | Ax
n
∈ P
A → y
1
| y
2
| | y
m
∈ P
L(G) = L(G^)
G^ = (V∪{Z}, T, S, P^)
A → y
i
| y
i
Z (i =1, m) ∈ P^
Z → x
i
| x
i

Z (i =1, n) ∈ P^
6
Example 6.2
G = ({A, B}, {a, b}, A, P)
A → Aa | aBc | λ
B → Bb

| ba
A → aBc

| aBcZ

| Z | λ A → aBc

| aBcZ

| Z | λ
Z → a | aZ Z → a | aZ
B → Bb | ba B → ba | baY
Y → b | bY
7
Removing Useless Productions
S → aSb | λ | A
A → aA
S → A is redundant as A cannot be transformed into a
terminal string.
8
Removing Useless Productions
G = (V, T, S, P)
A ∈ V is useful iff there is w ∈ L(G) such that:

S ⇒* xAy

⇒* w
A production is useless it it involves any useless variable.
9
Example 6.3
G = ({S, A, B}, {a, b}, S, P)
S → A
A → aA

| λ
B → bA
10
Example 6.4
G = ({S, A, B, C}, {a, b}, S, P)
S → aS | A | C S → aS | A S → aS | A
A → a A → a A → a
B → aa B → aa
C → aCb
11
Example 6.5
G = ({S, A, B, C}, {a, b}, S, P)
S → aS | A | C S → aS | A S → aS | A
A → a A → a A → a
B → aa B → aa
C → aCb dependency graph
S A B
12
Theorem 6.1
Let G = (V, T, S, P) be a context-free grammar.

Then there exists an equivalent grammar G^ = (V^, T^, S,
P^)
that does not contain any useless variables or productions.
13
Theorem 6.1 (cont’d)
Proof:

Construct (V
1
, T, S, P
1
) such that V
1
contains only variables A
for which A ⇒* w ∈ T*.
1. Set V
1
to ∅.
2. Repeat until no more variables are added to V
1
:
For every A ∈ T for which P has a production of the form
A → x
1
x
2
x
n
(x
i

∈ T*∪V
1
)
add A to V
1
.
3.

Take P
1
as all the productions in P with symbols in (V
1
∪ T)*.
14
Theorem 6.1 (cont’d)
Proof:

Draw the variable dependency graph for G
1
and find all
variables that cannot be reached from S.

Remove those variables and the productions involving them.

Eliminate any terminal that does not occur in a useful
production.
⇒ G^ = (V^, T^, S, P^)

15
Removing λ-Productions


Any production of a context-free grammar of the form:
A → λ
is called a λ-production.

Any variable A for which the derivation:
A ⇒* λ
is possible is called nullable.
16
Example 6.5
S → aS
1
b S → aS
1
b | ab
S
1
→ aS
1
b | λ S
1
→ aS
1
b | ab
17
Theorem 6.2
Let G = (V, T, S, P) be a CFG such that λ ∉ L(G).
Then there exists an equivalent grammar G^ having no
λ-productions.
18

Theorem 6.2 (cont’d)
Proof:

Find the set V
N
of all nullable variables of G:
1. For all productions A → λ, put A into V
N
.
2. Repeat until no more variables are added to V
N
:
For all productions
B → A
1
A
2
A
n
(A
i
∈ V
N
)
add B to V
N
.
19
Theorem 6.2 (cont’d)
Proof:


For each production in P of the form:
A → x
1
x
2
x
m
(m ≥ 1, x
i
∈ V∪T)
put into P^ that production as well as all those generated by
replacing null variables with λ in all possible combination.
Exception: if all x
i
are nullable, then A → λ is not put into P^.
20
Example 6.5 (cont’d)
S → ABaC S → ABaC | BaC | AaC | ABa | aC | Aa |Ba | a
A → BC A → B | C | BC
B → b | λ B → b
C → D | λ C → D
D → d D → d
V
N
= {A, B, C}
21
Removing Unit-Productions
Any production of a context-free grammar of the form:
A → B

is called a unit-production.
22
Theorem 6.3
Let G = (V, T, S, P) be a CFG without λ-productions.
Then there exists an equivalent grammar G^ = (V, T, S, P^)
that does not have any unit-productions.
23
Theorem 6.3 (cont’d)
Proof:
1. Put into P^ all non-unit-productions of P.
2. Repeat until no more productions are added to P^:
For every A and B ∈ V such that A ⇒* B and B → y
1
| y
2
| | y
n
∈ P^
add A → y
1
| y
2
| | y
n
to P^.
24
Example 6.6
S → Aa | B
B → A | bb
A → a | bc | B

25
Example 6.7
S → Aa | B S → Aa
A → a | bc | B A → a | bc
B → A | bb B → bb
S ⇒* A S → a | bc | bb
S ⇒* B A → bb
A ⇒* B B → a | bc
B ⇒* A

×