[antlr-interest] N00b question

Thomas Brandon tbrandonau at gmail.com
Wed Aug 20 19:34:59 PDT 2008


Lexing occurs independently of parsing and is not influenced by parser
context. So you need to either combine your VALUE and IDENTIFIER rules
into one or recognise the quotes as part of the VALUE rule. If you
combine the rules you could either detect rules that start with a
digit (not valid identifiers) in the lexer or use actions in the
parser.

Tom.

On Thu, Aug 21, 2008 at 12:04 PM, Anders Karlsson
<anders at globe-trotter.us> wrote:
> Hi,
>
> I have this simple little grammar where I would like to parse an ini-file that format ID="somevalue" on each line
> but I get an error when running in ANTLRworks saying MismatchedTokenException using a simple test line A1="AAA". If I below, change VALUE to be IDENTIFIER again it seems to work
>
> e.g.
>
> expr
>        :       (IDENTIFIER '=' '"' IDENTIFIER '"' )
>        ;
>
> So if somebody could explain why the below grammar doesn't work it would help me understand better.
>
> TIA
> Anders.
>
> grammar inifile;
>
> options
> {
> language=C;
> }
>
> prog : stat+;
>
> stat
>        :       expr NEWLINE
>        ;
>
> expr
>        :       (IDENTIFIER '=' '"' VALUE '"' )
>        ;
>
> // lex rules
>
> IDENTIFIER
>    :   LETTER (LETTER|DIGIT)*
>    ;
>
> VALUE
>    :   (LETTER|DIGIT)*
>    ;
>
> fragment
> LETTER
>    :   '$'
>    |   'A'..'Z'
>    |   'a'..'z'
>    |   '_'
>    ;
>
> fragment
> DIGIT
> :'0'..'9';
>
> NEWLINE
>        :       '\r'? | '\n' | { $channel=HIDDEN; }
>        ;
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>


More information about the antlr-interest mailing list