Esempio n. 1
0
        public LR1Parser(ILogger logger,
                         Grammar grammar,
                         ConflictList conflicts)
        {
            this.grammar = grammar;
            var endSymbol = grammar.Symbols[0];

            //Honalee Algorithm
            //Create kernel item from augmented gramar production
            var kernel     = new LR1Item(grammar[0], 0, endSymbol);
            var startState = new State(0)
            {
                kernel
            };

            States = new States {
                startState
            };
            startState.Closure(this.grammar);

            var queue = new Queue <State>();

            queue.Enqueue(startState);
            var actionBuilder = new ActionBuilder(this.grammar);

            //Method2
            while (queue.Count > 0)
            {
                startState = queue.Dequeue();
                Goto(startState, actionBuilder, queue);
            }

            var resolver = new ConflictResolvers(conflicts,
                                                 grammar.Symbols,
                                                 logger);

            Actions = actionBuilder.Build(logger,
                                          States,
                                          this.grammar.Symbols,
                                          resolver);
        }
Esempio n. 2
0
        public Actions Build(ILogger logger,
                             States states,
                             Symbols symbols,
                             ConflictResolvers resolvers)
        {
            var data = new int[states.Count, symbols.Count];

            for (var j = 0; j < data.GetLength(0); j++)
            {
                for (var i = 0; i < data.GetLength(1); i++)
                {
                    data[j, i] = -1;
                }
            }

            foreach (var stateAction in actions)
            {
                foreach (var q in stateAction.Value)
                {
                    var items = q.Value;
                    if (items.Count == 0)
                    {
                        continue;
                    }

                    if (items.Count == 1)
                    {
                        data[stateAction.Key, q.Key] = items[0];
                    }
                    else if (resolvers.TryFind(
                                 state: stateAction.Key,
                                 symbol: q.Key,
                                 action: out var resolveAction) &&
                             items.Contains(resolveAction))
                    {
                        data[stateAction.Key, q.Key] = resolveAction;
                    }