[antlr-interest] How to recognize a String

Gabriel Miro gsmiro at gmail.com
Mon Sep 12 06:45:14 PDT 2011


Eric,

Here's the grammar I'm using:

grammar test;

rule    :    STRING;

RPAREN    :    ')';
LPAREN    :    '(';

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;

CHAR:  '\'' ( ESC_SEQ | ~('\''|'\\') ) '\''
    ;

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

fragment
HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    |   UNICODE_ESC
    |   OCTAL_ESC
    ;

fragment
OCTAL_ESC
    :   '\\' ('0'..'3') ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7') ('0'..'7')
    |   '\\' ('0'..'7')
    ;

fragment
UNICODE_ESC
    :   '\\' 'u' HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT
    ;

Please not that, apart from the parenthesis, the rest is generated by
ANTLRWorks. Using the interpreter to match the string ' ' (single quotes and
one space, or any letter between the quotes), I get a
MismatchedTokenException(4!=10). It only matches '' (single quote without
space).

I tried removing the CHAR rule and then, for the same input, I get:

MismatchedTokenException(-1!=9)

and in the console

problem matching token at 1:3 NoViableAltException('?'@[()* loopback of
17:13: ( ESC_SEQ |~ ( '\\' | '\'' ) )*])

I tried using the debugger, but on Windows I get a javac, cannot create
process error and In my Mac, I get compile time errors in the generated
classes because it cannot find antlr classes (probably  just a classpath
problem). Is there any documentation on how to use AntlrWorks features? The
site documentation is pretty shallow and I cannot find any tutorials.

Regards,
Gabriel Miró
ANTLR Newbie

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of The Researcher
> Sent: Friday, September 09, 2011 2:46 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] How to recognize a String
>
> Hi Gabriel,
>
> It would help if you could post your entire grammar and the exact error
> message here.
>
> Also, don't you mean
>
> LPAREN: '(";
> RPAREN: ')';
>
> Don't worry we have all been there.
>
> Thanks, Eric
>


More information about the antlr-interest mailing list