[antlr-interest] Problem coding Antlr grammar for strings

Luís Reis luiscubal at gmail.com
Sat Jul 18 12:51:27 PDT 2009


I am coding an Antlr grammar which matches strings.
My first attempt was:

doc    :    STRINGCONST;

STRINGCONST
    :    ('@"' ( options {greedy=false;} : . )* '"') //Accepts lots of
stuff, including newlines
    |    ('"' (
            (
                '\\' ('\\' | '"' | 'n' | 't' | OCTALCHAR)
            ) | (
                ~('"'|'\\'|LINEBREAK)
            )
        )* '"')
    ;

Which matches correctly "", "\\" and "\na" but fails for "abc"(with
MismatchedTokenException).
However, I can not understand *why* it fails for "abc"!
It gets even weirder since

STRINGCONST
    :    ('@"' ( options {greedy=false;} : . )* '"') //Accepts lots of
stuff, including newlines
    |    ('"' (
            (
                '\\' ('\\' | '"' | 'n' | 't' | OCTALCHAR)
            ) | (
                ~('"'|'\\'|LINEBREAK)
            )
        )+ '"')
    ;

works perfectly. What's happening here? What am I missing?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090718/413478cc/attachment.html 


More information about the antlr-interest mailing list