private void Assign(NonTerminal head, Terminal term, Production production)
        {
            if (predictiveParseTable[head][term].HasValue)
                throw new InvalidGrammarException("Grammar is not LL(1)");

            predictiveParseTable[head][term] = production;
        }
Example #2
0
        protected override ParserAction Action(ParseState state, Token token, out Production production, out ParseState nextState)
        {
            var lr0 = base.Action(state, token, out production, out nextState);

            if (token != null && lr0 == ParserAction.Reduce && !Grammar.GetFollowSet(production.Head).Contains(token.Terminal))
                return ParserAction.Error;

            return lr0;
        }
Example #3
0
        public bool Equals(Production other)
        {
            if (!Head.Equals(other.Head))
                return false;

            if (Body.Length != other.Body.Length)
                return false;

            for (int i = 0; i < Body.Length; i++)
                if (!Body[i].Equals(other.Body[i]))
                    return false;

            return true;
        }
Example #4
0
        protected override ParserAction Action(ParseState state, Token token, out Production production, out ParseState nextState)
        {
            bool shift = false;
            bool reduce = false;

            if (token == null && state.AcceptingState)
            {
                production = default(Production);
                nextState = null;
                return ParserAction.Accept;
            }

            if (token != null)
            {
                nextState = automaton[state, token.Terminal];
                if (nextState != null)
                {
                    production = default(Production);
                    shift = true;
                }
            }
            else
                nextState = null;

            if (state.Count() == 1 && state.First().Position == state.First().Production.Body.Length)
            {
                nextState = null;
                production = state.First().Production;
                reduce = true;
            }
            else
                production = default(Production);

            if (shift && reduce)
                throw new ParseException(String.Format("Shift/Reduce conflict between shift {0} and reduce {1}", nextState, production));
            else if (shift)
                return ParserAction.Shift;
            else if (reduce)
                return ParserAction.Reduce;
            else if (token == null)
                throw new ParseException("Unexpected EOF");

            throw new ParseException(String.Format("Unexpected {0} at line {1} column {2}", token.Value, token.Line, token.Column));
        }
Example #5
0
 protected abstract ParserAction Action(ParseState state, Token token, out Production production, out ParseState nextState);
Example #6
0
 public Item(Production p, int position)
 {
     Production = p;
     Position = position;
 }