Пример #1
0
        public Production(Nonterminal nonterminal, SymbolList symbols, Bools drops, Bools promotes)
        {
            Id = -1;

            Nonterminal = nonterminal;
            Symbols     = symbols;
            Drops       = drops;
            Promotes    = promotes;

            //Debug.Assert(!IsPromote);
        }
Пример #2
0
        public void AddProductions(params IEnumerable <ProdSymbol>[] symss)
        {
            foreach (var syms in symss)
            {
                var symbols      = syms.ToList();
                var thisSymbols  = SymbolList.From(symbols.Select(p => p.Symbol));
                var thisDrops    = new Bools(symbols.Select(p => p.IsDrop));
                var thisPromotes = new Bools(symbols.Select(p => p.IsPromote));

                var promotesCount = thisPromotes.Count(p => p);
                var dropsCount    = thisDrops.Count(d => d);
                if (promotesCount > 1)
                {
                    throw new GrammarException($"nonterminal: rule `{Name}´ defines more than on promote");
                }
                if (promotesCount == 1)
                {
                    if (dropsCount != symbols.Count - 1)
                    {
                        throw new GrammarException($"nonterminal: rule `{Name}´ can only promote a single symbol");
                    }
                }
                if (symbols.Count > 0 && dropsCount == symbols.Count)
                {
                    throw new GrammarException($"nonterminal: rule `{Name}´ didn't want to drop anything in a row'");
                }

                foreach (var production in productions)
                {
                    if (production.Symbols.Equals(symbols))
                    {
                        var ss = string.Join(" ", symbols.Select(sym => sym.ToString()));
                        throw new GrammarException($"nonterminal: `{Name}´ rule `{ss}´ already defined before");
                    }
                }

                productions.Add(new Production(this, thisSymbols, thisDrops, thisPromotes));
            }
        }