コード例 #1
0
        protected override INode OnParse(IEnumerator <Token> iterator, Stack <int> tempstack, Stack <INode> tempnodes)
        {
            tempstack.Push(0);

            Map <int>[]       gtb = this.gtb;
            Map <TableItem>[] atb = this.atb;

            Grammar g = m_grammar;

            do
            {
                int current = (int)iterator.Current.type;
                if (!atb[tempstack.Peek()].TryGet(current, out TableItem ctb))
                {
                    m_handler.Fail(FMSG.I);
                    return(null);
                }

                int act = ctb.actType;
                if (act == LALRParser.act_SHIFT)
                {
                    tempnodes.Push(new TokNode(iterator.Current));
                    tempstack.Push(ctb.nextstate);

                    iterator.MoveNext();
                }
                else if (act == LALRParser.act_REDUCE)
                {
                    GItem item = g.m_items[ctb.nextstate];
                    int   len  = item.express.Length;

                    if (len > 1)
                    {
                        ExNode node = new ExNode(len);
                        while (--len >= 0)
                        {
                            tempstack.Pop();
                            node.Add(tempnodes.Pop());
                        }
                        tempnodes.Push(node);
                    }
                    else
                    {
                        while (--len >= 0)
                        {
                            tempstack.Pop();
                        }
                    }

                    tempstack.Push(gtb[tempstack.Peek()].Get(item.produce));
                }
                else if (act == LALRParser.act_ACCEPT)
                {
                    return(tempnodes.Pop());
                }
            } while (true);
        }
コード例 #2
0
        protected override INode OnParse(IEnumerator <Token> iterator, Stack <int> tempstack, Stack <INode> tempnodes)
        {
            tempstack.Push(0);

            Map <int>[]       gtb = this.gtb;
            Map <TableItem>[] atb = this.atb;

            Grammar g = m_grammar;

            do
            {
                int current = (int)iterator.Current.type;
                if (!atb[tempstack.Peek()].TryGet(current, out TableItem ctb))
                {
#if DEBUG
                    var debug_state = tempstack.Peek();
                    var debug_table = atb[debug_state];

                    foreach (var key in debug_table.Keys)
                    {
                        System.Diagnostics.Debug.WriteLine(((TokenType)key).ToString());
                    }

                    var debug_item = iterator.Current;
                    System.Diagnostics.Debugger.Break();
#endif
                    OnParsingError(tempstack, iterator, current);
                    m_handler.Fail(FMSG.I);
                    return(null);
                }

                int act = ctb.actType;
                if (act == act_SHIFT)
                {
                    tempnodes.Push(new TokNode(iterator.Current));
                    tempstack.Push(ctb.nextstate);

                    iterator.MoveNext();
                }
                else if (act == act_REDUCE)
                {
                    GItem item = g.m_items[ctb.nextstate];
                    int   len  = item.express.Length;

                    if (len > 1)
                    {
                        ExNode node = new ExNode(len);
                        while (--len >= 0)
                        {
                            tempstack.Pop();
                            node.Add(tempnodes.Pop());
                        }
                        tempnodes.Push(node);
                    }
                    else
                    {
                        tempstack.Pop();
                    }

                    tempstack.Push(gtb[tempstack.Peek()].Get(item.produce));
                }
                else if (act == act_ACCEPT)
                {
                    return(tempnodes.Pop());
                }
            } while (true);
        }