Exemple #1
0
        public void CompileTestReturnStateTest()
        {
            parser = new LrParser(grammarLoader.Words, grammarLoader.Rules, ruleTable, grammarLoader.CountOfRules);

            string textToParse = "= k 12 if := num 10 = 3 3 if := id 6";

            Queue <Word> actualWords = textParser.Parse(textToParse);

            Assert.True(parser.TryCompile(actualWords));
        }
Exemple #2
0
        public MainWindow()
        {
            _compileData = new CompileData();

            DataContext = _compileData;

            InitializeComponent();

            _compileData.CodeToCompile = "= m 45 if := num 10 = 3 3 if := k 6";

            ParserLoader loader = new ParserLoader("words.txt", "rules.txt", "table.txt");

            _parser = new LrParser(loader);
            _parser.OnCompileError    += ParserOnOnCompileError;
            _parser.OnCompileDone     += ParserOnOnCompileDone;
            _parser.OnCompileNextStep += ParserOnOnCompileNextStep;
        }
Exemple #3
0
        public void CompileTest1()
        {
            parser = new LrParser(grammarLoader.Words, grammarLoader.Rules, ruleTable, grammarLoader.CountOfRules);

            string textToParse = "= 13 12 if := num 10 = 3 3 if := id 6";

            Queue <Word> actualWords = textParser.Parse(textToParse);

            if (parser.TryCompile(actualWords))
            {
                string expectedString = String.Format(
                    "{0}{1}{2}", "if ( 13 == 12 ) { num = 10; }",
                    Environment.NewLine, "if ( 3 == 3 ) { id = 6; }");

                Assert.AreEqual(expectedString, parser.CompiledText);
            }
            else
            {
                Assert.Fail();
            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            // Input string
            string input = "2+3*(1+3)"; // = 14

            // Parsing
            LrParser parser = new LrParser(new LrTableImpl(), input);

            parser.Parse();

            // Check success
            if (parser.config.action.type == LrAction.Type.Accept)
            {
                Console.WriteLine("success, AST:");
                Console.WriteLine(parser.ast);

                // Evaluate our AST by attaching callbacks on nodes
                LrEvaluate evaluate = new LrEvaluate(parser.ast);
                evaluate.NonTerm("statement", (node) => node.children[0]);
                evaluate.NonTerm("expression", (node) => {
                    if (node.children.Count == 1)
                    {
                        return(node.children[0]);
                    }
                    else
                    {
                        int v = int.Parse(node.children[0].value);
                        v    += int.Parse(node.children[2].value);
                        return(new LrAst.Node(LrAst.Node.Type.Term, v.ToString()));
                    }
                });
                evaluate.NonTerm("term", (node) => {
                    if (node.children.Count == 1)
                    {
                        return(node.children[0]);
                    }
                    else
                    {
                        int v = int.Parse(node.children[0].value);
                        v    *= int.Parse(node.children[2].value);
                        return(new LrAst.Node(LrAst.Node.Type.Term, v.ToString()));
                    }
                });
                evaluate.NonTerm("factor", (node) => node.children[node.children.Count == 1 ? 0 : 1]);
                evaluate.NonTerm("digit", (node) => node.children[0]);
                evaluate.Eval();

                // Print the resulting AST
                Console.WriteLine("result:");
                Console.WriteLine(parser.ast);
            }
            else
            {
                Console.WriteLine("parsing error at:");
                Console.WriteLine(parser.config.input);
            }

            // The End
            Console.WriteLine("\npress a key to quit...");
            Console.ReadKey(true);
        }