private void ParseCpp(string input)
        {
            AntlrInputStream stream = new AntlrInputStream(input);
            ITokenSource     lexer  = new CPP14Lexer(stream);
            ITokenStream     tokens = new CommonTokenStream(lexer);
            CPP14Parser      parser = new CPP14Parser(tokens);

            parser.BuildParseTree = true;
            CPP14Parser.TranslationunitContext tree = parser.translationunit();
            if (tree != null)
            {
                var builder = new TreeBuilder(parser, tree, treeModel);
                builder.Build();
            }
        }
        public override void Parse(FileInfo file)
        {
            Lexer lexer = new CPP14Lexer(CharStreams.fromPath(file.FullName));

            CommonTokenStream tokens = new CommonTokenStream(lexer);

            CPP14Parser parser = new CPP14Parser(tokens);

            ParserRuleContext context = parser.translationunit();

            var children = context.children;

            foreach (IParseTree child in children)
            {
                this.ParseNode(child);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var input = @"
                            #include <iostream>
            
                            using namespace std;
                                        
                            void main()
                            {
                                cout << ""Hello World!"";

                                if(true)
                                    cout <<""good"";

                                for (int i = 0; i < 10; i++)
                                {
                                    cout << i + ""-"";
                                    cout << 2i + ""XX"";
                                }
                            }
                            ";

            Console.WriteLine("Input:");
            Console.WriteLine(input);

            AntlrInputStream inputStream = new AntlrInputStream(input);

            CPP14Lexer        lexer             = new CPP14Lexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            CPP14Parser       parser            = new CPP14Parser(commonTokenStream);

            parser.BuildParseTree = true;
            IParseTree        tree = parser.translationunit(); //first row of grammer in *.g4 file >> first rule
            ReplaceExpression replaceExpression = new ReplaceExpression(commonTokenStream);

            ParseTreeWalker.Default.Walk(replaceExpression, tree);

            // BVisitor visitor = new BVisitor();
            // Console.WriteLine(visitor.Visit(tree));

            Console.WriteLine("////////////////////////////////////////////////////////////////////////");
            Console.WriteLine("Output:");
            Console.WriteLine(replaceExpression);
        }
Beispiel #4
0
        do_parse
        (
            string Declaration
        )
        {
            //
            // Declare the various streams of characters and tokens for parsing the input
            //

            AntlrInputStream         input_stream = new AntlrInputStream(Declaration);                  // Create a stream that reads from the input source
            CPP14Lexer               lexer        = new CPP14Lexer(input_stream);                       // Create a lexer that feeds off of the input stream
            CommonTokenStream        tokens       = new CommonTokenStream(lexer);                       // Create a buffer of tokens pulled from the lexer
            CPP14Parser              parser       = new CPP14Parser(tokens);                            // Create a parser that feeds off of the token buffer
            ParserRuleContext        tree         = parser.translationunit();                           // Call the translationunit rule in the grammar to build a parse tree from the input
            CPP14_Listener_Overrides my_listeners = new CPP14_Listener_Overrides();                     // Instantiate my listener override functions so they can be used by ParseTreeWalker

            //
            // Call all the listeners
            //

            ParseTreeWalker.Default.Walk(my_listeners, tree);

            return(my_listeners.function_def);
        }           // End parse_declaration
Beispiel #5
0
        static void Main(string[] args)
        {
            var input = @"
                            #include <iostream>
            
                            using namespace std;
                                        
                            void main()
                            {
                                cout << ""Hello World!"";

                                int x = 2;

                                if(x == 2)
                                {
                                    int y = 2;
                                    x = 20 * y;
                                }

                                if(x<1)
                                {
                                    cout <<""bad"";
                                }else {
                                    cout <<""good"";
                                }

                                for (int i = 0; i < 10; i++)
                                {
                                    cout << i + ""-"";
                                    cout << 2i + ""XX"";
                                }
                            }
                            ";

            Console.WriteLine("Input:");
            Console.WriteLine(input);

            AntlrInputStream inputStream = new AntlrInputStream(input);

            CPP14Lexer        lexer             = new CPP14Lexer(inputStream);
            CommonTokenStream commonTokenStream = new CommonTokenStream(lexer);
            CPP14Parser       parser            = new CPP14Parser(commonTokenStream);

            parser.BuildParseTree = true;
            IParseTree        tree = parser.translationunit(); //first row of grammer in *.g4 file >> first rule
            ReplaceExpression replaceExpression = new ReplaceExpression(commonTokenStream);

            ParseTreeWalker.Default.Walk(replaceExpression, tree);

            // BVisitor visitor = new BVisitor();
            // Console.WriteLine(visitor.Visit(tree));

            Console.WriteLine("////////////////////////////////////////////////////////////////////////");
            Console.WriteLine("Output:");

            var resultString = Regex.Replace(replaceExpression.ToString(), @"^\s+$[\r\n]*", string.Empty,
                                             RegexOptions.Multiline);

            resultString = Regex.Replace(resultString, @"^\s+", string.Empty, RegexOptions.Multiline);

            Console.WriteLine(resultString);
        }