[antlr-interest] Re: SQL string literals

glindholm glindholm at yahoo.com
Fri May 31 20:24:39 PDT 2002


With K=2 I believe this will do what you want. It will allow doubled-
up quotes within a quoted string.

options {
    k=2;
}

STRING
    :   '"'  ( ('"'  '"')   | ~('"'))*  '"'
    |   '\'' ( ('\'' '\'')  | ~('\''))* '\''
    ;


(This might be the same as your original attempt but you need to set 
k=2. :)

Greg
--- In antlr-interest at y..., Ric Klaren <klaren at c...> wrote:
> Hi,
> 
> On Fri, May 31, 2002 at 04:43:59PM +0100, Pete Forman wrote:
> > At 2002-05-31 07:57 -0700, mzukowski at y... wrote:
> > >I think a syntactic predicate would do the trick.  Try this:
> > >
> > >STRING
> > >    : '"'! (~('"')|('"''"')=>'"''"'!)*  '"'!
> > >    ;
> >
> > sql.g:4: warning: lexical nondeterminism upon
> > sql.g:4:        k==1:'"'
> > sql.g:4:        between alt 2 and exit branch of block
> 
> To sum up a few of the already posted things (I think the amount 
of '"'""'
> etc. confused me here and there ;), so I rephrased/rewrote a few 
bits) :
> 
> You can :
> 
> STRING
> : '"'!
> 	( { LA(2) == '\"' }? "\"\""! { $append('\"'); }
> 	| ~'"'
> 	)*
> 	'"'!
> ;
> 
> This gives a warning but that is ok. As you look at the code it 
does the
> right thing. So you can turn of the warning:
> 
> STRING
> : '"'!
> 	( options { warnWhenFollowAmbig=false; } : 
> 		{ LA(2) == '\"' }? "\"\""! { $append('\"'); }
> 	| ~'"'
> 	)*
> 	'"'!
> ;
> 
> Also:
> 
> STRING
> : '"'!
> 	( ("\"\"") => "\"\""! { $append('\"'); }
> 	| ~'"'
> 	)*
> 	'"'!
> ;
> 
> Works but gives a warning. (slightly less efficient as the 
previous one)
> Also here it's possible to turn of the warning...
> 
> Or:
> 
> Increase the lookahead.
> 
> class YourLexer extends Lexer;
> options { k = 2 }
> 
> STRING
> : '"'!
> 	( "\"\""! { $append('\"'); }
> 	| ~'"'
> 	)*
> 	'"'!
> ;
> 
> It is common to turn of warnings for 'known-to-be-good' cases. 
Usually a
> quick glance at the generated code is enough to verify if this is 
valid.
> 
> HTH,
> 
> Ric
> --



 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list