[antlr-interest] Rewriting an AST with the same type listed twice

Bart Kiers bkiers at gmail.com
Thu Aug 25 09:15:48 PDT 2011


Hi Garry

The operator += creates a List of tokens, just use constValue+ in your
rewrite rule. And the literal token 'constval' is never used in your parser
rule, so you can't use it in a rewrite rule (this is what the error is
about).

Try this using an imaginary token (see the 'tokens { ... }' block below):

grammar YourGrammar;

options {
  ...
}

tokens {
  CONSTVAL;
}

...

constList
  :  '[' constValue (',' constValue)* ']' -> ^(CONSTVAL constValue+)
  ;

...


Regards,

Bart.

On Thu, Aug 25, 2011 at 5:48 PM, Garry Watkins <garry at dynafocus.com> wrote:

> Thanks for the quick response.  However I cannot get it to recognize the
> 'map' '<' construct.  That is ok.
>
> Next question.  I am trying to re-write this but it complains about this
> with a "reference to undefined token in rewrite rule."
>
> constList
> : '[' cv+=constValue (',' cv+=constValue)* ']' -> ^('constval' $cv+)
>  ;
>
>
> On Thu, Aug 25, 2011 at 11:25 AM, Bart Kiers <bkiers at gmail.com> wrote:
>
>> Hi Garry,
>>
>> You either use inline operators ^ and !, or use a rewrite rule, not both.
>> To reference a rule, add a $ before its label:
>>
>> mapType
>>   :  'map' '<' k=anyType ',' v=anyType '>' -> ^('map' $k $v)
>>   ;
>>
>> but I think this should work too:
>>
>> mapType
>>   :  'map' '<' anyType ',' anyType '>' -> ^('map' anyType anyType)
>>   ;
>>
>> Regards,
>>
>> Bart.
>>
>> On Thu, Aug 25, 2011 at 5:22 PM, Garry Watkins <garry at dynafocus.com>wrote:
>>
>>>  mapType
>>>
>>> : 'map' '<'! anyType ','! anyType '>'!
>>> | 'map<' k=anyType ','! v=anyType '>'! -> ^('map' k v)
>>> ;
>>>
>>>
>>> I am trying to re-write the AST generated on this alternative:
>>>
>>> | 'map<' k=anyType ','! v=anyType '>'! -> ^('map' k v)
>>>
>>>
>>> How does one alias the anyTypes?  I am getting an error on the code
>>> above?
>>>
>>> However the bigger question is my parser won't recognize the following
>>> map<string,string> if it uses just the first line in my rule.  However,
>>> it
>>> will recognize map <string,string>.
>>>
>>> Thanks
>>> Garry
>>>
>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>>> Unsubscribe:
>>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>>
>>
>>
>
>
> --
> Garry Watkins
> Managing Director
> DynaFOCUS, LLC
> (843) 608-8004 (google voice)
> (843) 276-6808 (cell)
>
>


More information about the antlr-interest mailing list