public Grammar(Productions.Grammar grammar) { Symbols = new Symbols(); Symbols.AddSymbols(grammar.Symbols); if (!Symbols.TryFind(grammar.Start, out var startSym)) { startSym = Symbols.Create( name: grammar.Productions[0].Name, isTerminal: false); } //Create kernal rule var languageSymbol = new Symbol(name: "S'", id: (uint)Symbols.Count, terminal: false); var rule = new Rule(this, 0, languageSymbol, startSym !); rules = new List <Rule> { rule }; foreach (var prod in grammar.Productions) { var left = Symbols.Create(prod.Name, false); var right = prod.Right .Select(x => Symbols[x.Name]) .ToArray(); prod.RuleId = rules.Count; rules.Add(new Rule(this, rules.Count, left, right)); } TerminalSets = new TerminalSets(this); }
public Grammar(params string[] symbols) { rules = new List <Rule>(); Symbols = new Symbols(); var startSym = Symbols.Create(symbols[0], false); var languageSymbol = new Symbol("S'", (uint)Symbols.Count, terminal: false); var rule = new Rule(this, 0, languageSymbol, new[] { startSym }); rules.Add(rule); Symbol?left = null; var right = new List <Symbol>(); foreach (var symValue in symbols) { var itemIsEmpty = string.IsNullOrEmpty(symValue); if (left == null) { if (itemIsEmpty) { continue; } left = Symbols.Create(symValue, false); } else if (itemIsEmpty) { var production = new Rule(this, rules.Count, left, right.ToArray()); rules.Add(production); left = null; right.Clear(); } else { right.Add(Symbols.Create(symValue, true)); } } TerminalSets = new TerminalSets(this); }