[antlr-interest] Problem with rewrite rules with optional non-terminals

Indhu Bharathi indhu.b at s7software.com
Thu Mar 26 20:04:51 PDT 2009


Jim Idle wrote:
> Andreas Meyer wrote:
>> David Jameson schrieb:
>>   
>>> I have the following grammar fragment
>>>
>>> refTag :
>>>                  LSQUARE a=expression (COMMA b=expression)? RSQUARE
>>>                     {
>>>                           //stuff
>>>
>>>                     } -> ^(REFTAG $a  $b)
>>>
>>>                  ;
>>>
>>>
>>> However, if I feed in source that does NOT have the optional comma  
>>> expression clause, the parser crashes because it blindly tries to  
>>> dereference  the stream_b object.
>>>
>>> How can I test whether there was an optional expression and control  
>>> the rewrite rule accordingly to include the $b or not?
>>>     
>>
>> Does ($b)? in the rewrite rule not work?
>>   
> Does not need teh () in fact:
>
>  -> ^(REFTAG $a  $b?)
> The cardinality of the rewrite reference needs to match the 
> cardinality of the element it is referencing.
>
> Jim
>
I guess -> ^(REFTAG expression+) will also work and looks more natural.

- Indhu

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090327/9e1dff29/attachment.html 


More information about the antlr-interest mailing list