[antlr-interest] [antlr v3]: literal problem

Terence Parr parrt at cs.usfca.edu
Tue Mar 13 15:29:23 PDT 2007


On Mar 9, 2007, at 6:18 PM, chen jw wrote:

> HI All,
>
> I am trying to implement a MiniJava compiler and meet trouble when  
> I get started with antlr v3.
>
> rule:
>
> exp: INTEGER_LITERAL;
>
> INTEGER_LITERAL: '0' | '1'..'9' ('0'..'9')*;
>
> INT    :    ('0' | '1'..'9' '0'..'9'*) ;

0 is matched by both rules.  Always becomes INTEGER_LITERAL here.

> ...
>
> when I use a number that greater than 9, it's fine, but complain  
> "no viable alternative at input '1' " (if I use 1)
>
> another problem is as follow:
>
> rule:
> program    :    mainClassDecl (classDecl)*;
>
> mainClassDecl
>     :    'class' id '{'
>             'public' 'static' 'void' 'main' '(' 'String' '[' ']' id  
> ')' '{'
>                 statement
>             '}'
>         '}'
>     ;
> when I use the file below:
>
> class Factorial {
>     public static void main(String[] args) {
>         System.out.println (new Fac().CompuFac(9));
>     }
> }
>
> the parser will complain: mismatched char 'F' expecting 'b'; ( I  
> found it want to find 'public' but not an id)
>

Does id allow uppercase letters?
Ter



More information about the antlr-interest mailing list