0% found this document useful (0 votes)
4 views68 pages

Class 5

theory ao automata

Uploaded by

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

Class 5

theory ao automata

Uploaded by

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

Grammars

1
Grammars
Grammars express languages

Example: the English language

sentence  noun _ phrase predicate

noun _ phrase  article noun

predicate  verb
2
article  a
article  the

noun  cat
noun  dog

verb  runs
verb  walks

3
A derivation of “the dog walks”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 the noun verb
 the dog verb
 the dog walks

4
A derivation of “a cat runs”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 a noun verb
 a cat verb
 a cat runs

5
Language of the grammar:

L = { “a cat runs”,
“a cat walks”,
“the cat runs”,
“the cat walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
6
Notation

Production Rules

noun  cat
noun  dog

Variable Terminal

7
Another Example
Grammar: S  aSb
S

Derivation of sentenceab :

S  aSb  ab

S  aSb S
8
Grammar: S  aSb
S

Derivation of sentence aabb :

S  aSb  aaSbb  aabb

S  aSb S
9
Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb

10
Language of the grammar

S  aSb
S

n n
L {a b : n 0}

11
More Notation

Grammar G V , T , S , P 
V : Set of variables

T : Set of terminal symbols


S : Start variable

P : Set of Production rules


12
Example

GrammarG : S  aSb
S

G V , T , S , P 

V {S } T {a, b}
P {S  aSb, S  }
13
More Notation
Sentential Form:
A sentence that contains
variables and terminals

Example:

S  aSb  aaSbb  aaaSbbb  aaabbb

Sentential Forms sentence


14
*
We write: S  aaabbb

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

15
*
In general we write: w1  wn

If: w1  w2  w3    wn

16
*
By default: w  w

17
Example
Grammar Derivations
S  aSb *
S 
S
*
S  ab
*
S  aabb
*
S  aaabbb
18
Example
Grammar Derivations
S  aSb 
S  aaSbb
S

aaSbb  aaaaaSbbbbb

19
Another Grammar Example
Grammar G : S  Ab
A  aAb
A 

Derivations:
S ⇒ Ab ⇒ b
S ⇒ Ab ⇒ aAbb ⇒ abb
S ⇒ Ab ⇒ aAbb ⇒ aaAbbb ⇒ aabbb
20
More Derivations
S  Ab  aAbb  aaAbbb  aaaAbbbb
 aaaaAbbbbb  aaaabbbbb


S  aaaabbbbb

S  aaaaaabbbbbbb

n n
S a b b
21
Language of a Grammar

For a grammarG
with start variable S :


L(G ) {w : S  w}

String of terminals
22
Example
For grammarG : S  Ab
A  aAb
A 

n n
L(G ) {a b b : n 0}


n n
Since: S a b b
23
A Convenient Notation

A  aAb
A  aAb | 
A 

article  a
article  a | the
article  the
24
Linear Grammars

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

Examples: S  aSb S  Ab
S A  aAb
A 

26
A Non-Linear Grammar

Grammar G : S  SS
S
S  aSb
S  bSa

L(G ) {w : na ( w) nb ( w)}

Number of a in string w
27
Another Linear Grammar

Grammar G : S A
A  aB | 
B  Ab

n n
L(G ) {a b : n 0}

28
Right-Linear Grammars
All productions have form: A  xB
or
A x

Example: S  abS string of


S a terminals

29
Left-Linear Grammars
All productions have form: A  Bx
or
A x

Example: S  Aab string of


A  Aab | B terminals

B a
30
Regular Grammars

31
Regular Grammars
A regular grammar is any
right-linear or left-linear grammar

Examples:
G1 G2
S  abS S  Aab
S a A  Aab | B
B a
32
Observation
Regular grammars generate regular
languages

G2
Examples:
G1 S  Aab
S  abS A  Aab | B
S a B a

L(G1 ) (ab) * a L(G2 ) aab(ab) *


33
Regular Grammars
Generate
Regular Languages

34
Theorem

Languages
Generated by
Regular Grammars
 Regular
Languages

35
Theorem - Part 1

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular grammar generates


a regular language

36
Theorem - Part 2

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular language is generated


by a regular grammar

37
Proof – Part 1

Languages
Generated by  Regular
Languages
Regular Grammars

The language L(G ) generated by


any regular grammar G is regular

38
The case of Right-Linear Grammars

Let G be a right-linear grammar

We will prove: L(G ) is regular

Proof idea: We will construct NFAM


with L( M ) L(G )
39
Grammar G is right-linear

Example: S  aA | B
A  aa B
B b B|a

40
Construct NFA M such that
every state is a grammar variable:

A
S special
VF
final state
B
S  aA | B
A  aa B
B b B|a 41
Add edges for each production:

a A
S VF
B

S  aA

42
a A
S VF

B

S  aA | B

43
A
a a

S a VF

B
S  aA | B
A  aa B
44
A
a a

S a VF

B
S  aA | B
b
A  aa B
B  bB 45
A
a a

S a VF
 a
B
S  aA | B
b
A  aa B
B  bB | a 46
A
a a

S a VF
 a
B

S  aA  aaaB  aaabB  aaaba


47
NFA M Grammar
A G
a
S  aA | B
a
A  aa B
S a
B  bB | a
 VF
a
B
L( M ) L(G ) 
b
aaab * a  b * a
48
In General
A right-linear grammarG

has variables: V0 ,V1,V2 ,

and productions: Vi  a1a2  amV j


or

Vi  a1a2  am
49
We construct the NFA M such that:

each variable Vi corresponds to a


node:
V1 V3
V0
VF
V2
V4 special
final state
50
For each production:Vi  a1a2  amV j

we add transitions and intermediate


nodes

Vi a1 a2 ……
am V
j

51
For each production:Vi  a1a2  am

we add transitions and intermediate


nodes

Vi a1 a2 ……
am
VF

52
Resulting NFA M looks like this:
a9

a2 a4
a1 V1 V3
a3 a5
V0
a3 a4
VF
a8 a9
V2 a5
V4

It holds that: L(G ) L( M )


53
The case of Left-Linear Grammars

Let G be a left-linear grammar

We will prove: L(G ) is regular

Proof idea:
We will construct a right-linear
 R
grammarG with L(G ) L(G)
54
Since G is left-linear grammar
the productions look like:

A  Ba1a2  ak

A  a1a2  ak

55
Construct right-linear grammarG
Left A  Ba1a2  ak
G
linear
A → Bv

Right A  ak  a2 a1B
G
linear
R
A v B
56
Construct right-linear grammarG
Left A  a1a2  ak
G
linear
A v

Right
G  A  ak  a2 a1
linear
R
A v
57
R
It is easy to see that: L(G ) L(G)

Since G is right-linear, we have:

R
L(G) L(G) L(G )
Regular Regular Regular
Language Language Language

58
Proof - Part 2

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular language L is generated


by some regular grammar G

59
Any regular language L is generated
by some regular grammar G

Proof idea:
Let M be the NFA with L L(M ) .

Construct from M a regular G


grammar L( M ) L(G )
such that 60
Since L is regular
there is an NFA M such that L L(M )
b
Example:
M a
a
q0 q1 q2
 b
L ab * ab(b * ab) * q3
L L(M )
61
Convert M to a right-linear grammar
b

M a
a
q0 q1 q2

 b
q0  aq1
q3

62
b

M a
a
q0 q1 q2
q0  aq1 b

q1  bq1 q3
q1  aq2

63
b

M a
a
q0 q1 q2
q0  aq1
q1  bq1  b

q1  aq2 q3

q2  bq3

64
L(G ) L( M ) L
G b

q0  aq1 M a
a
q0 q1 q2
q1  bq1
q1  aq2  b

q2  bq3 q3

q3  q1
q3  
65
In General
a
For any transition: q p

Add production: q  ap

variable terminal variable


66
For any final state: qf

Add production: qf  

67
Since G is right-linear grammar

G is also a regular grammar

with L(G ) L( M ) L

68

You might also like