FSA/Regular Sets/Grammars

Transcription

FSA/Regular Sets/Grammars
Equivalence of Regular Language
Representations
cs466(Prasad)
L14Equiv
1
Regular Languages: Grand Unification
L( NFA  s )  L( NFAs)
 L( DFAs )
(Collapsing graphs;
Structural Induction)
(S. Kleene’s work)
L( FA)  L( RE )
L( FA)  L( RE )
(Construction)
(Solving linear
equations)
L( FA)  L( RG )
L( RG )  L( RE )
cs466(Prasad)
(Parallel Simulation)
(Rabin and Scott’s work)
L14Equiv
2
Role of various representations
for Regular Languages
• Closure under complemention. (DFAs)
• Closure under union, concatenation, and Kleene
star. (NFA-s, Regular expression.)
• Consequence:
Closure under intersection by De Morgan’s Laws.
• Relationship to context-free languages. (Regular
Grammars.)
• Ease of specification. (Regular expression.)
• Building tokenizers/lexical analyzers. (DFAs)
cs466(Prasad)
L14Equiv
3
Application to Scanner (Lexer, Tokenizer)
• High-level view
NFA
Regular
expressions
DFA
Lexical
Specification
Table-driven
Implementation of a minimal DFA
cs466(Prasad)
L14Equiv
4
Construction of Finite Automata from Regular
Expressions
L( RE )  L( FA)
Show that there are FA for basis elements and
there exist constructions on FA for capturing
union, concatenation, and Kleene star operations.
M(a)
cs466(Prasad)
L14Equiv
Basis Case
5
Constructions on NFA-s


M(R1)


M(R1)


M(R2)
M(R*)
M(R1 U R2)

M(R)

M(R2)

M(R1 R2)
cs466(Prasad)
L14Equiv
6
Construction of Regular Expression from
Finite Automaton
• Expression Graph is a labeled directed
graph in which the arcs are labeled by
regular expressions. An expression graph,
like a state diagram, contains a
distinguished start node and a set of
accepting nodes.
cs466(Prasad)
L14Equiv
7
Examples
ab
L(M) = (ab)*
cs466(Prasad)
L14Equiv
8
Examples
b+ a
ba
aub
L(M) = (b+ a)* (a u b) (ba)*
cs466(Prasad)
L14Equiv
9
Examples
ba
bb
b*
a+
L(M) = (b a)* b*( bb u (a+(ba)*b*) )*
cs466(Prasad)
L14Equiv
10
Main Idea
• To associate an RE with an FA,
– reduce an arbitrary expression graph to one
containing at most two nodes,
– by repeatedly removing nodes from the graph
and relabeling the arcs to preserve the language.
• Without loss of generality, we can assume
one accepting state (because of the presence
of the union operation).
cs466(Prasad)
L14Equiv
11
Example
qj
qi
qk
Wi,k
Wj,i
qj
qk
Wj,i Wi,k
cs466(Prasad)
L14Equiv
12
Wi,i
qj
qk
Wi,k
Wj,i
qi
qj
qk
Wj,i (Wi,i)* Wi,k
cs466(Prasad)
L14Equiv
13
Final Graph : Alternative 1
u
L(M) = (u)*
cs466(Prasad)
L14Equiv
14
Final Graph : Alternative 2
u
w
v
x
L(M) = (u)* v( w u (x (u)* v) )*
cs466(Prasad)
L14Equiv
15
Detailed Example
b
q0
q1
a
b
a
b
a
q2
q3
b
cs466(Prasad)
L14Equiv
16
Delete node q1
b
q0
b
ab a
q1
bb a
b
a
q2
q3
b
cs466(Prasad)
L14Equiv
17
Delete node q2
ab*ab
q0
ab
b u bb
a
a
q2
q3
b
cs466(Prasad)
L14Equiv
18
Finally
ab*ab
q0
b u bb
a
q3
(ab*ab)*a ((bubb) (ab*ab)*a)*
cs466(Prasad)
L14Equiv
19
• For precise details, see Algorithm 6.2.2 on
Page 194 in Sudkamp’s Languages and
Machines, 3rd Edition.
cs466(Prasad)
L14Equiv
20
From Regular Expression to NFA to DFA to
Regular Grammars
Via Examples
cs466(Prasad)
L14Equiv
21
Exercise
• Construct a DFA for a+b+
q0
a
cs466(Prasad)
a
q1
b
q2
b
L14Equiv
22
Equivalent DFA
a
a
b
{q0,q1}
b
{q0}
a
b
{q1,q2}
{}
a,b
cs466(Prasad)
L14Equiv
23
Two Equivalent (Right-linear) Regular Grammars
<q0> ->
|
<q1> ->
|
<q2> ->
a <q0>
a <q1>
b <q1>
b <q2>
λ
<{q0}> -> a <{q0,q1}>
<{q0,q1}> ->
a <{q0,q1}>
| b <{q1,q2}>
<{q1,q2}> -> λ
| b <{q1,q2}>
• All productions involving <{}>
can be deleted, as <{}> does
not derive any terminal strings.
cs466(Prasad)
L14Equiv
24
Two Equivalent (Left-linear) Regular Grammars
<q0> -> λ
| <q0> a
<q1> -> <q1> b
| <q0> a
<q2> -> <q1> b
cs466(Prasad)
<{q0}> -> λ
<{q0,q1}> ->
<{q0,q1}> a
| <{q0}> a
<{q1,q2}> ->
| <{q0,q1}> b
| <{q1,q2}> b
L14Equiv
25
From Grammars to Finite Automata
S
A
B
F
S -> aA | c
A -> bB | bA
B -> λ
b
a
S
c
cs466(Prasad)
A
->
->
->
->
aA | cF
bB | bA
λ
λ
b
B
F
L14Equiv
26
From Grammars to Finite Automata
S
A
B
F
Z
S -> aA | c
A -> bB | bA
B -> λ
b
a
S
c
cs466(Prasad)
A
->
->
->
->
->
λ
Sa | Ab
Ab
Sc
B | F
b
B
F
L14Equiv
27