[antlr-interest] Help for a newbie please!

Gavin Lambert antlr at mirality.co.nz
Thu Jun 26 01:14:37 PDT 2008


At 03:28 26/06/2008, Peter Seban wrote:
>I'm a newbie in antlr.
>I have tried this example grammar by input text:
>
>apples= 100 * peaches;             // this is a simple expression
>*apples= 100 * peaches;           //  this is a comment line
[...]
>expr2
>        :
>        asterisk ~('\r'|'\n')* ';'
>        ;

This is not going to do what you think it will do.  Firstly, this 
will introduce new lexer rules for '\r' and '\n', and then accept 
any sequence of tokens that aren't one of those two.  You probably 
should be using a lexer rule instead.  (Lexer rules have an 
initial capital letter.)

>asterisk
>        :
>        {input.LT(-1).getText().toLowerCase().equals("\n")}?
>        {input.LT(0).getText().toLowerCase().equals(";")}?
>        {input.LT(1).getText().toLowerCase().equals("*")}? '*'
>        ;

Why are you calling toLowerCase() when you're comparing against 
characters that aren't case-sensitive?

Also, your chain of predicates doesn't match your example 
input.  You appear to have the \n and ; reversed.  (Also, you 
should consider cases such as when the comment is the first 
expression.)

>multiplication
>        :
>        {input.LT(1).getText().toLowerCase().equals("*")}? '*'
>        ;

Why are you using this predicate?

>WS     : (' '|'\t'|'\r'|'\n')+ {skip();};

This is ambiguous with the '\r' and '\n' implicit rules created by 
using string constants in the expr2 rule.  Yet more reason why 
expr2 ought to be a lexer rule instead.  (Having said that, you're 
still going to get ambiguity problems between this and the 
multiplication expressions, so you'll need to put in some code to 
detect and generate either a comment token or a multiplication 
token as appropriate.  Do you really really need to use * as a 
comment indicator?)

>WCHAR  : ~('*'|'='|'('| ')'|'"'|' '|'\t'|'\n'|'\r'|'#'| ';')*;

It is illegal to define a lexer rule that can match zero 
characters (because this can generate infinite loops).  Change the 
* to a +.



More information about the antlr-interest mailing list