Example #1
0
        static void Try(string input)
        {
            var str = new AntlrInputStream(input);

            System.Console.WriteLine(input);
            var lexer           = new arithmeticLexer(str);
            var tokens          = new CommonTokenStream(lexer);
            var parser          = new arithmeticParser(tokens);
            var listener_lexer  = new ErrorListener <int>();
            var listener_parser = new ErrorListener <IToken>();

            lexer.AddErrorListener(listener_lexer);
            parser.AddErrorListener(listener_parser);
            var tree = parser.file();

            if (listener_lexer.had_error || listener_parser.had_error)
            {
                System.Console.WriteLine("error in parse.");
            }
            else
            {
                System.Console.WriteLine("parse completed.");
            }
            System.Console.WriteLine(tree.ToStringTree());
            Decorate(tree, parser);
            System.Console.WriteLine(tree.ToStringTree());
        }
Example #2
0
        static void Try(string input)
        {
            var str      = new AntlrInputStream(input);
            var lexer    = new arithmeticLexer(str);
            var tokens   = new CommonTokenStream(lexer);
            var parser   = new arithmeticParser(tokens);
            var listener = new ErrorListener <IToken>(parser, lexer, tokens);

            parser.AddErrorListener(listener);
            var tree = parser.file();

            if (listener.had_error)
            {
                System.Console.WriteLine("error in parse.");
                System.Console.WriteLine(tokens.OutputTokens());
                System.Console.WriteLine(tree.OutputTree(tokens));
            }
            else
            {
                System.Console.WriteLine("parse completed.");
                System.Console.WriteLine(tokens.OutputTokens());
                System.Console.WriteLine(tree.OutputTree(tokens));
                var visitor = new CalculatorVisitor();
                visitor.Visit(tree);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var str    = new Antlr4.Runtime.AntlrInputStream("1 + 2");
            var lexer  = new arithmeticLexer(str);
            var tokens = new CommonTokenStream(lexer);
            var parser = new arithmeticParser(tokens);
            var tree   = parser.expression();

            System.Console.WriteLine(tree.ToStringTree(parser.RuleNames));
        }
Example #4
0
        static void Try(string input)
        {
            var str                = new AntlrInputStream("");
            var lexer              = new arithmeticLexer(str);
            var tokens             = new CommonTokenStream(lexer);
            var parser             = new arithmeticParser(tokens);
            var reconstructed_tree = new Visitor(parser, lexer).CreateTree(input);
            var str_2              = reconstructed_tree.ToStringTree(parser);

            System.Console.WriteLine(input);
            System.Console.WriteLine(str_2);
            var dot = OutDot(reconstructed_tree);

            System.Console.WriteLine(dot);
        }
Example #5
0
        static void Main(string[] args)
        {
            var input    = "1 + 2 * 3";
            var str      = new AntlrInputStream(input);
            var lexer    = new arithmeticLexer(str);
            var tokens   = new CommonTokenStream(lexer);
            var parser   = new arithmeticParser(tokens);
            var listener = new ErrorListener <IToken>();

            parser.AddErrorListener(listener);
            var tree = parser.file();

            if (listener.had_error)
            {
                System.Console.WriteLine("error in parse.");
            }
            else
            {
                System.Console.WriteLine("parse completed.");
                System.Console.WriteLine(tokens.OutputTokens());
                System.Console.WriteLine(tree.OutputTree(tokens));
            }
        }