static ITerm CreateTerm(char ch) { ITerm result; if (char.IsLower(ch)) { result = new Terminal(ch.ToString()); } else { result = new NonTerminal(ch.ToString()); } return(result); }
static Grammar MakeGrammar() { var I = new NonTerminal("I"); var T = new NonTerminal("T"); var M = new NonTerminal("M"); var K = new NonTerminal("K"); var a = new Terminal("a"); var b = new Terminal("b"); var c = new Terminal("c"); var b1 = new Terminal("("); var b2 = new Terminal(")"); var s1 = new Terminal("+"); var s2 = new Terminal("-"); var s3 = new Terminal("*"); var s4 = new Terminal("/"); var rules = new[] { I[T], I[I, s1, T], I[I, s2, T], T[M], T[T, s3, M], T[T, s4, M], M[b1, I, b2], M[K], K[a], K[b], K[c], }; var grammar = new Grammar( rules.GroupBy(r => r.NonTerminal.Name) .Select(g => new Rule(g.First().NonTerminal, g.SelectMany(r => r.Alts))) ) { Log = Console.WriteLine }; grammar.Rules.First().IsStart = true; return(grammar); }