Esempio n. 1
0
        public static AugmentedGrammar Augement(Grammar grammar)
        {
            var augmentedStartingSymbol = grammar.StartingSymbol + "`";

            var augmentedNonterminals = new List <string>();

            augmentedNonterminals.Add(augmentedStartingSymbol);
            augmentedNonterminals.AddRange(grammar.Nonterminals);

            var augmentedTerminals = new List <string>();

            augmentedTerminals.AddRange(grammar.Terminals);

            var augmentedSymbols = new List <string>();

            augmentedSymbols.AddRange(augmentedNonterminals);
            augmentedSymbols.AddRange(augmentedTerminals);

            var augmentedProductions = new Dictionary <string, List <LR0Item> >();

            augmentedProductions[augmentedStartingSymbol] = new List <LR0Item>()
            {
                new LR0Item(augmentedStartingSymbol, "." + grammar.StartingSymbol)
            };

            foreach (var productionPair in grammar.Productions)
            {
                var lhs      = productionPair.Key;
                var lr0Items = new List <LR0Item>();

                productionPair.Value.ForEach(rhs =>
                {
                    var item = new LR0Item(lhs, "." + rhs);
                    lr0Items.Add(item);
                });

                augmentedProductions[lhs] = lr0Items;
            }

            return(new AugmentedGrammar()
            {
                Nonterminals = augmentedNonterminals,
                Terminals = augmentedTerminals,
                Productions = augmentedProductions,
                StartingSymbol = augmentedStartingSymbol,
                AllSymbols = augmentedSymbols
            });
        }
Esempio n. 2
0
        public LR0Item MoveDot()
        {
            var dotIndex = GetDotIndex();

            if (dotIndex == rhs.Length - 1)
            {
                return(this);
            }

            var rhsBuilder = new StringBuilder(rhs);

            rhsBuilder[dotIndex]     = rhsBuilder[dotIndex + 1];
            rhsBuilder[dotIndex + 1] = '.';

            var newLR0Item = new LR0Item(lhs, rhsBuilder.ToString());

            return(newLR0Item);
        }