[antlr-interest] help - Parser problem

Gavin Lambert antlr at mirality.co.nz
Tue Dec 2 22:43:13 PST 2008


At 18:28 3/12/2008, virg g wrote:
>ok. Is there anyway i can mention the commands and its paramters 
>without mentioning them in the quotes?

Yes -- either declare a named token for it instead (as you did for 
TESTCMD) or define an explicit lexer rule:

   CMD : 'CMD';

Then you can refer to it as CMD in all the parser rules instead of 
'CMD', and it'll have a nicer name in the generated code to boot.

Eliminating quoted constants from parser rules like this will also 
let you group all of your lexer rules together in one place, 
making it easier to spot surprises and harder to forget that ANTLR 
is not a scannerless parser.

>The literal rule is combination of "OR" of Number, ALPHA and 
>STRING. So for the command " TESTCMD CNT=2 VAL=TYPE:CNT" where 
>for VAL  paramters CNT will match ALPHA lexer rule right? as it 
>works if it is "TEST". Otherwise i have to add expilcitely all 
>the quoted values to "literal" rule like this. sorry if 
>understanding is wrong.
>
>literal    : (NUMBER|ALPHA|STRING |TESTCMD|'CNT'|'VAL'|'TYPE')

Nope, that's exactly right (that you do have to explicitly mention 
them, I mean).  As each of those are unique token types, if you 
want 'literal' to match all of them then you have to actually 
mention all of them.

Despite the string "CNT" being *compatible* with the rule ALPHA, 
the simple fact is that when the lexer ran it didn't generate an 
ALPHA, it generated a CNT (or a T14, or whatever).  The parser 
doesn't know that this is acceptable where an ALPHA is unless you 
tell it.




More information about the antlr-interest mailing list