Exemplo n.º 1
0
        private static void _Main(string[] args)
        {
            // input "x = 2*(3+3)"

            ICharStream input;

            if (args.Length > 0)
            {
                if (args[0].Equals("-i"))
                {
                    if (args.Length > 1)
                    {
                        input = new ANTLRFileStream(args[1]);
                    }
                    else
                    {
                        throw new Exception("No input file specified.");
                    }
                }
                else
                {
                    input = new ANTLRStringStream(args[0]);
                }
            }
            else
            {
                input = new ANTLRInputStream(Console.OpenStandardInput());
            }

            var lex    = new VecMathLexer(input);
            var tokens = new CommonTokenStream(lex);
            var g      = new VecMathParser(tokens);
            IAstRuleReturnScope <CommonTree> r = g.prog();
            CommonTree t = r.Tree;

            Console.WriteLine("Original tree:   " + t.ToStringTree());

            var simplify = new Simplify(new CommonTreeNodeStream(t));

            t = (CommonTree)simplify.Downup(t);

            var reduce = new Reduce(new CommonTreeNodeStream(t));

            t = (CommonTree)reduce.Downup(t);

            Console.WriteLine("Simplified tree: " + t.ToStringTree());
            Console.ReadKey();
        }
Exemplo n.º 2
0
        public void TestDoubleLevelTree() /*throws Exception*/
        {
            TreeWizard wiz       = new TreeWizard(adaptor, tokens);
            CommonTree t         = (CommonTree)wiz.Create("(A (B C) (B D) E)");
            string     found     = t.ToStringTree();
            string     expecting = "(A (B C) (B D) E)";

            Assert.AreEqual(expecting, found);
        }
Exemplo n.º 3
0
        public void TestSingleNodeWithArg() /*throws Exception*/
        {
            TreeWizard wiz       = new TreeWizard(adaptor, tokens);
            CommonTree t         = (CommonTree)wiz.Create("ID[foo]");
            string     found     = t.ToStringTree();
            string     expecting = "foo";

            Assert.AreEqual(expecting, found);
        }
Exemplo n.º 4
0
        public void ParserShouldHandleTree()
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream(" 2 * (3 + 5)"));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            CommonTree tree = (CommonTree)parser.expression().Tree;

            Console.WriteLine(tree.ToStringTree());
        }
Exemplo n.º 5
0
        public void TestSingleLevelTree() /*throws Exception*/
        {
            TreeWizard wiz       = new TreeWizard(adaptor, tokens);
            CommonTree t         = (CommonTree)wiz.Create("(A B C D)");
            string     found     = t.ToStringTree();
            string     expecting = "(A B C D)";

            assertEquals(expecting, found);
        }
Exemplo n.º 6
0
        public void TestListTree() /*throws Exception*/
        {
            TreeWizard wiz       = new TreeWizard(adaptor, tokens);
            CommonTree t         = (CommonTree)wiz.Create("(nil A B C)");
            string     found     = t.ToStringTree();
            string     expecting = "A B C";

            Assert.AreEqual(expecting, found);
        }
Exemplo n.º 7
0
        public void TestParseWithText2()
        {
            TreeWizard wiz = new TreeWizard(adaptor, tokens);
            CommonTree t   = (CommonTree)wiz.Create("(A B[T__32] (C (D E[a])))");
            // C pattern has no text arg so despite [bar] in t, no need
            // to match text--check structure only.
            bool valid = wiz.Parse(t, "(A B[foo] C)");

            Assert.AreEqual("(A T__32 (C (D a)))", t.ToStringTree());
        }
Exemplo n.º 8
0
        public void ParserShouldHandleTree()
        {
            ECalcLexer  lexer  = new ECalcLexer(new ANTLRStringStream(" 2 * (3 + 5)"));
            ECalcParser parser = new ECalcParser(new CommonTokenStream(lexer));

            CommonTree tree = (CommonTree)parser.expression().Tree;

            //Console.WriteLine(tree.ToStringTree());
            //modify by wsl
            Assert.AreEqual("(* 2 (+ 3 5))", tree.ToStringTree());
        }
Exemplo n.º 9
0
        public void TestReplaceWithOneChildren() /*throws Exception*/
        {
            // assume token type 99 and use text
            CommonTree t  = new CommonTree(new CommonToken(99, "a"));
            CommonTree c0 = new CommonTree(new CommonToken(99, "b"));

            t.AddChild(c0);

            CommonTree newChild = new CommonTree(new CommonToken(99, "c"));

            t.ReplaceChildren(0, 0, newChild);
            string expecting = "(a c)";

            Assert.AreEqual(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Exemplo n.º 10
0
        public void TestReplaceTwoWithOneAtRight() /*throws Exception*/
        {
            CommonTree t = new CommonTree(new CommonToken(99, "a"));

            t.AddChild(new CommonTree(new CommonToken(99, "b")));
            t.AddChild(new CommonTree(new CommonToken(99, "c")));
            t.AddChild(new CommonTree(new CommonToken(99, "d")));

            CommonTree newChild = new CommonTree(new CommonToken(99, "x"));

            t.ReplaceChildren(1, 2, newChild);
            string expecting = "(a b x)";

            assertEquals(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Exemplo n.º 11
0
        public void TestReplaceAllWithOne() /*throws Exception*/
        {
            CommonTree t = new CommonTree(new CommonToken(99, "a"));

            t.AddChild(new CommonTree(new CommonToken(99, "b")));
            t.AddChild(new CommonTree(new CommonToken(99, "c")));
            t.AddChild(new CommonTree(new CommonToken(99, "d")));

            CommonTree newChild = new CommonTree(new CommonToken(99, "x"));

            t.ReplaceChildren(0, 2, newChild);
            string expecting = "(a x)";

            Assert.AreEqual(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Exemplo n.º 12
0
        public void TestReplaceAtLeft() /*throws Exception*/
        {
            CommonTree t = new CommonTree(new CommonToken(99, "a"));

            t.AddChild(new CommonTree(new CommonToken(99, "b")));       // index 0
            t.AddChild(new CommonTree(new CommonToken(99, "c")));
            t.AddChild(new CommonTree(new CommonToken(99, "d")));

            CommonTree newChild = new CommonTree(new CommonToken(99, "x"));

            t.ReplaceChildren(0, 0, newChild);
            string expecting = "(a x c d)";

            assertEquals(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Exemplo n.º 13
0
 /// <summary>Logs the Css diagnostics.</summary>
 /// <param name="css">The css content.</param>
 /// <param name="commonTree">The common tree.</param>
 private static void LogDiagnostics(string css, CommonTree commonTree)
 {
     Trace.WriteLine("Input Css:");
     Trace.WriteLine("____________________________________________________");
     Trace.WriteLine(css);
     Trace.WriteLine("____________________________________________________");
     Trace.WriteLine("Css String Tree:");
     Trace.WriteLine("____________________________________________________");
     //// var dotTreeGenerator = new DotTreeGenerator();
     //// Trace.WriteLine(dotTreeGenerator.ToDot(tree));
     Trace.WriteLine(commonTree.ToStringTree());
     Trace.WriteLine("____________________________________________________");
     Trace.WriteLine("Css Common Tree:");
     Trace.WriteLine("____________________________________________________");
     LogTree(commonTree);
     Trace.WriteLine("____________________________________________________");
 }
Exemplo n.º 14
0
        public void TestReplaceAllWithTwo() /*throws Exception*/
        {
            CommonTree t = new CommonTree(new CommonToken(99, "a"));

            t.AddChild(new CommonTree(new CommonToken(99, "b")));
            t.AddChild(new CommonTree(new CommonToken(99, "c")));
            t.AddChild(new CommonTree(new CommonToken(99, "d")));

            CommonTree newChildren = (CommonTree)adaptor.Nil();

            newChildren.AddChild(new CommonTree(new CommonToken(99, "x")));
            newChildren.AddChild(new CommonTree(new CommonToken(99, "y")));

            t.ReplaceChildren(0, 2, newChildren);
            string expecting = "(a x y)";

            assertEquals(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Exemplo n.º 15
0
        public void TestReplaceOneWithTwoInMiddle() /*throws Exception*/
        {
            CommonTree t = new CommonTree(new CommonToken(99, "a"));

            t.AddChild(new CommonTree(new CommonToken(99, "b")));
            t.AddChild(new CommonTree(new CommonToken(99, "c")));
            t.AddChild(new CommonTree(new CommonToken(99, "d")));

            CommonTree newChildren = (CommonTree)adaptor.Nil();

            newChildren.AddChild(new CommonTree(new CommonToken(99, "x")));
            newChildren.AddChild(new CommonTree(new CommonToken(99, "y")));

            t.ReplaceChildren(1, 1, newChildren);
            string expecting = "(a b x y d)";

            Assert.AreEqual(expecting, t.ToStringTree());
            t.SanityCheckParentAndChildIndexes();
        }
Exemplo n.º 16
0
        public static void Main(string[] args)
        {
            if (args.Length != 0)
            {
                string inputFileName = args[0];
                if (!Path.IsPathRooted(inputFileName))
                {
                    inputFileName = Path.Combine(Environment.CurrentDirectory, inputFileName);
                }
                ICharStream input = new ANTLRFileStream(inputFileName);

                // BUILD AST
                ANTLRv3Lexer      lex             = new ANTLRv3Lexer(input);
                CommonTokenStream tokens          = new CommonTokenStream(lex);
                ANTLRv3Parser     g               = new ANTLRv3Parser(tokens);
                ANTLRv3Parser.grammarDef_return r = g.grammarDef();
                CommonTree t = (CommonTree)r.Tree;
                System.Console.Out.WriteLine(t.ToStringTree());

                /*
                 * // BUILD AST + PARSE TREES (needs ANTLR -debug option)
                 * ANTLRv3Lexer lex = new ANTLRv3Lexer(input);
                 * CommonTokenStream tokens = new CommonTokenStream(lex);
                 * ParseTreeBuilder builder = new ParseTreeBuilder(inputName);
                 * ANTLRv3Parser g = new ANTLRv3Parser(tokens, builder);
                 * ANTLRv3Parser.grammarDef_return r = g.grammarDef();
                 * CommonTree t = (CommonTree)r.Tree; // not used here
                 * System.Console.Out.WriteLine("parse tree: "+builder.Tree.ToStringTree());
                 * System.Console.Out.Write("input:\n"+builder.Tree.ToInputString());
                 */

                // WALK AST
                CommonTreeNodeStream nodes  = new CommonTreeNodeStream(t);
                ANTLRv3Tree          walker = new ANTLRv3Tree(nodes);
                walker.grammarDef();
            }
            else
            {
                Console.Error.WriteLine("Usage: ANTLRv3 <input-file>");
            }
        }
Exemplo n.º 17
0
        public static void Main(string [] args)
        {
            String            inputString = " Lot --> A t(4,5,6) extrude(rand(2,32)) B ";
            ANTLRStringStream input       = new ANTLRStringStream(inputString);

            //Stream inputStream = Console.OpenStandardInput ();
            //ANTLRInputStream input = new ANTLRInputStream (inputStream);
            CGALexer          lexer  = new CGALexer(input);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            CGAParser         parser = new CGAParser(tokens);                           // Create parser
            var result = parser.ruleDefinition();

            CommonTree Tree = (CommonTree)result.Tree;

            Console.WriteLine(Tree.ToStringTree());                                   // Print out the tree

            DotTreeGenerator gen = new DotTreeGenerator();
            var st = gen.ToDot(Tree);

            Console.WriteLine(st);
            //Console.WriteLine (TreeUtilities.toTree (inputStream).ToStringTree());

            //Preorder (Tree, 0);                                             // Print out the tree with TAB GRAPH
        }