Read every node of an Antlr AST (Antlr v2) in C#

96 views
Skip to first unread message

Micxas

unread,
Jun 11, 2015, 4:22:39 AM6/11/15
to antlr-di...@googlegroups.com
Hello,

first ... sorry for my bad english! ;)

I've created an Antlr AST with Antlr v2.
Unfortunately, I had to use Antlr v2 because of the old Syntax of the Grammar I used (CIL-Grammar from Pascal Lacroix). Now I want to read every node, because I want to convert the AST into a kind of xml-Format.

I need something like that:

Foreach (node in AST) {
     String nodeStr = node.getName
     ....
}

That is my Code for creating the AST:

public static void Main(string[] args)
        {    
            string filename = @"file.il";
            
            if (args.Length > 0)
            {
                filename = args[0];
            }
            
            if (!File.Exists(filename))
            {
                Console.WriteLine("Input file '{0}' doesn't exist.", filename);
                return;
            }
            
            StreamReader stream = new StreamReader(filename);
            CILLexer lexer = new CILLexer(stream);            
            CILParser parser = new CILParser(lexer);
            
            try 
            {
                parser.start();
                Console.WriteLine("Parsing: OK");
                
                AST root = parser.getAST();
                
              // HERE I WANT TO READ THE AST 'root' and CONVERT HIM!
                
                
                antlr.debug.misc.ASTFrame frame = new antlr.debug.misc.ASTFrame("AST", root);
                frame.ShowDialog();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception during parsing: " + ex.ToString());            
            }            
        }       

Is there a method in ANTLR v2 by which I can pass through the tree?

Kind Regards,

Mario

Micxas

unread,
Jun 11, 2015, 5:37:34 AM6/11/15
to antlr-di...@googlegroups.com
Hello again,

i've found  AST (XML) Serialization (http://www.antlr2.org/doc/trees.html#AST%20Serialization).That's perfect!

Kind Regards,

Mario



Reply all
Reply to author
Forward
0 new messages