[antlr-interest] ANTLR first time user getting error

Bart Kiers bkiers at gmail.com
Sat Sep 11 23:57:24 PDT 2010


On Sun, Sep 12, 2010 at 1:51 AM, Dibyendu Majumdar
<mobile at majumdar.org.uk>wrote:

> Hi,
>
> I am trying to use ANTLR for the first time, and am getting an error when
> trying to run the grammar shown below; I would be grateful for any help in
> identifying what the issue is.
>
> The test input I am using is:
>
> package test;
>
> I am using the Eclipse ANTLR IDE plugin and when I try to interpret above,
> I
> get:
> MimatchedTokenException: Line 1:7 mismatched input '' expecting '\u0006'
>
>

Here's the same grammar except with a little main method in the parser:

grammar Problematic;

@members {
    public static void main(String[] args) throws Exception {
        String source = "package test;";
        ANTLRStringStream in = new ANTLRStringStream(source);
        ProblematicLexer lexer = new ProblematicLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ProblematicParser parser = new ProblematicParser(tokens);
        parser.compilationUnit();
    }
}

compilationUnit
   :   packageDeclaration
   ;

packageDeclaration
   :   PACKAGE qualifiedName SEMI
   ;

qualifiedName
   :   IDENTIFIER (DOT IDENTIFIER)*
   ;

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

PACKAGE
   :   'package'
   ;

SEMI
   :   ';'
   ;

DOT
   :   '.'
   ;

IDENTIFIER
   :   ('a'..'z'|'A'..'Z'|'_'|'$') ('a'..'z'|'A'..'Z'|'_'|'0'..'9'|'$')*
   ;


and when generating a parser/lexer from the grammar, and running the
parser (under Windows, replace the colons in the commands below by
semi-colons.):

java -cp antlr-3.2.jar org.antlr.Tool Problematic.g
javac -cp .:antlr-3.2.jar *.java
java -cp .:antlr-3.2.jar ProblematicParser


there are no problems whatsoever.
So there must be something wrong with your Eclipse plugin and not with ANTLR
itself.

Regards,

Bart.


More information about the antlr-interest mailing list