0% found this document useful (0 votes)
76 views13 pages

Class5 Regular Grammars

The document discusses regular grammars and languages. Some key points: - Regular grammars are grammars that are either right-linear or left-linear. They generate regular languages. - Any regular language can be generated by a regular grammar. Conversely, any language generated by a regular grammar is regular. - The document proves these statements by showing how to construct an NFA from a regular grammar and a regular grammar from an NFA such that they both accept the same regular language.

Uploaded by

Navneet Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views13 pages

Class5 Regular Grammars

The document discusses regular grammars and languages. Some key points: - Regular grammars are grammars that are either right-linear or left-linear. They generate regular languages. - Any regular language can be generated by a regular grammar. Conversely, any language generated by a regular grammar is regular. - The document proves these statements by showing how to construct an NFA from a regular grammar and a regular grammar from an NFA such that they both accept the same regular language.

Uploaded by

Navneet Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Theorem:

R
The reverse L of a regular language
is a regular language

Reverse of a Regular Language


Proof idea:
Construct NFA that accepts

LR

invert the transitions of the NFA


that accepts L
1

Proof
Since L is regular,
there is NFA that accepts

L
Invert Transitions

Example:

L = ab * + ba

Make old initial state a final state

Add a new initial state

a
5

1
PDF created with pdfFactory Pro trial version www.pdffactory.com

Resulting machine accepts

LR

LR

is regular

Grammars

L = ab * + ba

LR = b * a + ab

Grammars

article a

Grammars express languages


Example:

article the

the English language

sentence noun _ phrase

noun boy

predicate

noun dog
noun _ phrase article

noun
verb runs

predicate verb

verb walks

A derivation of the boy walks:

sentence noun _ phrase

10

A derivation of a dog runs:

sentence noun _ phrase

predicate

predicate

noun _ phrase

verb

noun _ phrase

verb

article

verb

article

noun

verb

a noun

verb

noun

the noun

verb

the boy verb

a dog verb

the boy walks

a dog runs
11

12

2
PDF created with pdfFactory Pro trial version www.pdffactory.com

Notation
Language of the grammar:

noun boy

L = { a boy runs,
a boy walks,
the boy runs,
the boy walks,
a dog runs,
a dog walks,
the dog runs,
the dog walks }

noun dog
Variable
or
Non-terminal

Production
rule

Terminal

13

14

Another Example
Grammar:

Grammar:

S aSb
S

Derivation of sentence

ab :

Derivation of sentence

aabb :

S aSb aaSbb aabb

S aSb ab
S aSb

S aSb
S

S aSb

15

Other derivations:

16

Language of the grammar

S aSb aaSbb aaaSbbb aaabbb

S aSb aaSbb aaaSbbb

S aSb
S
L = {a nb n : n 0}

aaaaSbbbb aaaabbbb
17

18

3
PDF created with pdfFactory Pro trial version www.pdffactory.com

More Notation

Example

G = (V , T , S , P )

Grammar

Grammar

V:

Set of variables

T:

Set of terminal symbols

S:

Start variable

P:

Set of Production rules

G :

S aSb
S

G = (V , T , S , P )
V = {S }

T = {a, b}
P = {S aSb, S }

19

20

More Notation
Sentential Form:
A sentence that contains
variables and terminals

We write:

S aaabbb

Example:

S aSb aaSbb aaaSbbb aaabbb

Instead of:

S aSb aaSbb aaaSbbb aaabbb


Sentential Forms

sentence
21

In general we write:

If:

22

w1 wn

By default:

w w

w1 w2 w3 wn

23

24

4
PDF created with pdfFactory Pro trial version www.pdffactory.com

Example
Grammar

Example
Grammar

Derivations
*

S aSb
S

Derivations

S aSb
S

S
*

S aaSbb

S ab

aaSbb aaaaaSbbbbb

S aabb
*

S aaabbb
25

Another Grammar Example


Grammar G : S Ab
A aAb
A

26

More Derivations

S Ab aAbb aaAbbb aaaAbbbb


aaaaAbbbbb aaaabbbbb

S aaaabbbbb

Derivations:

S Ab
b
S Ab aAbb abb
S Ab aAbb aaAbbb aabbb

S aaaaaabbbbbbb

S a nb nb
27

28

Language of a Grammar

Example
For grammar

For a grammar G
with start variable

S Ab

G:

A aAb
A

L (G ) = {a nb nb : n 0}

L (G ) = {w : S w}

Since:

String of terminals
29

S a nb nb
30

5
PDF created with pdfFactory Pro trial version www.pdffactory.com

A Convenient Notation

A aAb
A

A aAb |

article a

Linear Grammars

article a | the

article the

31

32

Linear Grammars

A Non-Linear Grammar

Grammars with
at most one variable at the right side
of a production

S aSb
S

Examples:

Grammar

G:

S SS
S
S aSb
S bSa

S Ab
A aAb
A

L (G ) = {w : na ( w) = nb ( w)}
33

Number of

Another Linear Grammar

a in string w

Right-Linear Grammars
All productions have form:

Grammar

34

SA
A aB |
B Ab

A xB
or

A x
Example:

L (G ) = {a nb n : n 0}
35

S abS
S a

string of
terminals

36

6
PDF created with pdfFactory Pro trial version www.pdffactory.com

Left-Linear Grammars
All productions have form:

A Bx
or

A x
Example:

S Aab

Regular Grammars

string of
terminals

A Aab | B
Ba

37

38

Regular Grammars

Observation

A regular grammar is any


right-linear or left-linear grammar

Regular grammars generate regular languages


Examples:

Examples:

G1
S abS
S a

G2

G1

G2
S Aab
A Aab | B
Ba
39

S abS
S a

S Aab
A Aab | B
Ba

L (G1 ) = (ab) * a

L (G2 ) = aab( ab) *

40

Theorem
Languages
Generated by
Regular Grammars

Regular Grammars
Generate
Regular Languages

41

Regular
Languages

42

7
PDF created with pdfFactory Pro trial version www.pdffactory.com

Theorem - Part 1
Languages
Generated by
Regular Grammars

Theorem - Part 2

Languages
Generated by
Regular Grammars

Regular
Languages

Any regular grammar generates


a regular language

Regular
Languages

Any regular language is generated


by a regular grammar

43

Proof Part 1
Languages
Generated by
Regular Grammars

44

The case of Right-Linear Grammars


Let

Regular
Languages

be a right-linear grammar

We will prove:

The language L (G ) generated by


any regular grammar G is regular

Proof idea:

L(G )

is regular

We will construct NFA


with L ( M ) = L(G )

45

Grammar

G is right-linear

Example:

M
46

Construct NFA M such that


every state is a grammar variable:

S aA | B

A aa B
B b B|a

47

S aA | B
A aa B
B b B|a

VF

special
final state

48

8
PDF created with pdfFactory Pro trial version www.pdffactory.com

Add edges for each production:

VF

VF

S aA | B

S aA
49

50

a
S

VF

S aA | B
A aa B
51

S aA | B
A aa B
B bB

b
52

S aA | B
A aa B
B bB | a

a
a

VF

a
S

VF

VF

B
b

b
53

S aA aaaB aaabB aaaba

54

9
PDF created with pdfFactory Pro trial version www.pdffactory.com

NFA

has variables:

A aa B

A right-linear grammar

G
S aA | B

A
a

In General

Grammar

B
b

V0 ,V1,V2 ,

B bB | a

VF

Vi a1a2 amV j

and productions:

L ( M ) = L (G ) =

or

aaab * a + b * a

Vi a1a2 am
55

We construct the NFA


each variable

Vi

such that:

For each production:

corresponds to a node:

V1

56

Vi a1a2 amV j

we add transitions and intermediate nodes

V3
Vi

V0

a1

a2

am V
j

VF
V2

For each production:

V4

special
final state 57

Vi a1a2 am

58

Resulting NFA

looks like this:


a9

we add transitions and intermediate nodes


a1

Vi

a1

a2

am

V0

VF

V1

It holds that:

a4

V3

a5

a4

a3
V2

59

a2
a3
a5

a8
V4

L (G ) = L ( M )

a9

VF

60

10
PDF created with pdfFactory Pro trial version www.pdffactory.com

The case of Left-Linear Grammars


Let

Since G is left-linear grammar


the productions look like:

G be a left-linear grammar

We will prove:

L (G )

is regular

A Ba1a2 ak
A a1a2 ak

Proof idea:
We will construct a right-linear
grammar G with L (G ) = L(G) R
61

Construct right-linear grammar


Left
linear

Right
linear

62

Construct right-linear grammar

A Ba1a2 ak
A Bv

A ak a2 a1B
A vRB

Left
linear

Right
linear

A a1a2 ak
Av

A ak a2 a1
A vR

63

64

Proof - Part 2
It is easy to see that:
Since

L (G ) = L(G)

R
Languages
Generated by
Regular Grammars

G is right-linear, we have:

L (G)

L(G) R

L (G )

Regular
Language

Regular
Language

Regular
Language

Regular
Languages

Any regular language L is generated


by some regular grammar G

65

66

11
PDF created with pdfFactory Pro trial version www.pdffactory.com

Any regular language L is generated


by some regular grammar G

Since L is regular
there is an NFA M such that

L = L(M )

b
Example:

Proof idea:
Let

be the NFA with

L = L (M ).
G
67

q0

Construct from M a regular grammar


such that L ( M ) = L(G )

Convert

q1

q2

L = ab * ab(b * ab) *
L = L(M )

q3
68

to a right-linear grammar

M
M
q0

q1

q0 aq1

q0

q2

q0 aq1

q1 bq1

q1

q2
b

q3

q1 aq2

q3

69

70

L (G ) = L ( M ) = L
b

q0 aq1
q1 bq1
q1 aq2
q2 bq3

q0

q1

G
a

q0 aq1

q2

q1 bq1
q1 aq2

q2 bq3

q3

M
q0

q1

q2
b

q3

q3 q1
71

q3

72

12
PDF created with pdfFactory Pro trial version www.pdffactory.com

In General
a

For any transition:

Add production:

q ap

variable

For any final state:

terminal

Add production:

qf

variable
73

Since

qf

74

G is right-linear grammar
G is also a regular grammar
with

L (G ) = L ( M ) = L

75

13
PDF created with pdfFactory Pro trial version www.pdffactory.com

You might also like