[antlr-interest] Parser: How to refer to an token in a grammar file, without conflict with the local AST variable generated by ANTLR?

luciano mantuaneli mantu_lists at yahoo.com.br
Tue Nov 21 03:50:19 PST 2006


Greetings!

Suppose a grammar rule like that:

1    classField!
2        :    // method, constructor, or variable declaration
3            mods:modifiers
4            (    td:typeDefinitionInternal[#mods]
5                {#classField = #td;}
6
7            |    (tp:typeParameters)?
8                (
9                    h:ctorHead s:constructorBody // constructor
10                    {#classField = #(#[CTOR_DEF,"CTOR_DEF"], mods, tp, h, s);}
11
12                    |    // A generic method/ctor has the typeParameters before the return type.
13                        // This is not allowed for variable definitions, but this production
14                        // allows it, a semantic check could be used if you wanted.
15                        t:typeSpec[false]        // method or variable declaration(s)
16                        (    IDENT{                // the name of the method
17                            //syso(methId_AST.getText());
18                            }
19
20                            // parse the formal parameter declarations.
21                            LPAREN! param:parameterDeclarationList RPAREN!
22
23                            rt:declaratorBrackets[#t]
24
25                            // get the list of exceptions that this method is
26                            // declared to throw
27                            (tc:throwsClause)?
28
29                            ( s2:compoundStatement | SEMI )
30                            {#classField = #(#[METHOD_DEF,"METHOD_DEF"],
31                                         mods,
32                                         tp,
33                                         #(#[TYPE,"TYPE"],rt),
34                                         IDENT,
35                                         param,
36                                         tc,
37                                         s2);}
38                        |    v:variableDefinitions[#mods,#t] SEMI
39                            {#classField = #v;}
40                        )
41                )
42            )
43
44        // "static { ... }" class initializer
45        |    static s3:compoundStatement
46            {#classField = #(#[STATIC_INIT,"STATIC_INIT"], s3);}
47
48        // "{ ... }" instance initializer
49        |    s4:compoundStatement
50            {#classField = #(#[INSTANCE_INIT,"INSTANCE_INIT"], s4);}
51        ;

Suppose now that i want to just print out the name of methods in java console. I tried to do something like this:

...
 15                        t:typeSpec[false]        // method or variable declaration(s)
 16                        (    methId:IDENT{                // the name of the method
 17                            System.out.println(methId_AST.getText());
 18                            }
 19
 20                            // parse the formal parameter declarations.
 21                            LPAREN! 
...

But, this approach induces to a compile error ("The method add(AST) in the type ASTArray is not applicable for the arguments (int)").
I noticed that when i have the line 16 as "(    IDENT{" only, no compile error occur. But, when i try to bind some reference to the IDENT token (i.e.:"(    methId:IDENT{"), I got that compile error message...
Analyzing the Parser class generated by ANTLR, I noticed the following:

   When I do not try to bind the IDENT token to a reference, ANTLR bind the IDENT token to a temporary AST variable, and use this variable to create the node for the enclosing grammar rule "classField"
   When I do, ANTLR indeed bind the IDENT token to my variable "methId", but this variable isn't used to create the grammar rule node. Antlr try to pass directly the IDENT constant to the creation of the node.

My questions is: How can I have a reference to the corresponding IDENT token? How can I avoid that "mess" when I try to reference the token using the "methId:IDENT" form? How can I grab the name of the methods w/o producing compile errors?
Please, help me!

My best regards!
Mantu






 		
---------------------------------
 Você quer respostas para suas perguntas? Ou você sabe muito e quer compartilhar seu conhecimento? Experimente o Yahoo! Respostas!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20061121/5e345679/attachment-0001.html 


More information about the antlr-interest mailing list