Example #1
0
        private StateCollection CreateParserStates(CGTContent content)
        {
            rules = CreateRules(content);

            StateCollection states = new StateCollection();

            foreach (LALRStateRecord record in content.LALRStateTable)
            {
                State state = new State(record.Index);
                states.Add(state);
            }

            foreach (LALRStateRecord record in content.LALRStateTable)
            {
                State state = states[record.Index];
                foreach (ActionSubRecord subRecord in record.ActionSubRecords)
                {
                    lalr.Action action =
                        ActionFactory.CreateAction(subRecord,
                                                   states,
                                                   symbols,
                                                   rules);
                    state.Actions.Add(action);
                }
            }
            return(states);
        }
Example #2
0
        private void DoReduce(Token token, ReduceAction action)
        {
            int reduceLength = action.Rule.Rhs.Length;

            State currentState;
            // Do not reduce if the rule is single nonterminal and TrimReductions is on
            bool skipReduce = ((TrimReductions) && (reduceLength == 1) && (action.Rule.Rhs[0] is SymbolNonterminal));

            if (skipReduce)
            {
                stateStack.Pop();
                currentState = stateStack.Peek();
            }
            else
            {
                Token[] tokens = new Token[reduceLength];
                for (int i = 0; i < reduceLength; i++)
                {
                    stateStack.Pop();
                    tokens[reduceLength - i - 1] = tokenStack.Pop();
                }
                NonterminalToken nttoken;
                if (reduceLength == 0)
                {
                    nttoken = new NonterminalToken(action.Rule, tokens, token.Location);
                }
                else
                {
                    nttoken = new NonterminalToken(action.Rule, tokens, tokens[reduceLength - 1].Location);
                }

                tokenStack.Push(nttoken);
                currentState = stateStack.Peek();

                if (OnReduce != null)
                {
                    ReduceEventArgs args = new ReduceEventArgs(action.Rule, nttoken, currentState);
                    OnReduce(this, args);
                    DoReleaseTokens(args.Token);

                    continueParsing = args.Continue;
                }
            }
            Action gotoAction = currentState.Actions.Get(action.Rule.Lhs);

            if (gotoAction is GotoAction)
            {
                DoGoto(token, (GotoAction)gotoAction);
            }
            else
            {
                throw new ParserException("Invalid action table in state");
            }
        }
Example #3
0
        private void ParseTerminal(TerminalToken token)
        {
            State currentState = stateStack.Peek();

            lalr.Action action = currentState.Actions.Get(token.Symbol);

            if (action is ShiftAction)
            {
                DoShift(token, (ShiftAction)action);
            }
            else if (action is ReduceAction)
            {
                DoReduce(token, (ReduceAction)action);
            }
            else if (action is AcceptAction)
            {
                DoAccept(token, (AcceptAction)action);
            }
            else
            {
                continueParsing = false;
                FireParseError(token);
            }
        }
Example #4
0
 public void Add(Action action)
 {
     table.Add(action.symbol, action);
 }