[antlr-interest] SQL string literals

Ric Klaren klaren at cs.utwente.nl
Fri May 31 09:22:44 PDT 2002


Hi,

On Fri, May 31, 2002 at 04:43:59PM +0100, Pete Forman wrote:
> At 2002-05-31 07:57 -0700, mzukowski at yci.com 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
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- klaren at cs.utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
     Innovation makes enemies of all those who prospered under the old
   regime, and only lukewarm support is forthcoming from those who would
               prosper under the new. --- Niccolò Machiavelli


 

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



More information about the antlr-interest mailing list