Reduced Grammars

116 downloads 254 Views 82KB Size Report
Def: A grammar is said to be reduced if ... An active nonterminal symbol is a nonterminal symbol that can generate at least one terminal ... Click here for answers ...
Reduced Grammars Def:

A grammar is said to be reduced if a. there are no productions of the form A b. for all nonterminals A, A c. S

Def:

+

Vi

Vi

+

where A

A, Vn and

Vt*

Vn.

An active nonterminal symbol is a nonterminal symbol that can generate at least one terminal string.

Algorithm 1.

1

= {A|A

2.

n

=

n-1

P, {A|A

P,

(Vt

n-1)*}

3.

repeat 2 until

4.

The active nonterminal symbols are those nonterminal symbols that are in productions that contain nonterminal symbols that are not in n.

e.g. S A B C

a S | b a | a a

A b B b C C

a B b A

1

2

3

4

5

Click here for answers

n

=

Vt*}

n-1 n.

Delete all

Def: A reachable symbol is a symbol that is in a sentential form. Algorithm 1.

0

= {S}

2.

n

=

3.

n-1

{A|B

repeat step 2 until

4.

A n

=

,B

n-1,

,

V*, A

Vn }

n-1

The reachable nonterminal symbols are those nonterminal symbols that are in productions that contain nonterminal symbols that are not in n.

n.

Delete all

e.g. S

A B C

a | b | a a d a | a

S b A B A c S b S b a

0

1

2

3

Click here for answers Note: The appropriate order to apply the two above algorithms to a grammar is to first apply the algorithm for active symbols and then the algorithm for reachable symbols. What could happen if you first did the reachable algorithm and then the active algorithm?

Productions Def:

A

production is a production whose right hand side is empty or , i.e. where A

.

An algorithm to eliminate from the right hand side of all productions except the start symbol when language of the grammar is given below. Algorithm Construct N = { A | A

2.

Construct a new set of productions P' such that if A P, then 0B1 1B2 …Bk k for 1 i k if Bi N and no symbol in j N 0 j i, then add A 0B1 … Bi-1 i-1 iBi+1 …Bk k to P'. do not add A to P'.

3.

Add S'

4.

If

G

-free

= ( Vn

Vn and A

+

1.

}

S to P'.

L(G), then add S'

{S'}, Vt, P

-free

to P'.

P', S' ) where P

-free

is P with all productions whose RHS is

e.g. S A C

A S | b C a | a C | a |

Click here for answers. Construct an algorithm for N

S

a S b S | b S a S |

removed.

is in the

ANSWERS

Active Nonterminal Symbols Answer 1

= { B }

2

= { B, A }

3

= { B, A, S }

4

= { B, A, S } S A B C

a A a S b |b B B a b b |a C a C A

Return

Reachable Nonterminal Symbols Answer 0

= { S }

1

= { S, A, B }

2

= { S, A, B } S

a S b | b A B | a A a A c B d C a S b S | a b a

Return

-free Productions Answers

S

A S | b C A a | C a C | a |

N = { A, C }

-free Productions S

A C

S

A S b C b a a C a

a S b S | b S a S |

-free Productions S

a S b S a b S a S b a b | b S a S b a S b S a b a |

Return

N = {S}