[antlr-interest] A simple question in my antlr code. Is it a antlr code bug or some thing wrong with my code

Gavin Lambert antlr at mirality.co.nz
Thu Jun 26 06:17:05 PDT 2008


At 00:58 27/06/2008, Gnanasegaran Vinayagamoorthy wrote:
>I am a new antlr user. I have made a simple code to decode a 
>string. Here comes the code.
[...]
>columnkey :  (   '5'         {System.out.println("digit1 : " + 
>"5");}     |
>                                     '6'         {}          |
>                         '7'         {}          |
>                         '8'         {}          |
>                         '9'         {}          |
>                         '10'       {}          |
>                         '12'       {}
>             );

When you use a quoted string constant in a parser rule (not a 
lexer rule) like this, it implicitly creates a token (ie. a lexer 
rule) that recognises that specific character sequence.  If you're 
not careful, this can introduce significant ambiguity, and is 
often not what you actually want (though it is occasionally useful 
for keywords or structural punctuation).

My general advice (at least when starting out) is to maintain 
strict separation between lexer and parser -- *never* use a quoted 
constant in a parser rule.  If you want to guarantee that you 
can't get them mixed up, then avoid the combined grammar entirely 
and just make separate lexer and parser grammars.



More information about the antlr-interest mailing list