summaryrefslogtreecommitdiff
path: root/javascripts/NfaSimulator.js
diff options
context:
space:
mode:
Diffstat (limited to 'javascripts/NfaSimulator.js')
-rw-r--r--javascripts/NfaSimulator.js54
1 files changed, 27 insertions, 27 deletions
diff --git a/javascripts/NfaSimulator.js b/javascripts/NfaSimulator.js
index b7a4473..0c29914 100644
--- a/javascripts/NfaSimulator.js
+++ b/javascripts/NfaSimulator.js
@@ -3,38 +3,37 @@
*
*/
function NfaSimulator(nfa) {
- this.startState = nfa.getStartState();
- this.acceptingState = nfa.getAcceptingState();
-}
+ this.startState = nfa.getStartState();
+ this.finalState = nfa.getFinalState();
+};
-NfaSimulator.prototype.getStartState = function() { return this.startState }
-NfaSimulator.prototype.getAcceptingState = function() { return this.acceptingState }
+NfaSimulator.prototype.getStartState = function() { return this.startState; };
+NfaSimulator.prototype.getFinalState = function() { return this.finalState; };
+//
NfaSimulator.prototype.simulate = function(word) {
- var a = '';
- var accepted = false;
+ var a, accepted = false;
this.p = new Stack();
this.q = new Stack();
this.getStartState().mark(true);
this.q.push(this.getStartState());
- word += REDELIMITER;
- for (var i = 0; i < word.length; i++) {
+ word += REDELIMITER;
+ for (var i = 0; i < word.length; i++) {
accepted = this.epsclosure();
a = word.substring(i, i+1);
this.move(a);
- }
+ };
return accepted;
-}
+};
+//
NfaSimulator.prototype.epsclosure = function() {
- var s = null;
- var t = null;
- var accepted = false;
+ var s, t, accepted = false;
while(!this.q.isEmpty()) {
s = this.q.pop();
this.p.push(s);
- accepted = accepted || s == this.getAcceptingState();
+ accepted = accepted || s == this.getFinalState();
if(s.symbol == EPSILON) {
for (var i = 0; i < 2; i++) {
@@ -43,23 +42,24 @@ NfaSimulator.prototype.epsclosure = function() {
if(!t.marked) {
t.mark(true);
this.q.push(t);
- }
- }
- }
- }
- }
+ };
+ };
+ };
+ };
+ };
return accepted;
-}
+};
-NfaSimulator.prototype.move = function(str) {
- var s = null;
+//
+NfaSimulator.prototype.move = function(symbol) {
+ var s;
while(!this.p.isEmpty()) {
s = this.p.pop();
s.mark(false);
- if (s.symbol == str) {
+ if (s.symbol == symbol) {
s.getFollowUp(0).mark(true);
this.q.push(s.getFollowUp(0));
- }
- }
-}
+ };
+ };
+};