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

William Koscho wkoscho at gmail.com
Sat Mar 27 20:27:34 PDT 2010


Hi John,

Sorry for the bad example; I haven't used ANTLR in several years and
am just getting back into it, and pointing out that I wasn't using the
return value solved another problem for me.

Your reply did help me. thanks

thanks
Bill

On Sat, Mar 27, 2010 at 9:33 PM, John B. Brodie <jbb at acm.org> wrote:
> 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