Skip to content

Instantly share code, notes, and snippets.

@Saneyan
Created July 31, 2012 14:25
Show Gist options
  • Select an option

  • Save Saneyan/3217372 to your computer and use it in GitHub Desktop.

Select an option

Save Saneyan/3217372 to your computer and use it in GitHub Desktop.
Deterministic Finite Automation
var automaton = {
current: {
state: 0
}
, defaults: {
state: 0
}
, transition: [
{ "p": 1 }
, { "u": 2, "r": 6 }
, { "b": 3 }
, { "l": 4 }
, { "i": 5 }
, { "c": 11 }
, { "i": 7 }
, { "v": 8 }
, { "a": 9 }
, { "t": 10 }
, { "e": 11 }
, { " ": -1 }
]
, reset: function(){
for( var key in this.current )
this.current[ key ] = this.defaults[ key ];
}
, entry: function( inputStr ){
this.reset();
for( i = 0; inputStr.length > i; i++ ){
var res = this.accept( inputStr[ i ] );
if( res === 0 )
continue;
else if( res === 1 || inputStr.length - 1 > i )
return false;
else if( res === -1 )
return true;
}
return false;
}
, accept: function( inputChar ){
var next = this.transition[ this.current.state ][ inputChar ];
if( next !== undefined ){
if( next === -1 )
return -1;
this.current.state = next;
return 0;
}
return 1;
}
};
automaton.entry( 'public ' ); // true
automaton.entry( 'private ' ); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment