[antlr-interest] two ways to match nothing

Daniel Killebrew killebrew.daniel at gmail.com
Mon Jan 25 18:10:43 PST 2010


Doh, thanks for pointing that out Kevin. Ignore my silliness, everyone 
:)  I got caught up transcribing a parser into Antlr and overlooked this 
simple, obvious transformation.

Daniel

On 1/25/2010 6:03 PM, Kevin J. Cummings wrote:
> On 01/25/2010 08:52 PM, Daniel Killebrew wrote:
>    
>> Antlr doesn't like it when there are multiple ways to match nothing. It
>> says there's an error in my grammar because the second "alternative"
>> (which is another way to match nothing) will never match.
>> Antlr can enter the optional (...)? element and match nothing, or skip
>> the optional element, thus matching nothing.
>>
>> example:
>>
>> naughty_rule
>>       :    Start (A? List*)? End
>>       ;
>>      
> Why can't you just rewrite naughty_rule as:
>
> good_rule
> 	: Start A? List* End
> 	;
>
> I think the outer ()? is what was confusing antlr....
>
>    
>> Start    :    'start';
>> A    :    'aaa';
>> End    :    'end';
>> List    :    'list';
>>
>> Rewritten so Antlr is happy
>> good_rule
>>       :    Start End
>>       |    Start A List* End
>>       |    Start List+ End
>>       ;
>>
>> While I can rewrite my grammar easily enough, it seems odd that Antlr
>> doesn't recognize that it's trying to match nothing in two different
>> ways, so who cares if it can't match the second alternative. That
>> shouldn't be an error. If it's a warning, I could understand that. To
>> make it the user rewrite their code into something less legible seems to
>> be opposite of the usual 'Antlr way'. Although I guess this would
>> require making the code a little more complicated to detect this special
>> case, so perhaps this was already considered.
>>
>> Cheers
>> Daniel
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>      
>
>    


More information about the antlr-interest mailing list