private bool Equals(Prodc prodc) { bool eq = _right.Count == prodc._right.Count && _left == prodc._left; if (eq) { for (int i = 0; i < _right.Count; ++i) { eq &= _right[i] == prodc._right[i]; } } return(eq); }
private DFA BuildDFA() { Prodc start = _prodcs.First(e => e._left == _start); DFA dfa = new DFA(new State(0, _prodcs, new HashSet <Deriv>() { new Deriv(start, 0) })); Queue <State> unhandled = new Queue <State>(); unhandled.Enqueue(dfa._states.First()); while (unhandled.Count > 0) { State cur = unhandled.Dequeue(); foreach (var token in _tokens) { HashSet <Deriv> derivs = new HashSet <Deriv>(); foreach (var deriv in cur._derivs) { if (deriv._point < deriv._prodc._right.Count && deriv._prodc._right[deriv._point] == token) { derivs.Add(new Deriv(deriv._prodc, deriv._point + 1)); } } if (derivs.Count == 0) { continue; } State added = new State(dfa._states.Count, _prodcs, derivs); try { State state = dfa._states.First(e => e == added); if (dfa._trfs.ContainsKey((cur._id, token))) { dfa._trfs[(cur._id, token)] = state._id;
public List <(int, List <Token>, List <Token>, bool, Prodc)> Analyze(List <Token> words) { words.Add(Dollar); var res = new List <(int, List <Token>, List <Token>, bool, Prodc)>(); List <Token> astack = new List <Token> { Dollar, _start }; int step = 0; int index = 0; do { ++step; List <Token> ta = new List <Token>(); astack.ForEach(e => ta.Add(e)); List <Token> rest = new List <Token>(); for (int pos = index; pos < words.Count; ++pos) { rest.Add(words[pos]); } Token top = astack.Last(); if (top._type == Token.Type.TERMINAL) { if (top == words[index]) { astack.RemoveAt(astack.Count - 1); ++index; res.Add((step, ta, rest, true, new Prodc())); } else { res.Add((step, ta, rest, false, new Prodc())); } } else { if (_sheet.TryGetValue((_rs[top], _cs[words[index]]), out HashSet <Prodc> ps)) { // use the first as default Prodc sel = ps.First(); res.Add((step, ta, rest, true, sel)); astack.RemoveAt(astack.Count - 1); if (sel._right.First() != Epsilon) { List <Token> added = new List <Token>(); sel._right.ForEach(e => added.Add(e)); added.Reverse(); astack = astack.Concat(added).ToList(); } } else { if (top == words[index]) { astack.RemoveAt(astack.Count - 1); ++index; res.Add((step, ta, rest, true, new Prodc())); } else { res.Add((step, ta, rest, false, new Prodc())); } } }
public Prodc(Prodc prodc) { _left = prodc._left; _right = new List <Token>(prodc._right); }
public bool InsertProduction(Prodc prodc) => _prodcs.Add(new Prodc(prodc));
public Action(Action action) { _type = action._type; _nextState = action._nextState; _prodc = new Prodc(action._prodc); }
public Action(Prodc prodc) { _type = Type.REDUC; _nextState = -1; _prodc = new Prodc(prodc); }
public Action(int nextState) { _type = Type.SHIFT; _nextState = nextState; _prodc = new Prodc(); }
public Deriv(Prodc prodc, int point) : base(prodc, point) { }
public Action(Type type) { _type = type; _nextState = -1; _prodc = new Prodc(); }
public DerivBase(DerivBase deriv) { _prodc = new Prodc(deriv._prodc); _point = deriv._point; }
public DerivBase(Prodc prodc, int point) { _prodc = new Prodc(prodc); _point = point; }