reverse of regular grammer
TRANSCRIPT
-
8/13/2019 Reverse of Regular Grammer
1/75
1
Reverse of a Regular Language
-
8/13/2019 Reverse of Regular Grammer
2/75
2
Theorem:
The reverse of a regular languageis a regular language
R
L L
Proof idea:
Construct NFA that accepts :RL
invert the transitions of the NFA
that accepts L
-
8/13/2019 Reverse of Regular Grammer
3/75
3
Proof
Since is regular,
there is NFA that accepts
L
Example:
baabL *
a
b
ba
L
-
8/13/2019 Reverse of Regular Grammer
4/75
4
Invert Transitions
a
b
ba
-
8/13/2019 Reverse of Regular Grammer
5/75
5
Make old initial state a final state
a
b
ba
-
8/13/2019 Reverse of Regular Grammer
6/75
6
Add a new initial state
a
b
ba
-
8/13/2019 Reverse of Regular Grammer
7/75
7
a
b
ba
Resulting machine accepts RL
baabL *
ababLR *
RL is regular
-
8/13/2019 Reverse of Regular Grammer
8/75
8
Grammars
-
8/13/2019 Reverse of Regular Grammer
9/75
9
Grammars
Grammars express languages
Example: the English language
verbpredicate
nounarticlephrasenoun
predicatephrasenounsentence
_
_
-
8/13/2019 Reverse of Regular Grammer
10/75
10walksverb
runsverb
dognounboynoun
thearticle
aarticle
-
8/13/2019 Reverse of Regular Grammer
11/75
11
A derivation of the boy walks:
walksboythe
verbboythe
verbnounthe
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
-
8/13/2019 Reverse of Regular Grammer
12/75
12
A derivation of a dog runs:
runsdoga
verbdoga
verbnouna
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
-
8/13/2019 Reverse of Regular Grammer
13/75
13
Language of the grammar:
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 }
-
8/13/2019 Reverse of Regular Grammer
14/75
14
Notation
dognoun
boynoun
Variable
orNon-terminal
TerminalProduction
rule
-
8/13/2019 Reverse of Regular Grammer
15/75
15
Another Example
Grammar:
Derivation of sentence :
S
aSbS
abaSbS
ab
aSbS S
-
8/13/2019 Reverse of Regular Grammer
16/75
16
aabbaaSbbaSbS
aSbS S
aabb
S
aSbSGrammar:
Derivation of sentence :
-
8/13/2019 Reverse of Regular Grammer
17/75
17
Other derivations:
aaabbbaaaSbbbaaSbbaSbS
aaaabbbbaaaaSbbbb
aaaSbbbaaSbbaSbS
-
8/13/2019 Reverse of Regular Grammer
18/75
18
Language of the grammar
S
aSbS
}0:{ nbaL nn
-
8/13/2019 Reverse of Regular Grammer
19/75
19
More Notation
Grammar PSTVG ,,,
:V
:T
:S
:P
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
-
8/13/2019 Reverse of Regular Grammer
20/75
20
Example
Grammar :
S
aSbSG
PSTVG ,,,
}{SV },{ baT
},{ SaSbSP
-
8/13/2019 Reverse of Regular Grammer
21/75
21
More Notation
Sentential Form:
A sentence that containsvariables and terminals
Example:
aaabbbaaaSbbbaaSbbaSbS
Sentential Forms sentence
-
8/13/2019 Reverse of Regular Grammer
22/75
22
We write:
Instead of:
aaabbbS
*
aaabbbaaaSbbbaaSbbaSbS
-
8/13/2019 Reverse of Regular Grammer
23/75
23
In general we write:
If:
nww
*
1
nwwww
321
-
8/13/2019 Reverse of Regular Grammer
24/75
24
By default: ww*
-
8/13/2019 Reverse of Regular Grammer
25/75
25
Example
S
aSbS
aaabbbS
aabbS
abS
S
*
*
*
*
Grammar Derivations
-
8/13/2019 Reverse of Regular Grammer
26/75
26
baaaaaSbbbbaaSbb
aaSbbS
S
aSbS
Grammar
Example
Derivations
-
8/13/2019 Reverse of Regular Grammer
27/75
27
Another Grammar Example
Grammar :
A
aAbA
AbS
Derivations:
aabbbaaAbbbaAbbS
abbaAbbAbSbAbS
G
-
8/13/2019 Reverse of Regular Grammer
28/75
28
More Derivations
aaaabbbbbaaaaAbbbbb
aaaAbbbbaaAbbbaAbbAbS
bbaS
bbbaaaaaabbbbS
aaaabbbbbS
nn
-
8/13/2019 Reverse of Regular Grammer
29/75
29
Language of a Grammar
For a grammarwith start variable :
GS
}:{)( wSwGL
String of terminals
-
8/13/2019 Reverse of Regular Grammer
30/75
30
Example
For grammar :
A
aAbA
AbS
}0:{)( nbbaGL nn
Since: bbaS nn
G
-
8/13/2019 Reverse of Regular Grammer
31/75
31
A Convenient Notation
A
aAbA|aAbA
thearticle
aarticle
theaarticle |
-
8/13/2019 Reverse of Regular Grammer
32/75
32
Linear Grammars
-
8/13/2019 Reverse of Regular Grammer
33/75
33
Linear Grammars
Grammars with
at most one variable at the right sideof a production
Examples:
AaAbA
AbS
S
aSbS
G
-
8/13/2019 Reverse of Regular Grammer
34/75
34
A Non-Linear Grammar
bSaS
aSbS
SSSS
Grammar :G
)}()(:{)( wnwnwGL ba
h L G
-
8/13/2019 Reverse of Regular Grammer
35/75
35
Another Linear Grammar
Grammar :
AbB
aBA
AS
|
}0:{)( nbaGL nn
G
Ri h Li G
-
8/13/2019 Reverse of Regular Grammer
36/75
36
Right-Linear Grammars
All productions have form:
Example:
xBA
xA
or
aS
abSS
L f Li G
-
8/13/2019 Reverse of Regular Grammer
37/75
37
Left-Linear Grammars
All productions have form:
Example:
BxA
aBBAabA
AabS
|
xA
or
-
8/13/2019 Reverse of Regular Grammer
38/75
38
Regular Grammars
R l G
-
8/13/2019 Reverse of Regular Grammer
39/75
39
Regular Grammars
A regular grammaris any
right-linear or left-linear grammar
Examples:
aS
abSS
aB
BAabA
AabS
|
1G 2G
Ob ti
-
8/13/2019 Reverse of Regular Grammer
40/75
40
Observation
Regular grammars generate regular languages
Examples:
aS
abSS
aabGL *)()(1
aB
BAabA
AabS
|
*)()(2
abaabGL
1G
2G
-
8/13/2019 Reverse of Regular Grammer
41/75
41
Regular Grammars
GenerateRegular Languages
Th
-
8/13/2019 Reverse of Regular Grammer
42/75
42
Theorem
LanguagesGenerated by
Regular Grammars
Regular
Languages
Th P t 1
-
8/13/2019 Reverse of Regular Grammer
43/75
43
Theorem - Part 1
LanguagesGenerated by
Regular Grammars
Regular
Languages
Any regular grammar generates
a regular language
Th P t 2
-
8/13/2019 Reverse of Regular Grammer
44/75
44
Theorem - Part 2
LanguagesGenerated by
Regular Grammars
Regular
Languages
Any regular language is generated
by a regular grammar
P f P t 1
-
8/13/2019 Reverse of Regular Grammer
45/75
45
Proof Part 1
LanguagesGenerated by
Regular Grammars
Regular
Languages
The language generated by
any regular grammar is regular
)(GL
G
Th s f Ri ht Li G mm s
-
8/13/2019 Reverse of Regular Grammer
46/75
46
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
)()( GLML
-
8/13/2019 Reverse of Regular Grammer
47/75
47
Grammar is right-linearG
Example:
aBbBBaaA
BaAS
|
|
-
8/13/2019 Reverse of Regular Grammer
48/75
48
Construct NFA such that
every state is a grammar variable:
aBbB
BaaABaAS
|
|
S FV
A
B
specialfinal state
-
8/13/2019 Reverse of Regular Grammer
49/75
49
Add edges for each production:
S FV
A
B
a
aAS
-
8/13/2019 Reverse of Regular Grammer
50/75
50
S FV
A
B
a
BaAS |
-
8/13/2019 Reverse of Regular Grammer
51/75
51
S FV
A
B
a
BaaA
BaAS
|
a
a
-
8/13/2019 Reverse of Regular Grammer
52/75
52
S FV
A
B
a
bBB
BaaABaAS
|
a
a
b
-
8/13/2019 Reverse of Regular Grammer
53/75
53
S FV
A
B
a
abBB
BaaABaAS
|
|
a
a
b
a
-
8/13/2019 Reverse of Regular Grammer
54/75
54
aaabaaabBaaaBaAS
S FV
A
B
a
a
a
b
a
GrammarNFA
-
8/13/2019 Reverse of Regular Grammer
55/75
55
SFV
A
B
a
a
a
b
aabBB
BaaA
BaAS
|
|
GGrammarNFA
abaaaab
GLML
**
)()(
In General
-
8/13/2019 Reverse of Regular Grammer
56/75
56
In General
A right-linear grammar
has variables:
and productions:
G
,,, 210 VVV
jmi VaaaV 21
mi aaaV 21
or
-
8/13/2019 Reverse of Regular Grammer
57/75
57
We construct the NFA such that:
each variable corresponds to a node:iV
0V
F
V
1V
2V
3V
4V special
final state
-
8/13/2019 Reverse of Regular Grammer
58/75
58
For each production:
we add transitions and intermediate nodes
jmi VaaaV 21
iV jV1a 2a ma
-
8/13/2019 Reverse of Regular Grammer
59/75
59
For each production:
we add transitions and intermediate nodes
mi aaaV 21
iV FV1a 2a ma
-
8/13/2019 Reverse of Regular Grammer
60/75
60
Resulting NFA looks like this:
0V
FV
1V
2
V
3V
4V
1a
3a3a
4a
8a
2a 4a
5a
9a5a
9a
)()( MLGL It holds that:
The case of Left-Linear Grammars
-
8/13/2019 Reverse of Regular Grammer
61/75
61
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 )()(
-
8/13/2019 Reverse of Regular Grammer
62/75
62
Since is left-linear grammar
the productions look like:
G
kaaBaA
21
kaaaA 21
-
8/13/2019 Reverse of Regular Grammer
63/75
63
Construct right-linear grammar G
In :G kaaBaA 21
In :G BaaaA k 12
vBA
BvA R
-
8/13/2019 Reverse of Regular Grammer
64/75
64
Construct right-linear grammar G
In :G kaaaA 21
In :G 12aaaA k
vA
RvA
-
8/13/2019 Reverse of Regular Grammer
65/75
65
It is easy to see that:
Since is right-linear, we have:
RGLGL )()(
)(GL RGL )(
G
)(GL
Regular
Language RegularLanguage RegularLanguage
Proof - Part 2
-
8/13/2019 Reverse of Regular Grammer
66/75
66
Proof Part 2
LanguagesGenerated by
Regular Grammars
Regular
Languages
Any regular language is generated
by some regular grammar
L
G
-
8/13/2019 Reverse of Regular Grammer
67/75
67
Proof idea:
Let be the NFA with .
Construct from a regular grammar
such that
Any regular language is generated
by some regular grammar
L
G
)(MLL
G)()( GLML
-
8/13/2019 Reverse of Regular Grammer
68/75
68
Since is regular
there is an NFA such that
L
)(MLL
Example: a
b
a
b*)*(* abbababL
)(MLL
1q 2q
3q
0q
-
8/13/2019 Reverse of Regular Grammer
69/75
69
Convert to a right-linear grammar
a
b
a
b
0q 1q 2q
3q
10 aqq
-
8/13/2019 Reverse of Regular Grammer
70/75
70
a
b
a
b
0q 1q 2q
3q
21
11
10
aqq
bqq
aqq
-
8/13/2019 Reverse of Regular Grammer
71/75
71
a
b
a
b
0q 1q 2q
3q
32
21
11
10
bqq
aqq
bqq
aqq
LMLGL )()(
-
8/13/2019 Reverse of Regular Grammer
72/75
72
a
b
a
b
0q 1q 2q
3q
3
13
32
21
11
10
q
qq
bqq
aqq
bqq
aqq
G
)()(
In General
-
8/13/2019 Reverse of Regular Grammer
73/75
73
In General
For any transition:
a
q p
Add production: apq
variable terminal variable
-
8/13/2019 Reverse of Regular Grammer
74/75
74
For any final state: fq
Add production: fq
-
8/13/2019 Reverse of Regular Grammer
75/75