theory of computation (fall 2014): regular grammars & regular languages
TRANSCRIPT
Theory of Computation
Regular Grammars, Right Linear & Left Linear Grammars, Regular Grammars &
Regular Languages
Vladimir Kulyukin
www.vkedco.blogspot.com
Outline
● Grammars & Derivations● Regular Grammars● Right Linear Grammars● Left Linear Grammars● 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
Grammars & Derivations
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
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
Derivation Example
a a a b
S
Derivation Example
a a a b
S
S
Derivation Example
a a a b
S
S
S
Derivation Example
a a a b
S
S
S
S
Derivation Example
a a a b
S
S
S
S
X
Derivation Example
a a a b
S
S
S
S
X
X
Derivation Example
a a a b ε
S
S
S
S
X
X
Language Generated by Grammar
.|Then
grammar. a be Let ** xSxGL
G
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
Regular Grammars
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)
Regular Linear 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
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.
Proof
For every production of the form X → a1a2a3...akY,
add the following transitions to the NFA:
…
a1 a2 a3 akX Y
Proof
If X → Y, then we add the following production:
X Yε
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
Proof
For every production of the form X → ε, no additional transitions need to be added to the NFA.
X
ε
X is the same as
Example: Regular Grammar to NFA
● Consider the following regular grammar:
S 1S | 0T | εT 1T | 0UU 1U | 0S
Example
S
1
Modeling production S 1S
Example
S
1
Modeling production S 0T
0T
Example
S
1
- Modeling production S ε- S becomes a final state
0T
Example
S
1
Modeling production T 1T
0T
1
Example
S
1
Modeling production T 0U
0T
1
U0
Example
S
1
Modeling production U 1U
0T
1
U0
1
Example
S
1
Modeling production U 0S
0T
1
U0
1
0
Left Linear Grammars
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
Left Linear 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.
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
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
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
Regular Grammars & Regular Languages
LanguagesRegular GrammarsRegular
Grammarslinear -Right Grammarslinear -Left
From Regular Languages to Regular Grammars
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.
Example
Q0 Q1 Q2
a c
b εNFA M
Q0 aQ0Q0 bQ1Q1 cQ1Q1 Q2Q2 ε
Right Linear Grammar G:
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
Regular Languages = Regular Grammars
GrammarsRegular LanguagesRegular
Therefore,
LanguagesRegular GrammarsReguar
GrammarsRegular LanguagesRegular
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