[antlr-interest] Ultimately Frustrated

Raymond J. Schneider rschneid at bridgewater.edu
Sat Feb 14 07:32:11 PST 2009


Thanks tremendously Robert ... that was exactly my problem although I was totally oblivious to it.

Actually I wasn't expecting anything ... I'm not particularly familiar with packages and jar files so the significance of those lines in the grammar shot right over my head.  I'm working through "The Definitive ANTLR Reference" book with my Compiler Design class and we are all total newbies to ANTLR and are only in chapter 3.

Thanks Robert ... you got me on the right track all right.  It was still confusing because there were two lines in the grammar that referred to package test and I only caught the first one the first time.
In my Expr.g grammar I just made the following modification at the top:
---
grammar Expr;

@header {
/*package test;*/
import java.util.HashMap;
}

@lexer::header {/*package test;*/}
...
---
Then it stopped looking for the lexer and parser in another file.  Then the whole thing worked.  A blow by blow account follows and may help other newbies.
---
1. Removed package test from Expr.g
2. ran 
java -cp antlrworks-1.2.2.jar org.antlr.Tool Expr.g 
3. found a package test; line in the generated ExprLexer.java file and eliminated it
4. compiled with
I:\E>javac -cp .;antlrworks-1.2.2.jar Test.java ExprLexer.java ExprParser.java
Note: ExprParser.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
 

5.And ran it I:\E>java -classpath .;antlrworks-1.2.2.jar Test
a=3
b=4
a+b
^Z
7

I:\E>


6. Made a small test file input with
I:\E>copy con input
a=3
b=4
c=a+b
a*b
c*a
c
^Z
        1 file(s) copied.

7. Then ran it with 
I:\E>java -classpath .;antlrworks-1.2.2.jar Test < input
12
21
7

I:\E>

8. Changed a line in ExprLexer to @lexer::header {/*package test;*/} to comment out the package test; line
and ran the following
I:\E>java -cp antlrworks-1.2.2.jar org.antlr.Tool Expr.g
ANTLR Parser Generator  Version 3.1.1

I:\E>javac -cp antlrworks-1.2.2.jar;. Test.java ExprLexer.java ExprParser.java
Note: ExprParser.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

I:\E>java -cp antlrworks-1.2.2.jar;. Test < input
12
21
7

I:\E>
----
I think I've got this now ... at least until the next glitch.
Thanks very much again Robert and all.  Great resource and I'm looking forward to getting proficient enough to use it for some nice DSLs.

Regards, Ray
--
Ray Schneider,PE, Ph.D
Associate Professor
Math and Computer Science
Bridgewater College
http://www.bridgewater.edu/~rschneid/
http://theweedlessgarden.blogspot.com
________________________________________
From: Robert Soule [robert.soule at gmail.com]
Sent: Friday, February 13, 2009 23:56
To: Raymond J. Schneider
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Ultimately Frustrated

It looks like you are expecting your files to be in the package "test".
I would remove the line "package test;" from your grammar. Alternatively,
you could make a directory "test", and move all the files that expect to
be in the package "test" into the test directory.



More information about the antlr-interest mailing list