Ejemplo n.º 1
0
            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);
            }
Ejemplo n.º 2
0
        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;
Ejemplo n.º 3
0
        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()));
                        }
                    }
                }
Ejemplo n.º 4
0
 public Prodc(Prodc prodc)
 {
     _left  = prodc._left;
     _right = new List <Token>(prodc._right);
 }
Ejemplo n.º 5
0
 public bool InsertProduction(Prodc prodc)
 => _prodcs.Add(new Prodc(prodc));
Ejemplo n.º 6
0
 public Action(Action action)
 {
     _type      = action._type;
     _nextState = action._nextState;
     _prodc     = new Prodc(action._prodc);
 }
Ejemplo n.º 7
0
 public Action(Prodc prodc)
 {
     _type      = Type.REDUC;
     _nextState = -1;
     _prodc     = new Prodc(prodc);
 }
Ejemplo n.º 8
0
 public Action(int nextState)
 {
     _type      = Type.SHIFT;
     _nextState = nextState;
     _prodc     = new Prodc();
 }
Ejemplo n.º 9
0
 public Deriv(Prodc prodc, int point) : base(prodc, point)
 {
 }
Ejemplo n.º 10
0
 public Action(Type type)
 {
     _type      = type;
     _nextState = -1;
     _prodc     = new Prodc();
 }
Ejemplo n.º 11
0
 public DerivBase(DerivBase deriv)
 {
     _prodc = new Prodc(deriv._prodc);
     _point = deriv._point;
 }
Ejemplo n.º 12
0
 public DerivBase(Prodc prodc, int point)
 {
     _prodc = new Prodc(prodc);
     _point = point;
 }