[antlr-interest] Java doesn't find class definition

Johannes Luber jaluber at gmx.de
Wed Apr 4 06:27:05 PDT 2007


Hello,

Sorry for the lateness, but I needed to do some additional experiments
to exclude some potential bug causes.

Martin Wood-Mitrovski wrote:
>> I can get the ANTLR tools running and in Eclipse I can start those
>> programs, too, but for whatever reason it doesn't work on the command
>> line. As Eclipse doesn't compile some projects because it believes that
>> a class doesn't have all referenced members, 
> 
> you should be able to get everything running from eclipse just fine.

Eclipse has a bug which sometimes causes it not to load all available
class definitions. A friend of mine experiences this behaviour for a
project, which works on three other machines just fine. So when this bug
appears, I have to uses the javac on the command line. Furthermore, the
examples in the Beta Book read always from System.in and I haven't found
a way to do this while running Eclipse.

> How are you generating the code from your grammar? are you using ANT ?

I use ANTLRworks for generating my grammar - but it doesn't work
strangely for the 2pass grammar from the Beta Book, when I want to
generate the tree parser. Not sure why it doesn't locate the right
tokens file, as the path copied from the error message works fine.

A bit background info regarding my project: I attempt to create a C#
parser. As the C# support is lacking, I decided to check if for my needs
a Java implementation is sufficient. Being more a C# programmer than
Java I'm not that familiar with Eclipse, so I may overlook available
options. Because I was reading the Beta Book I've decided to test the
2pass grammar, where I stumbled upon my problem. In preparation of this
email I tested my own grammar (as far I got with it). I have the same
problem there - and found at least one ANTLR bug... Arrg. The last part
of my email shows the details.

Regarding ANT: For the few files I don't need ANT, just a batch file,
as when I'll switch back to C# I can run that batch from VS2005. So it
would be more useful to me, if I could run a Java program from the
command line.

> This might be obvious but you have to manually refresh the resource
> tree after running an ANT build otherwise eclipse doesnt notice the
> changes.

In my case it didn't help. The ExprLexer class inherits ruleNestingLevel
from Lexer, but Eclipse doesn't find it. And if Eclipse doesn't find it,
the file won't be compiled in Eclipse. In such cases it looks easier to
compile the files on the command line, too.

Regarding the ANTLR bugs mentioned above: The generated file creates
three errors. I've used the most recent build of March 30th for ANTLR to
generate the file.

The rule

NEW_LINE
	:	'\u000D' // Carriage return character
	|	'\u000A' // Line feed character
	|	'\u000D\u000A' // Carriage return character followed by line feed
character
	|	'\u0085' // Next line character
	|	'\u2028' // Line separator character
	|	'\u2029' // Paragraph separator character
	;

generates for the '\r\n'/'\u000D\u000A' alternative:

                case 3 :
                    //
D:\\Studium\\Diplomarbeit\\CSharpML\\CSharp3.g:65:4: '\\u000D\\u000A'
                    {
                    match("\u000D\u000A");
                    }
		    break;

I receive two errors from javac:

"CSharp3Lexer.java:1485: unclosed string literal
                    match("\u000D\u000A");
                          ^
CSharp3Lexer.java:1485: unclosed string literal
                    match("\u000D\u000A");
                                       ^"

Can't one use unicode escape sequences in a string? Or do I have to use
a workaround for the alternative like '\u000D' '\u000A' or '\r\n' or
'\r' '\n'?

The last error is for the rule

SIMPLE_ESCAPE_SEQUENCE
	:	'\\\''
	|	'\\\"'
	|	'\\\\'
	|	'\\0'
	|	'\\a'
	|	'\\b'
	|	'\\f'
	|	'\\n'
	|	'\\r'
	|	'\\t'
	|	'\\v'
	;

This is the generated code for '\\\"':

                case 2 :
                    //
D:\\Studium\\Diplomarbeit\\CSharpML\\CSharp3.g:507:4: '\\\\\\\"'
                    {
                    match("\\\\"");

For whatever reason there is now one backslash more than required.

Thanks in advance,
Johannes Luber



More information about the antlr-interest mailing list