Exemplo n.º 1
0
 public void Crashes(LexAction another)
 {
     if (Type == another.Type && Target == another.Target && Crash == null && another.Crash == null)
     {
         return;
     }
     else if (Crash == null || another.Type != Crash.Type || another.Target != Crash.Target)
     {
         Crash = another;
     }
 }
Exemplo n.º 2
0
        public bool TryParse(TokenStream ts, out IASTNode result)
        {
            Reset();
            //PrintStatus();
            while (true)
            {
                LexAction action = GetAction(ts.Peek());
                //Console.WriteLine($"Token: {ts.Peek().Type}, Action: {action}");
                switch (action.Type)
                {
                case Accept: result = Symbols.Pop(); return(true);

                case Error: result = null; return(false);

                case Reduce: {
                    PureProduction   production = Productions[action.Target];
                    Stack <IASTNode> tmp        = new Stack <IASTNode>();
                    for (int i = 0; i < production.ElemCount; i++)
                    {
                        Status.Pop();
                        tmp.Push(Symbols.Pop());
                    }
                    IASTNode node = production.Action != null?
                                    production.Action(tmp.ToArray()) :
                                        (tmp.Count == 1 ? tmp.Pop() : new ASTList(tmp.ToArray()));

                    Symbols.Push(node);
                    if (Table[Status.Peek()].TryGetValue(production.Name, out LexAction a))
                    {
                        Status.Push(a.Target);
                    }
                    else
                    {
                        result = null;
                        return(false);
                    }
                    break;
                }

                case Shift: {
                    Token token = ts.Read();
                    Symbols.Push(new ASTLeaf(token));
                    Status.Push(action.Target);
                    break;
                }
                }
                //PrintStatus();
            }
        }