46
Theory of Computation Regular Grammars, Right Linear & Left Linear Grammars, Regular Grammars & Regular Languages Vladimir Kulyukin www.vkedco.blogspot.com

Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Embed Size (px)

Citation preview

Page 1: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Theory of Computation

Regular Grammars, Right Linear & Left Linear Grammars, Regular Grammars &

Regular Languages

Vladimir Kulyukin

www.vkedco.blogspot.com

Page 2: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Outline

● Grammars & Derivations● Regular Grammars● Right Linear Grammars● Left Linear Grammars● Regular Grammars & Regular Languages

Page 3: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

A Grammar: Formal Definition

disjoint. are and i.e. ,

. and

over strings are and where, form theof

is productioneach s;production ofset finite a is

symbol;start theis

alphabet; terminal theis

alphabet; lnontermina theis

where,,,, tuple-4 a is grammar A

VΣVΣ

εx VΣ

yx yx

P

VS

Σ

V

PSVGG

Page 4: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Grammars & Derivations

Page 5: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Grammar Example

.,,,,,,,,

:definition Formal

.|

|

:grammar theof form shorthand theis Here

.

:language for this

grammar a is Here .by defined language heConsider t **

XbXXXSaSSSbaXSG

bXX

XaSS

X

bXX

XS

aSS

ba

Page 6: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Definition

. with ends

and with starts that steps moreor zero of

derivation a is thereifonly and if

. and ,, 2)

,,,,, stringsexist there1)

ifonly and if ) derives (

*

*

z

w

zw

Pyxuyvzuxvw

xVvyxu

zwzw

Page 7: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example

a a a b

S

Page 8: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example

a a a b

S

S

Page 9: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example

a a a b

S

S

S

Page 10: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example

a a a b

S

S

S

S

Page 11: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example

a a a b

S

S

S

S

X

Page 12: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example

a a a b

S

S

S

S

X

X

Page 13: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example

a a a b ε

S

S

S

S

X

X

Page 14: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Language Generated by Grammar

.|Then

grammar. a be Let ** xSxGL

G

Page 15: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Derivation Example Formalized

4) (Rule

3) (Rule

2) (Rule

1) (Rule

1) (Rule

1) (Rule

.derivation a find try tousLet

. iff ,definitionBy ? Is

4) ;3) ; 2) ; )1

:reference of easefor out rules the writeusLet

.,,,,,,,,

Consider

*

aaab

aaabX

aaaX

aaaS

aaS

aSS

aaabSGLaaabGLaaab

XbXXXSaSS

XbXXXSaSSSbaXS

G

Page 16: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Regular Grammars

Page 17: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Regular Grammars

● Two types of regular grammars: Right linear grammars Left linear grammars

● They are called regular, because they generate regular languages

● Regular grammars can be viewed as a bridge from regular expressions to context-free grammars (or from finite state automata to stack machines)

Page 18: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Regular Linear Grammars &

Regular Languages

Page 19: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Right Linear Grammar

. and,, where

; 2.

; 1.

:forms two theof one has in productionevery

ifflinear right is ,,,grammar A

*Σz V YVX

zX

zYX

P

PSVG

Page 20: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Right Linear Grammars & Regular Languages

Theorem: If G is a right linear grammar, L(G) is regular.

Main Proof Insight: Construct an NFA M such that L(M) = L(G). How? For every type of production, add a corresponding path to the NFA.

Page 21: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Proof

For every production of the form X → a1a2a3...akY,

add the following transitions to the NFA:

a1 a2 a3 akX Y

Page 22: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Proof

If X → Y, then we add the following production:

X Yε

Page 23: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Proof

For every production of the form X → a1a

2a

3...a

k, add the following

transitions to the NFA. F is the symbol that we use to designate the final state.

a1 a2 a3 akX F

Page 24: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Proof

For every production of the form X → ε, no additional transitions need to be added to the NFA.

X

ε

X is the same as

Page 25: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example: Regular Grammar to NFA

● Consider the following regular grammar:

S 1S | 0T | εT 1T | 0UU 1U | 0S

Page 26: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

S

1

Modeling production S 1S

Page 27: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

S

1

Modeling production S 0T

0T

Page 28: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

S

1

- Modeling production S ε- S becomes a final state

0T

Page 29: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

S

1

Modeling production T 1T

0T

1

Page 30: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

S

1

Modeling production T 0U

0T

1

U0

Page 31: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

S

1

Modeling production U 1U

0T

1

U0

1

Page 32: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

S

1

Modeling production U 0S

0T

1

U0

1

0

Page 33: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Left Linear Grammars

Page 34: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Left Linear Grammar

. and,, where

; 2.

; 1.

:forms two theof one has in productionevery

ifflinear left is ,,,grammar A

*Σz V YVX

zX

YzX

P

PSVG

Page 35: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Left Linear Grammars &

Regular Languages

Page 36: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Left Linear Grammars & Regular Languages

Theorem: If G is a left linear grammar, then L(G) is regular.

Proof Insight: Convert G to a right linear grammar GR such that L(G) = [L(GR)]R.

Page 37: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Proof

● Given a left-linear grammar G, construct a right-linear grammar GR by reversing its rules

● L(GR) = LR

● Construct an NFA for GR with a single start state and a single final state (this is always possible with ε-moves)

● Reverse the NFA by reversing the direction of the arcs and making the old start state the new final state and the old final state the new start state

Page 38: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Language Reversals

.01,01

;110,011,010,10,011,110,010,01

:Examples

.| then language, a is If

.110,011

;,

:Examples

. of reversal theis then , If

**

*

R

R

RR

R

R

R

LL

LL

LxxLL

xx

baxabx

xxΣx

Page 39: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Back to Proof

. Thus,

. then , ifThen .'

production a has then , production a

has If .' production a has then

, form theof production a has If

RR

RR

RRR

R

RR

GLx x

GLxGLx zX

GzX

GYzXG

YzXG

Page 40: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Regular Grammars & Regular Languages

LanguagesRegular GrammarsRegular

Grammarslinear -Right Grammarslinear -Left

Page 41: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

From Regular Languages to Regular Grammars

Page 42: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

From Regular Languages to Regular Grammars

Theorem: If L is a regular language, there is a right linear grammar G such that L(G) = L.

Proof Insight: Take an NFA M such that L(M) = L and construct a right linear grammar G from its transitions such that L(G) = L.

Page 43: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Example

Q0 Q1 Q2

a c

b εNFA M

Q0 aQ0Q0 bQ1Q1 cQ1Q1 Q2Q2 ε

Right Linear Grammar G:

Page 44: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Proof

. iff , oflength on theinduction

By . production theadd , state finalevery For

.|...| sproduction theadd,1,,...,,

form theofsition every tranFor follows. as linear

right aconstruct We.such that NFA an be Let

11

GLxMLx x

qFq

ararqnrraq

G

LMLM

nn

Page 45: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

Regular Languages = Regular Grammars

GrammarsRegular LanguagesRegular

Therefore,

LanguagesRegular GrammarsReguar

GrammarsRegular LanguagesRegular

Page 46: Theory of Computation (Fall 2014): Regular Grammars & Regular Languages

References

● Davis, Weyuker, Sigal. Computability, Complexity, and Languages, 2nd Edition, Academic Press

● Brooks Webber. Formal Language: A Practical Introduction, Franklin, Beedle & Associates, Inc

● Hopcroft and Ullman. Introduction to Automata Theory, Languages, and Computation, Narosa Publishing House