public static IParseTree <Symbol> Build(Source.IOrigin origin) { var sanitizedTokens = SanitizedTokens(origin); // Assume every list from tupple is of size 1 !!! var leafs = sanitizedTokens.Select(r => { Debug.Assert(r.Item2.Count() == 1); return(new ParseLeaf <Symbol>(r.Item1, new Symbol(r.Item2.First()))); }); var grammar = new Grammar <Symbol>( NicodemGrammarProductions.StartSymbol(), NicodemGrammarProductions.MakeProductionsDictionaryForGrammarConstructor()); var parser = new LlParser <Symbol>(grammar); var parseRes = parser.Parse(leafs); if (parseRes is OK <Symbol> ) { return((parseRes as OK <Symbol>).Tree); } else { var err = parseRes as Error <Symbol>; Console.WriteLine(ParserUtils <Symbol> .PrepareErrorMessage(err)); return(null); } }
public void EmptyGrammarTest() { var start = new CharSymbol('S'); var prodChar = RegExParser.Parse("$"); var prodSymbol = new StringProduction(start.C, prodChar); var grammar = new Grammar <CharSymbol>(start, new Dictionary <CharSymbol, IProduction <CharSymbol>[]> { { start, new [] { prodSymbol } } }); var parser = new LlParser <CharSymbol>(grammar); var emptyInput = new List <ParseLeaf <CharSymbol> >(); var res = parser.Parse(emptyInput); Assert.IsTrue(res); var resAsBranch = ((OK <CharSymbol>)res).Tree as ParseBranch <CharSymbol>; Assert.IsNotNull(resAsBranch); Assert.AreEqual(resAsBranch.Children.Count(), 1); Assert.AreEqual(resAsBranch.Symbol, start); var resLeaf = resAsBranch.Children.ElementAt(0) as ParseLeaf <CharSymbol>; Assert.IsNotNull(resLeaf); Assert.AreEqual(resLeaf.Symbol, new CharSymbol('$')); }
public void LL2ValidProgramsTests() { var ll1s = _grammars.Where(k => k.Type.Equals(GrammarType.LL2)); foreach (var ge in ll1s) { Debug.WriteLine("LL2 Testing valid programs for grammar: " + ge.Name); var parser = new LlParser <CharSymbol>(ge.Grammar); foreach (var prog in ge.ValidPrograms) { Assert.IsTrue(parser.Parse(prog.Item2), ge.Name + ": " + prog.Item1); } } }