[antlr-interest] String literals - what am I missing?

Norman Dunbar Norman at dunbar-it.co.uk
Fri Dec 16 02:46:25 PST 2011


Morning.

I'm attempting to write my first grammar. I am using ANTLRWorks 1.4.3 
with ANTLR 3.4 built in.

I created a new grammar and had the system auto generate a double quote 
delimited STRING for me, as follows:

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

As I don't need or want escape sequences and backslashes are not special 
characters, I've amended it slightly to the following including a 
separate option for single quote delimited strings:

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

In theory, if I've got what I think is my reg-exp correct, a string 
starts and ends with a single or double quote, and has zero or more 
characters between except the single or double quote delimiter.

Unfortunately, the debugger allows "abc " def" or 'abc ' def' as valid 
strings.

The bit in the middle, which I think should be disallowing the delimiter 
within the string, is auto generated by ANTLRWorks - wel, the part of it 
that I'm using is.

Any help gratefully received. It's bound to be simple and staring me in 
the face, but I can't see it. :-(


Cheers,
Norm.

-- 
Norman Dunbar
Dunbar IT Consultants Ltd

Registered address:
Thorpe House
61 Richardshaw Lane
Pudsey
West Yorkshire
United Kingdom
LS28 7EL

Company Number: 05132767


More information about the antlr-interest mailing list