Friday Dec 05, 2008
Saturday Oct 11, 2008
By Yang Jiang on Oct 11, 2008
The Compiler Grammar workspace is a branch from the main Openjdk workspace. It integrates an Antlr parser which builds the same AST trees as javac does.
To build the workspace, you need the following prerequisites:
Antlr-3.1.1+ is needed. At the time this document is written, 3.1.1 is the latest version.
Click this link to download the file directly. Or go to the Antlr website to get the latest version. You need the “Complete ANTLR 3.1.1 jar”, which contains the runtime, and the ANTLR tools.
You must have Ant installed and properly set up so you can execute the command “ant” under command line.
Go to the Ant website to download.
Mercurial is needed to get the latest source code. Or you can download the source archive from here.
You can download Mercurial from here.
Refer to this page for how to install and setup Mercurial.
Once you have everything installed and downloaded, you can start to build the workspace by following the steps listed bellow. It assumes that you are working on a linux or similar OS, although windows users should be able to build the workspace same way.
1) Create a work directory, for example "/workspace".
2) Place the downloaded antlr-3.1.1.jar under a “lib” directory, for example "/home/lib"
NOTE: At the time this document is written, antlr-3.1.1 is the latest version and the build file is configured to search for a file with the name “antlr-3.1.1.jar”. You will have to change the file name accordingly in the build.xml if new antlr version comes out.
ALSO NOTE that the antlr-3.1.1.jar must be placed under a “lib” directory. You have to set the parent directory of that "lib" directory to the antlr.home property. In the case of “/home/lib”, the antlr.home has to be set to “/home”
3) Go to "/workspace", run
hg clone http://hg.openjdk.java.net/compiler-grammar/compiler-grammar/langtools
this will download all the source into "/workspace/langtools"
Or optionally, if you don't have Mercurial installed, you can download an archived version of the workspace from here.
4) Create a new file "/workspace/langtools/build.properties" with the following content
boot.java.home = /absolute_path_to_your_jdk_home
antlr.home = /absolute_path_to_your_antlr_home
5) Go to /workspace/langtools/make, run
This will build the workspace.
6) Run the new javac. Go to "/workspace/langtools/", run
dist/bootstrap/bin/javac -XDantlrdebug=true -XDparser=antlr /path_to_any_dot_java_file
You should see “Parsing with antlr” printed out as a result of the -XDantlrdebug option. Also, try to set "-XDparser=default" to use the default javac parser.
Sunday Jun 15, 2008
By Yang Jiang on Jun 15, 2008
With the current antlr java grammar, this is parsed by making various sub-rules, which are reflected into varous method calls in the parser code:
ID ( '&&' ID )\*
AndExpression( '||' AndExpression)\*
OrExpression '==' OrExpression
This handles operator precedence very well and is easy to read and code, but with each expression (even a single identifier) being parsed by making many method calls, speed is going to be a problem.
Optimization is possible by writing a grammar like
ID (('&&' | '||' | '==') ID)\*
This doesn't handle precedence very well, but this way it is possible to parse the expression with stacks, thus not that many method calls, with more space requirement though :)
And I'm also expecting the new antlr feature could help here, which gives operators precedence by the sequence they are listed.
Only I'm not sure if it is still been translated into many sub-rules, or if it is implemented in a stack based way?