Skip to content

Instantly share code, notes, and snippets.

@v2e4lisp
Created December 6, 2012 03:36
Show Gist options
  • Save v2e4lisp/4221607 to your computer and use it in GitHub Desktop.
Save v2e4lisp/4221607 to your computer and use it in GitHub Desktop.
javascript string match automata. (tobefixed)
var string_automata = function (string) {
var length = string.length;
var init = string[0];
var automata = [];
automata[0][init] = [1];
// var get_state_by_id = function (id) {
// return automata[id];
// }
var update_next_state = function (input, cur_id) {
if (cur_id === length) {
return true;
}
var cur_state = automata[cur_id];
var next_list = [];
if (!cur_state[input]) {
return [0];
}
cur_state[input].forEach (function (i) {
if (automata[i][input]) {
next_list.concat (automata[i][input]);
}
});
if (input === init) {
next_list.push(1)
}
next_list.push(cur_id+2);
automata[cur_id+1][input] = new_list;
return false
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment