[antlr-interest] problems with contructing AST and walking

Glenn Schütze glenn.schuetze at gmail.com
Mon May 9 12:57:18 PDT 2005


Hi!

I have to write a "compiler" to transfer eODL-source-codes  to a  
repository. I'm using ANTLR to parse programs and to build up an AST.
I have the following problem:

A possible extract I have to parse:
interface foo : AA::BB::CC , EE::FF::GG {
...
}

It's an interface which inherits from defined interfaces AA::BB::CC  
and EE::FF::GG.
I have to store these inheritance specification:

Interface's names which I collect in the AST don't appear in the rule  
<interface_inheritance_spec>. I only get the first Interface: in my  
example: AA::BB::CC


here the extract from the parser, where interface_inheritance_spec is  
something like :AA::BB::CC

interface_inheritance_spec:
             ":"^ scoped_name ( ","! scoped_name )*
;

scoped_name :
               "::"^ scoped_name_rest        //then it is full qualified
     |         scoped_name_rest
     ;

scoped_name_rest :
             IDENT^ ( "::"! IDENT )*
;

here is the appropriate extract from the tree walker:

interface_inheritance_spec { ArrayList<String> ine; ArrayList<String>  
inr ; } :
       #( ":" ine = scoped_name
               {
                                 System.out.println("##############");
                                 for ( String s : ine )  
{ System.out.println(s); }
                                 System.out.println("##############");
               }
                             (
                                       // this branch never seems to  
be entered, but I need it:-)
                                       inr = scoped_name
                                                     {
                                                             
System.out.println("############## hinteres");
                                                            for  
( String s : inr ) { System.out.println(s); }
                                                             
System.out.println("############## hinteres");
                                                     }
                             )*
       )
;


// That seems to work well! debug printing of ArrayLists works.
scoped_name returns [ ArrayList<String> strs ] { ArrayList<String>  
in; strs = new ArrayList<String>(); } :
         #( "::" in = scoped_name_rest )
                     {
                                 in.add("::");
                                 return in;
                     }
     |    in = scoped_name_rest
                     {
                                 strs = in; return strs;
                     }
     ;

scoped_name_rest returns [ArrayList<String> strs] { strs = new  
ArrayList<String>(); int i = 0;} :
             #( ide:IDENT
                                 {
                                          strs.add(ide.getText());
                                 }
                         ( idr:IDENT
                                         {
                                                     strs.add 
(idr.getText());
                                         }
                         )*
             )
             {
                  return strs; }
         ;





Thank you very much!!!
Bye, Glenn.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20050509/a02a7606/attachment-0001.html


More information about the antlr-interest mailing list