[antlr-interest] error on parsing some Java source code - mismatched character '<EOF>' expecting '\n'

Heng Chon TAN hengchon at gmail.com
Thu Jun 26 01:17:28 PDT 2008


Hello everyone,

I'm a novice to parser tool and especially Antlr. I have been spending  
the last week trying to figure out how Antlr works and how I can  
modify the grammar to work on my project. I have some difficulty now  
and I have nowhere to turn to... Therefore I hope someone on this list  
can point me in the direction.

I started with, Java 1.5 grammar for ANTLR v3 - http://www.antlr.org/grammar/1152141644268/Java.g 
, build a main test class and compiles perfectly. I did modified the  
grammar a bit internally.

This is the main class content:
> ANTLRInputStream input = new ANTLRInputStream(inputStream);
> JavaLexer lexer = new JavaLexer(input);
> CommonTokenStream tokens = new CommonTokenStream(lexer);
> JavaParser parser = new JavaParser(tokens);
> parser.compilationUnit()


This is the grammar that I have modified (Please find in the  
attachment, I removed most part of the comments to reduce the size of  
this mail):


It was able to compile and it runs well on most files. However, for  
some of the Java files, I get some funny error, as below:

> line 1:1214 mismatched character '<EOF>' expecting '\n'
> constructor:DebugBag
> line 1:771 mismatched input 'else' expecting '}'
> voidmethod:setUp
> line 0:-1 mismatched input '<EOF>' expecting '}'
> class:DebugBag


Could somebody please guide me on which part of the grammar could have  
causes the exception above?
Another thing to point out is that the grammar is able to recognise  
most of the declarations except the getNext() method. I have tried all  
possible possibilities trying to capture the output from the grammar  
but I just can't find the right grammar section which dealt with this.  
Hopefully someone could point out at which section in the grammar  
handles the getNext() method with a throw keyword.

The code for the DebugBag is as follow:

> package bingo.game;
>
> import java.util.*;
>
> import bingo.shared.*;
>
> class DebugBag implements BagOfBalls {
>
>     static final int ROW = 1;
>     static final int COLUMN = 2;
>     static final int DIAGONAL = 3;
>     static final int REVERSEDIAGONAL = 4;
>
>     static final int rowOrColNumber = 1;
>
>     private Stack balls = new Stack();
>
>     DebugBag() {
>     }
>
>     void setUp(Card c, int callThis) {
> 	if (callThis == ROW) {
> 	    for (int i = 0; i < Card.SIZE; i ++)
> 	        balls.push(c.boardValues[i][rowOrColNumber]);
> 	} else if (callThis == COLUMN) {
> 	    for (int i = 0; i < Card.SIZE; i ++)
> 	        balls.push(c.boardValues[rowOrColNumber][i]);
> 	} else if (callThis == DIAGONAL) {
> 	    for (int i = 0; i < Card.SIZE; i ++)
> 	        balls.push(c.boardValues[i][i]);
> 	} else if (callThis == REVERSEDIAGONAL) {
> 	    balls.push(c.boardValues[0][4]);
> 	    balls.push(c.boardValues[1][3]);
> 	    // don't need to push the free space which is in (2,2)
> 	    balls.push(c.boardValues[3][1]);
> 	    balls.push(c.boardValues[4][0]);
> 	}
>     }
>
>     public BingoBall getNext() throws NoMoreBallsException {
> 	if (balls.size() > 0) {
> 	    return (BingoBall)balls.pop();
> 	} else {
> 	    throw new NoMoreBallsException();
> 	}
>     }
> }
>


Appreciate if someone could give a hand here. Thank you very much!




regards,

Heng Chon TAN

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080626/d1878be2/attachment-0002.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Java.g
Type: application/octet-stream
Size: 20093 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20080626/d1878be2/attachment-0001.obj 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080626/d1878be2/attachment-0003.html 


More information about the antlr-interest mailing list