예제 #1
0
        //given Production A := <alpha> ~B<beta>,a
        //return first(<beta>a)
        private static List <int> getFirst(Production e)
        {
            List <int> first = new List <int>();
            //where B in Rules
            int place = e.Place;

            //B is the last one,<beta> is empty
            if (place == e.Rules().Length - 1)
            {
                //end with #
                if (e.Nexttok == -1)
                {
                    //return #
                    first.Add(-1);
                    return(first);
                }
                else
                {
                    //return a
                    return(ParserGenerate.first[Token.GetToken(e.Nexttok)]);
                }
            }
            else
            {
                // return first(<beta>)
                return(ParserGenerate.first[Token.GetToken(e.Rules()[place + 1])]);
            }
        }
예제 #2
0
        // recuresive throught rightmost nonterminal
        //ResultID : number in the ParseResult, which should include current reduce rule
        public ParseNode GenerateParseTree(ParseNode node)
        {
            if (node.thisTok.ttype == Ttype.Terminal)
            {
                return(node);
            }
            Production currRule = ParserResult[ResultID];

            node.geneTok = new ParseNode[currRule.Rules().Length];
            for (int i = currRule.Rules().Length - 1; i >= 0; i--)
            {
                Token token = Token.GetToken(currRule.Rules()[i]);
                if (token.ttype == Ttype.Nontermi)
                {
                    ResultID--;
                }
                node.geneTok[i] = GenerateParseTree(new ParseNode(token));
            }
            return(node);
        }
예제 #3
0
 //generate node through the production
 public ParseNode(Production Production)
 {
     this.mProduction = Production;
     thisTok          = Token.GetToken(Production.Nonterminal());
 }
예제 #4
0
        public void ParseTable()
        {
            init();
            // Index for current lexical Analyze result
            int  currResultTok = 0;
            bool accepted      = false;

            ParseStack.Push(Tuple.Create(0, Token.tokens[0]));
            while (!accepted)
            {
                Token nexttok;
                if (currResultTok < Analysis.ResultTable.Count)
                {
                    nexttok = StrType.Res2Token[Analysis.ResultTable[currResultTok].type];
                }
                else
                {
                    nexttok = Token.GetToken(-1);
                }
                Tuple <Ptype, int> nextact;
                Tuple <int, Token> nexttuple = Tuple.Create(ParseStack.Peek().Item1, nexttok);
                //there is no Action in next given tuple
                if (!Action.ContainsKey(nexttuple))
                {
                    Tuple <int, Token> emptyreduce = Tuple.Create(ParseStack.Peek().Item1, Token.GetToken(-1));
                    //no empty reduce
                    if (Action.ContainsKey(emptyreduce))
                    {
                        nextact = Action[emptyreduce];
                    }
                    else
                    {
                        PanicRecover(ref currResultTok);
                        break;
                    }
                }
                else
                {
                    nextact = Action[nexttuple];
                }
                switch (nextact.Item1)
                {
                case Ptype.move:
                {
                    ParseStack.Push(Tuple.Create(nextact.Item2, nexttok));
                    ShiftToken(ref currResultTok);
                    break;
                }

                case Ptype.reduce:
                {
                    Production rule   = Production.formalGrammar[nextact.Item2];
                    int        tokNum = rule.Rules().Length;
                    while (tokNum > 0)
                    {
                        ParseStack.Pop();
                        tokNum--;
                    }
                    //top equal to -1 will not be allowded
                    if (ParseStack.Peek().Item1 == -1)
                    {
                        //error handling
                        PanicRecover(ref currResultTok);
                    }
                    //next state
                    int nextsta = ParseStack.Peek().Item1;
                    ParseStack.Push(Tuple.Create(Goto[Tuple.Create(closures[nextsta], Token.tokens[rule.Nonterminal()])], Token.tokens[rule.Nonterminal()]));
                    AddParseResult(rule);
                    break;
                }

                case Ptype.accept:
                {
                    accepted = true;
                    ResultID = ParserResult.Count - 1;
                    AcceptAction();
                    break;
                }
                }
            }
            if (afterPanicRecover)
            {
                UIHelper.Log(GeneticStringClass.ITTerminate);
                Thread.CurrentThread.Abort();
            }
        }