コード例 #1
0
ファイル: LR1Item.cs プロジェクト: mrfichtn/opal
        public static StringBuilder Append(this StringBuilder builder,
                                           LR1Item item, bool showTransition)
        {
            var production = item.Production;

            builder.Append(production.Left.Name);
            builder.Append(": ");
            int i   = 0;
            var min = Math.Min(item.Position, item.Production.Right.Length);

            for (; i < min; i++)
            {
                builder.Append(production.Right[i].Name);
                builder.Append(' ');
            }

            builder.Append('·');

            for (; i < production.Right.Length; i++)
            {
                builder.Append(' ');
                builder.Append(production.Right[i].Name);
            }

            builder.Append(", ");
            builder.Append(item.Lookahead.Id == 0 ? "$" : item.Lookahead.Name);

            if (showTransition && (item.NextState >= 0))
            {
                builder.Append(" => ").Append(item.NextState);
            }

            return(builder);
        }
コード例 #2
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);
        }