Date post: | 06-May-2015 |
Category: |
Technology |
Upload: | vladimir-kulyukin |
View: | 672 times |
Download: | 1 times |
Theory of Computation
Closure Properties of Regular
Languages, DFA and NFA
Implementation
Vladimir Kulyukin
http://www.vkedco.blogspot.com/
Outline
• Closure Properties of Regular Languages
• DFA Implementation
• NFA Implementation
Closure Properties of Regular
Languages
Definition: Language Complement
.|
is of complement the
,over language a is If
* LxxL
L
L
Example
q0
q1
q2
0
0,1
0,1 L is the language over {0, 1} that
starts with 0.
1
q0
q1
q2
0,1
0,1
1
0
The complement of L is the
language of strings over {0, 1} that
start with 1 and the empty string.
Closure under Complement
regular. also is then language,regular a is If :Theorem LL
. ifonly and if ,any for Then,
.,,,,
DFA new a definecan We
.such that ,,,,
DFA a is thereregular, a is If :Proof
*
0
0
MLx
MLxx
FQqQM
MLLFqQM
L
Definition: Language Intersection
. & |
then languages, are and If
21
*
21
21
LxLxxLL
LL
Closure under Intersection
regular. also is
then languages,regular are and If :Theorem 2121 LLLL
.,,
,,,
, and ,
,any For .,,
where,,,,,,
where,,,,,,
DFA new aconstruct We.
such that ,,,, and
such that ,,,, DFAs two
are thereregular, are and If :Proof
212100
*
3
210
*
20
*
1
20
*
210
*
1
21
*
213
210033
22
202211
1011
21
LLxFFxrq
FFxrxq
FxrFxq
LLxxaRrQq
araqarq
FFrqRQM
MLL
FrRMMLL
FqQM
LL
Definition: Language Union
.or |
then languages, are and If
21
*
21
21
LxLxxLL
LL
Closure under Union
regular. also is
then languages,regular are and If :Theorem 2121 LLLL
regular. is
on,intersecti and complementunder closed are
languagesregular .areSince
then regular, are and If :1 Proof
21
2121
21
LL
LLLL
LL
Closure under Union
regular. also is
then languages,regular are and If :Theorem 2121 LLLL
. are states final
theunion, for the DFA theof states final for the
except on,intersectiunder closure of proof in the
ason constructi same theusecan We:2 Proof
21 FQRF
DFA Implementation
Problem
Show that the language of binary
representations of numbers divisible by 3 is
regular.
DFA
q0 q1 q2
0
1
0
Q3 is the sink state where the automaton stays as soon as a symbol
different from 0 or 1 is seen.
1
0
1
q3 0, 1
not 0 or 1
not 0 or 1
not 0 or 1
DFA
q0 q1 q2
0
1
0
This DFA does the job but does not handle symbols outside of {0, 1}. We
need to add an error state where the DFA goes and stays as soon as it
sees a symbol other than 0 or 1.
1
0
1
Two Implementation Approaches
• There are two approaches used in implementing
both DFAs and NFAs: implement delta as a
function and implement delta as a table
• Implementing delta as a function is more
understandable and sometimes more efficient if
the FA is a weakly connected graph
• Implementing delta as a table is more common
because it tables can be constructed offline
read in at run time
Defining States
public class Mod3DFA {
static final int Q0 = 0;
static final int Q1 = 1;
static final int Q2 = 2;
static final int Q3 = 3;
private static int mCurrState;
…
}
Defining Delta private static int delta(int state, char c) {
switch ( state ) {
case Q0: switch ( c ) {
case '0': return Q0;
case '1': return Q1;
default: return Q3;
}
case Q1: switch ( c ) {
case '0': return Q2;
case '1': return Q0;
default: return Q3;
}
case Q2: switch ( c ) {
case '0': return Q1;
case '1': return Q2;
default: return Q3;
}
default: return Q3;
}
}
Resetting, Processing, Accepting
static void reset() {
mCurrState = Q0;
}
static void process(String input) {
for(int i = 0; i < input.length(); i++) {
mCurrState = delta(mCurrState, input.charAt(i));
}
}
static boolean isAccepted() {
return mCurrState == Q0;
}
Defining String Filter
static void mod3StringFilter() {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String inputStr = input.readLine();
while ( inputStr != null ) {
Mod3DFA.reset();
Mod3DFA.process(inputStr);
if ( Mod3DFA.isAccepted() )
System.out.println("YES");
else
System.out.println("NO");
inputStr = input.readLine();
}
}
catch ( IOException ex ) {
System.err.println(ex.toString());
}
}
Using String Filter
public static void main(String[] argv) {
Mod3DFA.mod3StringFilter();
}
Defining States
public class Mod3DFA {
static final int Q0 = 0;
static final int Q1 = 1;
static final int Q2 = 2;
static final int Q3 = 3;
private static int mCurrState;
…
}
Defining Delta
static int[][] deltaTable =
{
{Q0, Q1}, // state Q0: delta('0', Q0) = Q0; delta('1', Q1)
{Q2, Q0}, // state Q1: delta('0', Q1) = Q2; detal('1', Q0)
{Q1, Q2}, // state Q2: delta('0', Q2) = Q1; delta('1', Q2)
{Q3, Q3} // state Q3: delta('0', Q3) = Q3; delta('1', Q3)
};
Resetting, Processing, Accepting
static void reset() { mCurrState = Q0; }
static void process(String input) {
for(int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
try { // Table lookup is based on the following Java trick:
// System.out.println('1'-'0'); // this outputs integer 1
// System.out.println('1'-'1'); // this outputs integer 0
mCurrState = deltaTable[mCurrState][c-'0'];
}
catch ( ArrayIndexOutOfBoundsException ex ) {
mCurrState = Q3;
} } }
static boolean isAccepted() { return mCurrState == Q0; }
Defining String Filter
// same as in function implement
static void mod3StringFilter() {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String inputStr = input.readLine();
while ( inputStr != null ) {
Mod3DFA.reset();
Mod3DFA.process(inputStr);
if ( Mod3DFA.isAccepted() )
System.out.println("YES");
else
System.out.println("NO");
inputStr = input.readLine();
}
}
catch ( IOException ex ) {
System.err.println(ex.toString());
}
}
Using String Filter
// same as in function implementation
public static void main(String[] argv) {
Mod3DFA.mod3StringFilter();
}
Sketchy NFA Implementation
Defining Symbols
public class Symbol {
public String name; // C-style structure
public Symbol() { name = ""; }
public Symbol(String n) { name = n; }
@Override
public String toString() {
return name;
}
}
Defining Symbols
public class EpsilonSymbol {
public static final Symbol epsilon = new Symbol("epsilon");
public EpsilonSymbol() {}
}
Defining States
public class FSAState {
public Integer id;
public FSAState(int id) {
this.id = new Integer(id);
}
public FSAState(Integer id) {
this.id = new Integer(id.intValue());
}
@Override
public String toString() {
return new String("q" + id.toString());
}
}
Defining Alphabet
• Sigma is a map that maps Symbol.name
(String) to Symbol objects
import java.util.TreeMap;
TreeMap<String, Symbol> sigma;
Defining State Sets
import java.util.ArrayList;
private ArrayList<FSAState> states;
private ArrayList<FSAState> finalStates;
private FSAState startState;
Defining State Sets
• Suppose an NFA in state 1 transitions to
states {2, 3} on a and to states {4, 5} on b
• Here is a way to implement this:
TreeMap<Integer, TreeMap<String, ArrayList<FSAState>> deltaTable;
References
• A. Brooks Weber. Formal Language: A
Practical Introduction. Franklin, Beedle &
Associates.
• M. Davis, R. Sigal, E. Weyuker.
Computability, Complexity, & Languages:
Fundamentals of Theorectical Computer
Science. Academic Press.
http://www.vkedco.blogspot.com/