1
votes

I have downloaded Antlr 3.3 and antlr works, along with Java.g from the Antlr site. I was able to successfully generate the JavaParser, JavaLexer.java and tokens using Antlr works for Java.g. I then mounted the antlr jar in my IDE and was following the following instructions to use it in my code: http://www.antlr.org/wiki/pages/viewpage.action?pageId=789

The first problem arose when the documentation above says to code the following line:

RuleReturnScope result = parser.compilationUnit();

The problem is that parser.compilationUnit() does not return a result.

Then i tried following the example further down under "Parsing a tree", but this is incomplete.

I can't find any good documentation on how to use this library.

Here is what I want to do: -in Java submit a file name, File object or file contents as a String to Antlr and have it return some sort of object that i can navigate in code that will give me things like the imports, methods, class variables, expressions etc.

Basically what the NetBeans IDE 6.9.1 does to for refactoring, like fix imports, go to source, rename variables etc. All i need is the meta data about the class, and i am not sure how to obtain it.

Thanks.

1
There are various Java.g grammars lying around: can you post your (entire!) modified .g grammar file? And what exactly are you trying to extract from .java source files? - Bart Kiers
My grammar is the one from antlr and can be found here: openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g I am trying to extract what i described in my message above (imports, method names, variable names, and method content, themselves parsed as expressions with variables etc) The most important for now is imports but i would like the rest too. - Coder
Do you really need to write this from the ground up? Is this an (academic) exercise, or do you need to do this for your work? If it's the latter, I'd strongly suggest using 3rd party tools or libraries that already do this. For example, it is quite tricky to rename variables in source file: you will need to keep track of all the scopes in the source file, and that's not even half of it when there are public/protected variables that are used by other source files. But, if it's an exercise, I can imagine you doing it the "hard" way, but then at least start with Parr's book about ANTLR. - Bart Kiers

1 Answers

1
votes

To answer your question why compilationUnit does not return any result is because if you don't specify a return type on a rule yourself, the parser rule just gets translated into

public void nameOfTheParserRule() { 
    ... 
} 

If you set the output to AST in the options { ... } section of your grammar, parser rules by default return an object of type RuleReturnScope, which I suspect went wrong in your case, but I am not able to be certain since you did not post your grammar and the link you gave to the grammar Java.g does not generate anything: one needs to do certain things with it before being able to generate a parser and lexer from it. You most likely did the wrong things :).

Best of luck!