1
votes

I found in GitHub a repository from antlr4 that has the complete grammar file for Java 8 which is here: https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4

If I use antlr4 it will take the grammar and create the lexer, the parser etc. But this is not enough to replicate the existing Java compiler.

We need Java code that will hook into the antlr4-generated classes (the lexer, parser, etc.) So that we can convert an input source file to byte code. There is a project in GitHub called minijava where the author created a compiler for a subset of Java which is here: https://github.com/csaroff/MiniJava-Compiler?files=1

Is there compiler code like the minijava project, but for the whole Java 8 grammar?

1

1 Answers

2
votes

The question is what you want to accomplish. If you really want to get into the compiler ... JDK is open source (http://download.java.net/openjdk/jdk8/). Probably not with ANTLR, but maybe your best bet. Another option is the java compiler from eclipse, open source, too.

But probably you do not really want to fiddle into a compiler ... but only want to hook into the compile process to generate some additional code? Then Java Annotation processors (https://docs.oracle.com/javase/8/docs/api/javax/annotation/processing/Processor.html) are what you are looking for.

Or maybe you want to postprocess an already created class? Then maybe go with some byte code generating/manipulating framework (e.g. http://jboss-javassist.github.io/javassist/).

I think there is no "complete ANTLR grammar with the rest of a compiler" project.