示例#1
0
文件: Program.cs 项目: paulochang/LR0
        private static bool parse(List<input> theInputList, ParsingTable theParsingTable, List<GrammarRule> theGrammar)
        {
            theInputList.Add("úEndSymbol");
            dfaState currentState = 0;
            Stack<dfaState> helperStack = new Stack<dfaState>();
            helperStack.Push(0);
            input currentInput;
            Action currentAction;
            int currentPosition = 0;

            while (currentPosition < theInputList.Count)
            {
                currentInput = theInputList[currentPosition];
                currentState = helperStack.Peek();
                if (theParsingTable.transitionTable.TryGetValue(new KeyValuePair<dfaState, input>(currentState, currentInput), out currentAction))
                {
                    switch (currentAction.type)
                    {
                        case ActionType.Shift:
                            currentPosition++;
                            helperStack.Push(currentAction.destiny);
                            break;
                        case ActionType.Reduce:
                            GrammarRule currentRule = theGrammar[currentAction.destiny];
                            for (int i = 0; i < currentRule.Expression.Count; i++)
                            {
                                helperStack.Pop();
                            }
                            input X = currentRule.RuleSymbol.id;
                            Action gotoAction;
                            dfaState peekedState = helperStack.Peek();
                            if (theParsingTable.transitionTable.TryGetValue(new KeyValuePair<dfaState, input>(peekedState, X), out gotoAction))
                            {
                                helperStack.Push(gotoAction.destiny);
                            }
                            else
                                return false;
                            break;
                        case ActionType.Accept:
                            return true;
                        default:
                            return false;
                    }
                }
            }
            return false;
        }
示例#2
0
文件: Program.cs 项目: paulochang/LR0
        private static ParsingTable GetParsingTable(DFA theAutomaton, HashSet<input> theTokens, HashSet<input> NonTerminals, HashSet<Tuple<dfaState, Symbol, Int32>> ReduceStates, List<GrammarRule> theGrammar)
        {
            ParsingTable Table = new ParsingTable();

            foreach (KeyValuePair<KeyValuePair<dfaState, input>, dfaState> item in theAutomaton.transitionTable)
            {
                if (NonTerminals.Contains(item.Key.Value))
                {
                    Table.transitionTable.Add(item.Key, new Action(item.Value, ActionType.Goto));
                }
                else
                {
                    Table.transitionTable.Add(item.Key, new Action(item.Value, ActionType.Shift));
                }
            }

            foreach (dfaState theState in theAutomaton.final)
            {
                Table.transitionTable.Add(
                    new KeyValuePair<dfaState, input>(theState, "úEndSymbol"),
                    new Action(-1, ActionType.Accept));
            }

            foreach (Tuple<dfaState, Symbol, Int32> item in ReduceStates)
            {
                if (!NonTerminals.Contains(item.Item2.id))
                    Table.transitionTable.Add(
                    new KeyValuePair<dfaState, input>(item.Item1, item.Item2.id),
                    new Action(item.Item3, ActionType.Reduce));
            }

            return Table;
        }