Beispiel #1
0
        static void TestGenerationSample()
        {
            var g = Grammar.FromTokens("S'", "$", "S", "C", "C", "c", "d");

            g.Matches("S'", "S");
            g.Matches("S", "C", "C");
            g.Matches("C", "c", "C");
            g.Matches("C", "d");
            var gen = new ParserGenerator(g);
            var tab = gen.GenerateParserTable();
        }
Beispiel #2
0
        static void TestGenerationAmbiguous()
        {
            var g = Grammar.FromTokens("E", "+", "-", "*", "/", "N", "(", ")", "x");

            g.Matches("E", "N");
            g.MatchesRight("E", "E", "+", "E");
            g.MatchesRight("E", "E", "-", "E");
            g.MatchesLeft("E", "E", "*", "E");
            g.MatchesLeft("E", "E", "/", "E");
            g.MatchesLeft("E", "-", "E");
            g.Matches("E", "(", "E", ")");
            g.Matches("N", "x");
            g.Matches("N", "N", "x");
            var gen = new ParserGenerator(g);
            var tab = gen.GenerateParserTable();

            Console.WriteLine("Constructed table with {0} states.", tab.Length);
            var parser = new RunTimeParser <double>(tab, new RunTimeParser <double> .RuntimeParserAction[]
            {
                seg => seg.Array[seg.Offset],
                seg => seg.Array[seg.Offset],
                seg => seg.Array[seg.Offset] + seg.Array[seg.Offset + 2],
                seg => seg.Array[seg.Offset] - seg.Array[seg.Offset + 2],
                seg => seg.Array[seg.Offset] * seg.Array[seg.Offset + 2],
                seg => seg.Array[seg.Offset] / seg.Array[seg.Offset + 2],
                seg => - seg.Array[seg.Offset + 1],
                seg => seg.Array[seg.Offset + 1],
                seg => seg.Array[seg.Offset],
                seg => 10 * seg.Array[seg.Offset] + seg.Array[seg.Offset + 1]
            });

            while (true)
            {
                Console.Write(">");
                var line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }
                parser.SetInput(Tokenize(line));
                try
                {
                    var res = parser.Parse();
                    Console.WriteLine("Res={0}", res);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("ERROR: {0}", ex);
                }
            }
        }
Beispiel #3
0
        static void TestGeneration()
        {
            var g = Grammar.FromTokens("E'", "$", "E", "T", "F", "P", "w", "(", ")", "+", "*", "?", "|");

            g.Matches("E'", "E", "$");
            g.Matches("E", "T");
            g.Matches("E", "E", "|", "T");
            g.Matches("T", "F");
            g.Matches("T", "T", "F");
            g.Matches("F", "P");
            g.Matches("F", "P", "*");
            g.Matches("F", "P", "+");
            g.Matches("F", "P", "?");
            g.Matches("P", "w");
            g.Matches("P", "(", "E", ")");
            var gen = new ParserGenerator(g);
            var tab = gen.GenerateParserTable();
        }
Beispiel #4
0
        public IEnumerable <CodeGenerator> DoGeneration()
        {
            // generation requires two runs - first we collect the grammar
            yield return(this);

            // process the grammar
            _productions = _grammar.Productions.ToArray();
            var generator = new ParserGenerator(_grammar);
            var table     = generator.GenerateParserTable();

            // on second run we render the code and insert the actions.
            WriteLine(PreParser.Replace("{VALUETYPE}", _valueType));
            // write the token enum
            WriteTokenEnum();
            // write the parser table code
            WriteParserTableCode(table);
            // put in the actions at the end using a second run
            yield return(this);

            EndAction(); // (manually close the last action)
            // write the end of the parser function
            WriteLine(PostParser);
        }