1 context-free grammars 2. construction 3. parse tree 4. ambiguity 1. formal definition 5....

33
1 Context-Free Grammars 2. Construction 3. Parse Tree 4. Ambiguity 1. Formal Definition 5. Simplification of CFG 6. CNF & GNF

Upload: shawn-robertson

Post on 26-Dec-2015

235 views

Category:

Documents


0 download

TRANSCRIPT

1

Context-Free Grammars

2. Construction

3. Parse Tree

4. Ambiguity

1. Formal Definition

5. Simplification of CFG

6. CNF & GNF

2

English Grammar

sentence noun_phrase predicate

noun_phrase article noun

predicate verb

article a | an | the

noun boy | dog

verb runs | walks

a boy runs a dog walks

3

Context-Free Grammar

A , where AV, ( V T )*

A grammar G=( V, T, S, P ) is said to be

context-free if all productions in P have the form

4

Palindrome Language

L={ w | w{0,1}* and w = wR }

recursive definition

• basis , 0, 1 are palindromes.

• induction If w is a palindrome, so is 0w 0 and 1w1.

5

Palindrome Language

definition with grammars or rules

1. is a palindrome.

2. 0 is a palindrome.

3. 1 is a palindrome.

4. If w is a palindrome, so is 0w0.

5. If w is a palindrome, so is 1w1.

L={ w | w{0,1}* and w = wR }

6

CFG & Palindrome Language

L={ w | w{0,1}* and w = wR }

1. is a P.

2. 0 is a P.

3. 1 is a P.

4. If w is a P, so is 0w0. 5. If w is a P, so is 1w1.

1. P

2. P 0

3. P 1

4. P 0P 0

5. P 1P 1

7

CFG of Palindrome Language

L={ w | w{0,1}* and w = wR }

Compact notation

S | 0 | 1 | 0S 0 | 1S 1

• CFG for palindromes on {0,1}

R = ({S }, {0,1}, P, S ), P is defined as follow

S , S 0, S 1, S 0S 0, S 1S 1

8

Example 7.1

L={ 0n1n | n 0 }

S | 0S1

R = ({S }, {0,1}, P, S ), P is defined as follow

9

Example 7.2

L={ 0n1m | n m }

S AC | CB, C 0C1 |

A A0 | 0 , B 1B | 1

R = ({S,A,B,C }, {0,1}, P, S ), P is defined as follow

10

Example 7.3

L={ w{0,1}* | w contains same number of 0’s

and 1’s }

S | 0S1 | 1S0 |SS

R = ({S }, {0,1}, P, S ), P is defined as follow

11

Example 7.4

L={w {0,1}*| n0(w)=n1(w) and n0(v) n1(v)

where v is any prefix of w }

S | 0S1 | SS

R = ({S }, {0,1}, P, S ), P is defined as follow

12

Derivations and Recursive Inferences

L={a2nbm | n 0, m 0 }

for w =aabb :

S AB , A |aaA , B | Bb

R = ({S,A,B }, {a,b}, P, S ), P is defined as follow

SABaaABaaABbaaBbaaBbbaabb

S AB

A aaA

B Bb

A

B Bb B

13

Context-Free Language

L(G) = {w | w T * and S w }

Let G=( V, T, S, P ) be context-free, then

14

Left/Right Most Derivations

L={a2nbm | n 0, m 0 }

S AB , A |aaA , B | Bb

SABaaABaaABbaaBbaaBbbaabb

SABaaABaaBaaBbaaBbbaabb

SABABbABbbAbbaaAbbaabb

Left most :

Right most :

for w =aabb :

15

Parse Tree

Let G = ( V,T , S, P ) be a CFG. A tree is a parse tree for G if :

1. Each interior node is labeled by a variable in V 2. Each leaf is labeled by a symbol in T{}. Any -labeled leaf is the only child of its parent.

3. If an interior node is labeled A, and its

children (from left to right) labeled x1,x2, ,xk,

Then A x1,x2, ,xk P .

16

Parse Tree

S | 0 | 1 | 0S 0 | 1S 1

Example 7.5 L={ w | w{0,1}* and w = wR }

S

0 S 0

1 S 1

S

0 S 0

1

0 S 0

w=0110 w=00100

deriv

atio

ns

recu

rsiv

e

infe

ren

ces

17

Ambiguity

EI |E +E |E E|(E), Ia | b

G = ({E, I }, {a, b, (, ), +, }, P, E )

Derivation for w = a a a :

EEEE+EEI+EEa+EE a+aa

EEEI+Ea+Ea+EEa+aa

18

Ambiguity

parse-tree for w = a a a :

E

E E

I

E + E

a

I

a

I

a

E

E E

I

a I

E E

a

I

a

19

Removing Ambiguity

EI | E+E | EE | (E) , Ia | b

Left most derivation for w = a a a :

EETT+TF+T I+T a+Ta+TF

ET|E+T, TF|TF, FI|(E) , Ia|b|Ia|Ib

a+FFa+IFa+aFa+aIa+aa

ETTT(E)T(E+T)T(a+a)a

20

Inherent Ambiguity

• What is inherent ambiguity

A CFL L is said to be inherently ambiguous if

every grammar that generates it is ambiguous.

Example 7.6 Let L={ w | w{0,1}* and n0(w)=n1(w) }

S | 0S1 | 1S0 | 0S11S0 | 1S00S1

L is not inherently ambiguous ,because there is an unambiguous CFG :

21

Example 7.7

L={anbncmdm | n1, m1} {anbmcmdn | n1, m1}The CFG for L is :

SAB | C , AaAb | ab, BcBd | cd

CaCd | aDd, DbDc | bc

Let w= abcd , there are two left most derivations

SAB abB abcd

SC aDd abcd

22

Simplification of CFG

Why & what :

C1CC | 1DG | 0G, D1CD | 1DH | 0H,

E0A, F0B, G, H1

-productions

unit productions

useless symbols and productions

SA | B, A1CA | 1DE | , B1CB | 1DF,

23

Eliminating -productions

Variable A is said to be nullable if A .

Let G=(V,T,P,S) is a CFG

If A → P, then A is nullable.

*

If A → A1 A2 AkP, and Ai → P for i=1, ,k

then A is nullable.

Example 7.8 G : SAB, AaAA|, BbBB|

A A is nullable.

B B is nullable. SAB S is nullable.

24

Eliminating unit productions

Example 7.9 G : SA|B|0S1, A0A|0, B1B|1

S 0A|0|1B|1|0S1

A0A|0

B1B|1

25

Eliminating useless productions

A symbol X is useful for a grammar G=(V,T,P,S),

if there is a derivation for some wT*

A symbol X is generating if X w for some wT*

S X w * *

*

A symbol X is reachable if S X for {,}(VT)**

26

S and A are generating , B is not.

Eliminate B, that eliminate SAB, leaving

Now only S is reachable. So there leaves Sa.

Sa, Ab

If eliminate non-reachable symbol first :

Example 7.10 G : SAB|a, Ab

SAB|a, Ab SAB|a, Ab

Then eliminate non-generating symbol :

SAB|a, Ab Sa, Ab

27

Example 7.11 G : SA | B, A1CA | 1DE |

E0A, F0B, G, H1

B1CB | 1DF, C1CC| 1DG | 0G, D1CD | 1DH | 0H,

• eliminating -productions

the only one : A

SA | B, A1CA | 1C | 1DE, B1CB | 1DF,

C1CC | 1DG | 0G, D1CD | 1DH | 0H,

E0A|0, F0B, G, H1

28

SA | B, A1CA | 1C | 1DE, B1CB | 1DF,

C1CC | 1DG | 0G, D1CD | 1DH | 0H,

E0A|0, F0B, G, H1

• eliminating unit productions

the only two : SA and SB

S1CA | 1C | 1DE|1CB | 1DF,

A1CA | 1C | 1DE, B1CB | 1DF,

C1CC | 1DG | 0G, D1CD|1DH | 0H,

E0A|0, F0B, G, H1

29

S1CA | 1C | 1DE|1CB | 1DF,

A1CA | 1C | 1DE, B1CB | 1DF,

C1CC | 1DG | 0G, D1CD|1DH | 0H,

E0A|0, F0B, G, H1

• eliminating useless symbols and productions

S1DE ,A1DE , D1DH | 0H , E0A | 0, H1

30

Chomsky Normal Form(CNF)

1. A BC ;

2. A a .

S1DE ,A1DE , D1DH | 0H , E0A | 0, H1

Chomsky normal form :

SIE ,AIE, DIH|EH, EEA|0, IHD, H1

DIH|FH, EFA|0, F0

31

Chomsky Normal Form(CNF)

SABa , Aaab , BAc

Example 7.12 Convert following grammar to CNF

32

Greibach Normal Form(GNF)

A ax , where aT , xV

SAB , AaA|bB|b , Bb

Example 7.13 Convert following grammar to GNF

S01S1|00

Example 7.14 Convert following grammar to GNF

33

? ? ?

• eliminating -productions : L ?

• Greibach normal form :

A a advantage ?

A A shortage ?

• left recursiveness

• Chomsky normal form :

A a |BC advantage ?