Tải bản đầy đủ (.pdf) (39 trang)

sliek môn máy tự động chương 6 simplication of context-free

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 (128.23 KB, 39 trang )

Chapter 6: Simplification of Context-Free
Grammars
November 13, 2009
Chapter 6: Simplification of Context-Free Grammars
Simplification of Context-Free Grammars
Some useful substitution rules.
Removing useless productions.
Removing λ-productions.
Removing unit-productions.
Chapter 6: Simplification of Context-Free Grammars
Some Useful Substitution Rules
G = (V, T, S, P)
A → x
1
Bx
2
∈ P
B → y
1
| y
2
| | y
n
∈ P
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

L(G) = L(G

)
Chapter 6: Simplification of Context-Free Grammars
Example 6.1
G = ({A, B}, {a, b}, A, P)
A → a | aaA | abBc
B → bA | b
G

= ({A, B}, {a, b}, A, P


)
A → a | aaA | abbAc | abbc
Chapter 6: Simplification of Context-Free Grammars
Eliminating left-recursive
G = (V, T, S, P)
A → Ax
1
| Ax
2
| | Ax
n
∈ P
A → y
1
| y
2
| | y
m
∈ P
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

L(G) = L(G

)
Chapter 6: Simplification of Context-Free Grammars
Example 6.2
G = ({A, B}, {a, b}, A, P)
A → Aa | aBc | λ
B → Bb | ba
A → aBcZ | Z | aBc | λ
Z → a | aZ
B → Bb | ba
A → aBcZ | Z | aBc | λ
Z → a | aZ
B → ba | baY
Y → b | bY
Chapter 6: Simplification of Context-Free Grammars
Removing Useless Productions
S → aSb | λ | A
A → aA
S → A is redundant as A cannot be transformed into a
terminal string.
Chapter 6: Simplification of Context-Free Grammars
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 if it involves any useless variable.
Chapter 6: Simplification of Context-Free Grammars
Example 6.3
G = ({S, A, B}, {a, b}, S, P)
S → A | B
A → aA | λ
C → bA
B → Bc
Chapter 6: Simplification of Context-Free Grammars
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
Chapter 6: Simplification of Context-Free Grammars
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
dependency graph
S A B

Chapter 6: Simplification of Context-Free Grammars
Theorem 6.1 - Linz ’s book
Theorem
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.
Chapter 6: Simplification of Context-Free Grammars
Proof of theorem 6.1 - Linz ’s book
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 )


.
Chapter 6: Simplification of Context-Free Grammars
Proof of theorem 6.1 - Linz ’s book
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

)
Chapter 6: Simplification of Context-Free Grammars
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.

Chapter 6: Simplification of Context-Free Grammars
Proof of theorem 6.1 - Linz ’s book
S → aS
1
b S → aS
1
b | ab
S
1
→ aS
1
b | λ S
1
→ aS
1
b | ab
Chapter 6: Simplification of Context-Free Grammars
Theorem 6.2 - Linz ’s book
Theorem
Let G = (V, T, S, P) be a CFG such that λ ∈ L(G). Then there
exists an equivalent grammar G

having no λ-productions.
Chapter 6: Simplification of Context-Free Grammars
Proof of theorem 6.2 - Linz ’s book
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
.
Chapter 6: Simplification of Context-Free Grammars
Proof of theorem 6.2 - Linz ’s book
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

.
Chapter 6: Simplification of Context-Free Grammars
Example 6.5
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}
Chapter 6: Simplification of Context-Free Grammars
Removing Unit-Productions
Definition
Any production of a context-free grammar of the form: A → B is
called a unit-production
Chapter 6: Simplification of Context-Free Grammars
Theorem 6.3 - Linz ’s book
Theorem
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.
Chapter 6: Simplification of Context-Free Grammars
Proof of theorem 6.3 - Linz ’s book
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

.
Chapter 6: Simplification of Context-Free Grammars
Example 6.6
S → Aa | B
B → A | bb
A → a | bc | B
Chapter 6: Simplification of Context-Free Grammars
Example 6.7
S → Aa | B
A → a | bc | B
B → A | bb
Chapter 6: Simplification of Context-Free Grammars

×