[antlr-interest] Quoted String Literal - confused by greed=false behavior.

G. Richard Bellamy rbellamy at pteradigm.com
Wed Jul 27 13:06:40 PDT 2011


Thanks to both of you for your help.

Clearly I understated things when I said I was confused.

1. I was under the impression that greedy=true was the default, in every 
case. For instance, in "The Definitive ANTLR Reference" it mentions the 
lexer rule ML_COMMENT:

ML_COMMENT
     : '/*' ( options { greedy=false; } : . )* '*/'
     ;

Which is definitely using the .* catch-all.

2. I've tried both variations, and am still getting an exception: 
mismatched character '<EOF>' expecting '"'


On 7/27/2011 12:57 PM, Bart Kiers wrote:
> And by default, greedy=true (except with .* and .+), so in this case, one
> could simply write
>
> STRING_LITERAL
>    :  '"' ('""' | ~'"')* '"'
>    ;
>
>
> AFAIK.
>
> Regards,
>
> Bart.
>
>
> On Wed, Jul 27, 2011 at 9:54 PM, Sam Harwell<sharwell at pixelminegames.com>wrote:
>
>> You're reading the greedy option in reverse. :) I'd write the rule this
>> way:
>>
>> STRING_LITERAL
>>         :       '"'
>>                 (       options{greedy=true;}
>>                 :       '""'
>>                 |       ~'"'
>>                 )*
>>                 '"'
>>         ;
>> Sam
>>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org
>> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of G. Richard Bellamy
>> Sent: Wednesday, July 27, 2011 1:49 PM
>> To: antlr-interest at antlr.org
>> Subject: [antlr-interest] Quoted String Literal - confused by greed=false
>> behavior.
>>
>> I've got a lexer rule that should be gobbling everything after the double
>> quote '"' except for the last double quote - I basically stole the rule
>> from
>> a post from Jim Idle
>> (http://www.antlr.org/pipermail/antlr-interest/2010-March/038051.html).
>>
>> I've also tried other variations on the same rule, and I'm a bit confused
>> as
>> it seems the {greedy=false;} option is being ignored.
>>
>> Any help is appreciated
>>
>> -----------------------------------------------
>> INPUT: @"(FOO="")"
>> -----------------------------------------------
>>
>> lexer grammar Lexer
>>
>> options
>> {
>>      language=CSharp3;
>>      TokenLabelType=CommonToken;
>> }
>>
>> DQUOTE : '"';
>>
>> STRING_LITERAL
>>      : DQUOTE (options { greedy = false; }
>>           : (
>>              (
>>                  {input.LA(1) == '"'&&  input.LA(2) == '"'}? DQUOTE DQUOTE
>>                  | ~DQUOTE
>>              )*
>>          )
>>      )
>>      DQUOTE
>>      ;
>> -----------------------------------------------
>>
>> LEXER TRACE (excerpt):
>> enter STRING_LITERAL " line=1:7
>> enter DQUOTE " line=1:7
>> exit DQUOTE ) line=1:8
>> enter DQUOTE ? line=1:9
>> exit DQUOTE ? line=1:9
>> exit STRING_LITERAL ? line=1:9
>> line 1:10 mismatched character '<EOF>' expecting '"'
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
> 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