[antlr-interest] 3.1b1 trouble with a CSharp2/AST example
Link, Hamilton
helink at sandia.gov
Fri May 23 13:32:13 PDT 2008
Okie dokie... just a quick post by way of keeping track of what I'm doing.
Once I got 3.1b1 rebuilding, I was satisfied that I should be able to build and test a Java-targeted parser. I took the CSharp2-targeted .g file and changed the grammar name and the target:
grammar TestJava;
options { language = Java; output = AST; }
tokens { MULT; } // imaginary token
start: term ('+'^ term)*
;
term: INT ID -> ^(MULT["*"] INT ID)
| INT exp -> ^(MULT["*"] INT exp)
| exp
| INT
| ID
;
exp : ID '^'^ INT
;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\t'|'\r'|'\n')+ {$channel = HIDDEN;} ;
and used ANTLR to build the TestJavaLexer.java and TestJavaParser.java files. Then I took my C# test program and converted it to Java (back to Java, really, as I took the program from the web site to begin with):
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.runtime.debug.*;
public class Program {
public static void main(String[] args) throws Exception {
String testString = "2x+3x^5";
TestJavaLexer lexer = new TestJavaLexer(new ANTLRStringStream(testString));
CommonTokenStream tokens = new CommonTokenStream(lexer);
TestJavaParser parser = new TestJavaParser(tokens);
TestJavaParser.start_return bar = parser.start();
CommonTree t = (CommonTree)bar.getTree();
String result = (t != null) ? t.toStringTree() : null;
System.out.println(result);
}
}
And I built and ran this:
> javac -cp "./antlr-3.1b1/lib/antlr-3.1b1.jar;./antlr-3.1b1/lib/stringtemplate-3.1.jar;./antlr-3.1b1/lib/antlr-runtime-3.1b1.jar;./antlr-3.1b1/lib/antlr-2.7.7.jar;./antlr-3.1b1/lib/gunit-1.0.2.jar" -d . Program.java TestJavaLexer.java TestJavaParser.java
> java -cp ".;./antlr-3.1b1/lib/antlr-3.1b1.jar;./antlr-3.1b1/lib/stringtemplate-3.1.jar;./antlr-3.1b1/lib/antlr-runtime-3.1b1.jar;./antlr-3.1b1/lib/antlr-2.7.7.jar;./antlr-3.1b1/lib/gunit-1.0.2.jar" Program
It printed
(+ (* 2 x) (* 3 (^ x 5)))
as desired, so obviously it's building the AST properly (and it's getting farther than the C# version by merit of not throwing an error during parsing).
So the Java target and Java runtime seem to be OK and C# target generation is a likely suspect. I will do a side-by-side comparison between the FooParser.java and FooParser.cs and see what I can turn up. _If_ I can identify a possible bug I will report it and also see if I can decipher what Johannes was suggesting re: fixing and rebuilding.
h
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080523/ba5b6672/attachment.html
More information about the antlr-interest
mailing list