[antlr-interest] Help - Populating AST with Type Info

John B. Brodie jbb at acm.org
Sat Mar 27 18:33:16 PDT 2010


Greetings!

First let me say that I am really confused by your example, so my reply
below may not be of any use to you.... sorry

On Sat, 2010-03-27 at 20:32 -0400, William Koscho wrote:
> Hi All,
> 
> I'm trying to build an AST with type information, but the grammar I'm
> using is ambiguious, and I'm not sure how to fix it.  The grammar
> below can match the input string "component abc { }" using rule
> componentDef or interfaceDef.
> 
> How can I fix this and still populate the AST with Type information?
> 
> elementDef : componentDef | interfaceDef ;
> 
> componentDef : ( type ID '{' interfaceRef* '}' ) -> ^(type ID interfaceRef*);
> 
> interfaceDef : ( type ID '{' messageDef* '}' ) -> ^(type ID messageDef*);
> 
> type returns [Type t] :
>      'component' { $t = (Type)m_symbolTable.resolve("component"); }
>     | 'interface' { $t = (Type)m_symbolTable.resolve("interface"); }
>     | 'message' { $t = (Type)m_symbolTable.resolve("message"); } ;
> 

But neither the componentDef nor the interfaceDef rules utilize the
returned value from the type rule. Simply returning a Type from the type
rule does not mean that it gets magically included in the AST (AFAIK).

So I do not see how you are populating your tree with information from
the symbol table.

On the other hand, I think that you may simply resolve your ambiguity by
hoisting the 'component' keyword into your componentDef rule. And you
may also deal with the symbol table therein.

something like (untested):

componentDef :
@init{ Type t = null; }
    kw='component' { t = (Type)m_symboltable.resolve($kw.text); }
        ID '{' interfaceRef* '}' 
    -> /* need to use t here somehow...*/ ^($kw ID interfaceRef*)
    ;

Please, Please, PLEASE try to post the smallest yet complete example of
your issue when asking for help. Thanks.
   -jbb




More information about the antlr-interest mailing list