1. Trang chủ
  2. » Công Nghệ Thông Tin

Simplification of Context-Free Grammars

38 359 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Simplification of Context-Free Grammars
Tác giả Quan Thanh Tho
Trường học Hochiminh City University of Technology
Chuyên ngành Computer Science
Thể loại phân tích
Thành phố Ho Chi Minh City
Định dạng
Số trang 38
Dung lượng 80 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Simplification of Context-Free Grammars • Some useful substitution rules... Then there exists an equivalent grammar G^ = V^, T^, S, P^ that does not contain any useless variables or prod

Trang 1

Simplification of Context-Free

Grammars

Quan Thanh Tho qttho@cse.hcmut.edu.vn

Trang 2

Simplification of Context-Free

Grammars

• Some useful substitution rules.

• Removing useless productions.

• Removing -productions.

Trang 3

Some Useful Substitution Rules

G = (V, T, S, P)

A  x1Bx2  P

B  y1 | y2 | | yn  P

L(G) = L(G^) G^ = (V, T, S, P^)

A  x1y1x2 | x1y2x2 | | x1ynx2  P^

Trang 7

Removing Useless Productions

S  aSb |  | A

A  aA

S  A is redundant as A cannot be transformed into a

terminal string.

Trang 8

Removing Useless Productions

Trang 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

Trang 13

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 P1 as all the productions in P with symbols in (V1 T)*.

Trang 14

Theorem 6.1 (cont’d)

Proof:

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.

Trang 16

Example 6.5

S1  aS1b |  S1  aS1b | ab

Trang 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

Trang 18

Theorem 6.2 (cont’d)

Proof:

• Find the set VN of all nullable variables of G:

1 For all productions A   , put A into VN.

For all productions

B  A1A2 An (Ai  VN)

Trang 19

Theorem 6.2 (cont’d)

Proof:

• For each production in P of the form:

put into P^ that production as well as all those generated by

Exception: if all xi are nullable, then A   is not put into P^

Trang 21

Removing Unit-Productions

Any production of a context-free grammar of the form:

A  B

is called a unit-production

Trang 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

Trang 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  y1 | y2 | | yn  P^ add A  y1 | y2 | | yn to P^.

Trang 24

Example 6.6

S  Aa | B

B  A | bb

A  a | bc | B

Trang 26

Theorem 6.4

Let L be a context-free language that does not contain 

Then there exists a CFG that generates L and does not have any useless productions,  -productions, or unit-productions

Trang 27

Theorem 6.4 (cont’d)

Let L be a context-free language that does not contain 

Then there exists a CFG that generates L and does not have any useless productions,  -productions, or unit-productions

Proof:

1 Remove -productions

2 Remove unit-productions

3 Remove useless-productions

Trang 28

Two Important Normal Forms

• Chomsky normal form.

• Greibach normal form.

Trang 29

Chomsky Normal Form

A context-free grammar G = (V, T, S, P) is in Chomsky normal form iff all productions are of the form:

A  BC

or

A  a

where A, B, C  V and a  T.

Trang 30

Theorem 6.5

Any context-free grammar G = (V, T, S, P) such that   L(G)has an equivalent grammar G^ = (V^, T, S, P^) in Chomsky normal form

Trang 31

Theorem 6.5 (cont’d)

Proof:

First, construct an equivalent grammar G 1 = (V 1 , T, S, P 1 ).

• V 1 = V  {B a | a  T} P 1 = P  {B a  a | a  T}

• Remove all terminals from productions of length  1:

1 Put all productions A  a into P1.

2 Repeat until no more productions are added to P 1 :

For each production

A  x 1 x2 xn (n  2, x i  TV)

add A  C 1 C 2 C n to P 1

where Ci = xi if xi  V or C i = Ba if xi = a

Trang 32

Theorem 6.5 (cont’d)

Proof:

Construct G^ = (V^, T, S, P^) from G1 = (V1, T, S, P1).

• V^ = V 1

• Reduce the length of the right sides of the productions:

1 Put all productions A  a and A  BC into P^.

2 Repeat until no more productions are added to P^:

For each production

Trang 33

Example 6.8

S  ABa

A  aaB

B  aC

Trang 34

Greibach Normal Form

A context-free grammar G = (V, T, S, P) is in Greibach normal form iff all productions are of the form:

A  ax

where a  T and x  V*.

Trang 35

Theorem 6.6

Any context-free grammar G = (V, T, S, P) such that   L(G)has an equivalent grammar G^ = (V^, T, S, P^) in Greibach normal form

Trang 37

Example 6.9

A2  A1A2 | b

A1  A2A2 | a

Ngày đăng: 13/05/2014, 10:21