jweinst1
5/21/2016 - 6:36 PM

a simple non-deterministic automata in js

a simple non-deterministic automata in js

//linked automata implementation

var LA = (function(){
	function LA(map, state){
		if(!(state in map)) throw "Unrecognized State";
		this.map = map;
		this.state = state;
	}
	LA.prototype.input = function(instruction){
		for(var key in this.map){
			if(instruction in this.map[key]){
				this.state = key;
				return true;
			}
		}
		return false;
	};
	return LA;
})();

//a sample map, that maps states to sets of input tokens, which allow state transitions
var SampleMap = {
	"a":{1:true, 2:true, 3:true},
	"b":{0:true, 3:true},
	"c":{4:true, 1:true}
};
/*  var a = new LA(SampleMap, "a");
   a
=> { map: 
   { a: { '1': true, '2': true, '3': true },
     b: { '0': true, '3': true },
     c: { '1': true, '4': true } },
  state: 'a' }
   a.input(2)
=> true
   a
=> { map: 
   { a: { '1': true, '2': true, '3': true },
     b: { '0': true, '3': true },
     c: { '1': true, '4': true } },
  state: 'a' }
   a.input(0)
=> true
   a
=> { map: 
   { a: { '1': true, '2': true, '3': true },
     b: { '0': true, '3': true },
     c: { '1': true, '4': true } },
  state: 'b' }
   */