prof. busch - lsu1 linear grammars grammars with at most one variable at the right side of a...

43
Prof. Busch - LSU 1 Linear Grammars Grammars with at most one variable at the right side of a production Examples: A aAb A Ab S S aSb S

Upload: colleen-garrison

Post on 22-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Prof. Busch - LSU 1

Linear GrammarsGrammars with at most one variable at the right sideof a production

Examples:

A

aAbA

AbS

S

aSbS

Prof. Busch - LSU 2

A Non-Linear Grammar

bSaS

aSbS

S

SSS

Grammar :G

)}()(:{)( wnwnwGL ba

Number of in stringa w

Prof. Busch - LSU 3

Another Linear Grammar

Grammar :

AbB

aBA

AS

|

}0:{)( nbaGL nn

G

Prof. Busch - LSU 4

Right-Linear GrammarsAll productions have form:

Example:

xBA

xAor

aS

abSS

string of

terminals

Prof. Busch - LSU 5

Left-Linear GrammarsAll productions have form:

Example:

BxA

aB

BAabA

AabS

|

xAor

string of terminals

Prof. Busch - LSU 6

Regular Grammars

Prof. Busch - LSU 7

Regular GrammarsA regular grammar is any right-linear or left-linear grammar

Examples:

aS

abSS

aB

BAabA

AabS

|

1G 2G

Prof. Busch - LSU 8

ObservationRegular grammars generate regular

languages

Examples:

aS

abSS

aabGL *)()( 1

aB

BAabA

AabS

|

*)()( 2 abaabGL

1G

2G

Prof. Busch - LSU 9

Regular Grammars Generate

Regular Languages

Prof. Busch - LSU 10

Theorem

LanguagesGenerated byRegular Grammars

RegularLanguages

Prof. Busch - LSU 11

Theorem - Part 1

LanguagesGenerated byRegular Grammars

RegularLanguages

Any regular grammar generatesa regular language

Prof. Busch - LSU 12

Theorem - Part 2

LanguagesGenerated byRegular Grammars

RegularLanguages

Any regular language is generated by a regular grammar

Prof. Busch - LSU 13

Proof – Part 1

LanguagesGenerated byRegular Grammars

RegularLanguages

The language generated by any regular grammar is regular

)(GLG

Prof. Busch - LSU 14

The case of Right-Linear Grammars

Let be a right-linear grammar

We will prove: is regular

Proof idea: We will construct NFA with

G

)(GL

M)()( GLML

Prof. Busch - LSU 15

Grammar is right-linearG

Example:

aBbB

BaaA

BaAS

|

|

Prof. Busch - LSU 16

Construct NFA such thatevery state is a grammar variable:

M

aBbB

BaaA

BaAS

|

|

S FV

A

B

specialfinal state

Prof. Busch - LSU 17

Add edges for each production:

S FV

A

B

a

aAS

Prof. Busch - LSU 18

S FV

A

B

a

BaAS |

Prof. Busch - LSU 19

S FV

A

B

a

BaaA

BaAS

|

a

a

Prof. Busch - LSU 20

S FV

A

B

a

bBB

BaaA

BaAS

|

a

a

b

Prof. Busch - LSU 21

S FV

A

B

a

abBB

BaaA

BaAS

|

|

a

a

b

a

Prof. Busch - LSU 22

aaabaaaabBaaaBaAS

S FV

A

B

a

a

a

b

a

Prof. Busch - LSU 23

SFV

A

B

a

a

a

b

aabBB

BaaA

BaAS

|

|

G

M GrammarNFA

abaaaab

GLML

**

)()(

Prof. Busch - LSU 24

In GeneralA right-linear grammar

has variables:

and productions:

G

,,, 210 VVV

jmi VaaaV 21

mi aaaV 21

or

Prof. Busch - LSU 25

We construct the NFA such that:

each variable corresponds to a node:

M

iV

0V

FV

1V

2V

3V

4V specialfinal state

Prof. Busch - LSU 26

For each production:

we add transitions and intermediate nodes

jmi VaaaV 21

iV jV………

1a 2a ma

Prof. Busch - LSU 27

For each production:

we add transitions and intermediate nodes

mi aaaV 21

iV FV………

1a 2a ma

Prof. Busch - LSU 28

Resulting NFA looks like this:M

0V

FV

1V

2V

3V

4V

1a

3a

3a

4a

8a

2a 4a

5a

9a5a

9a

)()( MLGL It holds that:

Prof. Busch - LSU 29

The case of Left-Linear Grammars

Let be a left-linear grammar

We will prove: is regular

Proof idea: We will construct a right-linear grammar with

G

)(GL

G RGLGL )()(

Prof. Busch - LSU 30

Since is left-linear grammar

the productions look like:

G

kaaBaA 21

kaaaA 21

Prof. Busch - LSU 31

Construct right-linear grammar G

Leftlinear

G kaaBaA 21

G BaaaA k 12

BvA→

BvA R

Rightlinear

Prof. Busch - LSU 32

Construct right-linear grammar G

G kaaaA 21

G 12aaaA k

vA

RvA

Leftlinear

Rightlinear

Prof. Busch - LSU 33

It is easy to see that:

Since is right-linear, we have:

RGLGL )()(

)(GL RGL )(

G

)(GL

RegularLanguage

RegularLanguage

RegularLanguage

Prof. Busch - LSU 34

Proof - Part 2

LanguagesGenerated byRegular Grammars

RegularLanguages

Any regular language is generated by some regular grammar

LG

Prof. Busch - LSU 35

Proof idea:

Let be the NFA with . Construct from a regular grammar such that

Any regular language is generated by some regular grammar

LG

M )(MLL

M G)()( GLML

Prof. Busch - LSU 36

Since is regularthere is an NFA such that

LM )(MLL

Example:a

b

a

b*)*(* abbababL

)(MLL

M

1q 2q

3q

0q

Prof. Busch - LSU 37

Convert to a right-linear grammarM

a

b

a

b

M0q 1q 2q

3q10 aqq

Prof. Busch - LSU 38

a

b

a

b

M0q 1q 2q

3q

21

11

10

aqq

bqq

aqq

Prof. Busch - LSU 39

a

b

a

b

M0q 1q 2q

3q

32

21

11

10

bqq

aqq

bqq

aqq

Prof. Busch - LSU 40

a

b

a

b

M0q 1q 2q

3q

3

13

32

21

11

10

q

qq

bqq

aqq

bqq

aqq

G

LMLGL )()(

Prof. Busch - LSU 41

In General

For any transition:aq p

Add production: apq

variable terminal variable

Prof. Busch - LSU 42

For any final state: fq

Add production: fq

Prof. Busch - LSU 43

Since is right-linear grammar

is also a regular grammar

with

G

G

LMLGL )()(