private FSM <int> ParseSet() { List <int> charset = new List <int>(); bool negate = false; if (this.Pattern[_parseIndex] == '^') { negate = true; _parseIndex++; } while (this.Pattern[_parseIndex] != ']') { switch (this.Pattern[_parseIndex]) { case '\\': _parseIndex++; charset.Add(this.Pattern[_parseIndex++]); break; case '-': _parseIndex++; int start = charset.Last(); charset.RemoveAt(charset.Count - 1); for (int c = start; c <= this.Pattern[_parseIndex]; c++) { charset.Add(c); } _parseIndex++; break; default: charset.Add(this.Pattern[_parseIndex++]); break; } } //consume closing set _parseIndex++; FSM <int> result = new NFSM <int>(-1); for (int i = 0; i < 256; i++) { if (i != (int)'\\') { if ((!negate && charset.Contains(i)) || (negate && !charset.Contains(i))) { result = result.Or(i); } } } return(result); }
private FSM <int> AnyChar() { FSM <int> result = new NFSM <int>(-1); for (int i = 0; i < 256; i++) { result = result.Or(i); } return(result); }
public override FSM <T> And(T symbol) { FSM <T> andvar = new NFSM <T>(this.Epsilon, symbol); foreach (string label in this.AcceptingLabels) { andvar.AcceptingStates.Last().Value.Add(label); } return(this.And(andvar)); }
public override FSM <T> Or(FSM <T> fsm) { NFSM <T> result = (NFSM <T>) this.Clone(); if (this._transitions[0].Count == 0) { return(fsm); } int offset = result._transitions.Keys.OrderBy(x => x).Last(); foreach (int state in fsm._transitions.Keys) { foreach (T symbol in fsm._transitions[state].Keys) { int startstate = (state != 0) ? state + offset : 0; if (!result._transitions.ContainsKey(startstate)) { result._transitions[startstate] = new Dictionary <T, List <int> >(); } if (!result._transitions[startstate].ContainsKey(symbol)) { result._transitions[startstate][symbol] = new List <int>(); } foreach (int target in fsm._transitions[state][symbol]) { int endstate = target + offset; if (fsm.AcceptingStates.ContainsKey(target)) { endstate = result.AcceptingStates.Keys.Last(); } result._transitions[startstate][symbol].Add(endstate); if (!result._transitions.ContainsKey(endstate)) { result._transitions[endstate] = new Dictionary <T, List <int> >(); } if (fsm.AcceptingStates.ContainsKey(target)) { if (!result.AcceptingStates.ContainsKey(endstate)) { result.AcceptingStates[endstate] = new List <string>(); } result.AcceptingStates[endstate].AddRange(fsm.AcceptingStates[target].Except(result.AcceptingStates[endstate])); } } } } return(result); }
public override FSM <T> And(FSM <T> fsm) { NFSM <T> result = (NFSM <T>) this.Clone(); result.AcceptingStates.Clear(); if (this._transitions[0].Count == 0) { return(fsm); } for (int i = 0; i < this.AcceptingStates.Count; i++) { int offset = result.States.Last(); foreach (int state in fsm._transitions.Keys) { foreach (T symbol in fsm._transitions[state].Keys) { int startstate = state + offset; if (state == 0) { startstate = this.AcceptingStates.Keys.ToArray()[i]; } if (!result._transitions.ContainsKey(startstate)) { result._transitions[startstate] = new Dictionary <T, List <int> >(); } result._transitions[startstate][symbol] = new List <int>(); foreach (int target in fsm._transitions[state][symbol]) { int endstate = target + offset; if (target == 0) { endstate = this.AcceptingStates.Keys.ToArray()[i]; } result._transitions[startstate][symbol].Add(endstate); if (!result._transitions.ContainsKey(endstate)) { result._transitions[endstate] = new Dictionary <T, List <int> >(); } if (fsm.AcceptingStates.ContainsKey(target)) { result.AcceptingStates[endstate] = new List <string>(fsm.AcceptingStates[target]); } } } } } return(result); }
public NFSM(NFSM <T> nfsm) : this(nfsm.Epsilon) { foreach (int state in nfsm._transitions.Keys) { this._transitions[state] = new Dictionary <T, List <int> >(); foreach (T symbol in nfsm._transitions[state].Keys) { this._transitions[state][symbol] = new List <int>(nfsm._transitions[state][symbol]); } } this.AcceptingStates = new Dictionary <int, List <string> >(); foreach (KeyValuePair <int, List <string> > acc in nfsm.AcceptingStates) { this.AcceptingStates[acc.Key] = new List <string>(acc.Value); } }
public object Clone() { NFSM <T> result = new NFSM <T>(this.Epsilon); foreach (KeyValuePair <int, Dictionary <T, List <int> > > i in this._transitions) { result._transitions[i.Key] = new Dictionary <T, List <int> >(); foreach (KeyValuePair <T, List <int> > j in i.Value) { result._transitions[i.Key][j.Key] = new List <int>(this._transitions[i.Key][j.Key]); } } foreach (KeyValuePair <int, List <string> > i in this.AcceptingStates) { result.AcceptingStates[i.Key] = new List <string>(this.AcceptingStates[i.Key]); } return(result); }
public override FSM <T> Kleene() { NFSM <T> result = (NFSM <T>) this.Clone(); NFSM <T> eps = new NFSM <T>(this.Epsilon, this.Epsilon); eps.AcceptingStates.Clear(); eps.AcceptingStates[1] = new List <string>(this.AcceptingLabels); result = (NFSM <T>)eps.And(result).And(eps).Or(eps); foreach (int acc in result.AcceptingStates.Keys) { if (!result._transitions[acc].ContainsKey(this.Epsilon)) { result._transitions[acc][this.Epsilon] = new List <int>(); } result._transitions[acc][this.Epsilon].Add(1); } return(result); }
private FSM <int> ConcatRegex() { FSM <int> result = new NFSM <int>(-1); List <FSM <int> > temp = new List <FSM <int> >(); while (_parseIndex < this.Pattern.Length) { switch (this.Pattern[_parseIndex]) { case '|': case ')': return(temp.Aggregate(new NFSM <int>(-1), (a, b) => (NFSM <int>)a.And(b))); case '*': _parseIndex++; temp[temp.Count - 1] = temp[temp.Count - 1].Kleene(); break; case '+': _parseIndex++; temp[temp.Count - 1] = temp[temp.Count - 1].And(temp[temp.Count - 1].Kleene()); break; case '?': _parseIndex++; FSM <int> next = new NFSM <int>(-1, -1); temp[temp.Count - 1] = temp[temp.Count - 1].Or(next); break; default: temp.Add(BasicRegex()); break; } } return(temp.Aggregate(new NFSM <int>(-1), (a, b) => (NFSM <int>)a.And(b))); }