I'm looking for a java library that allows me to parse a java source file and that gives me an AST representation of the code.
Actually I'm only interested in the class and method definitions with their annotations. I don't need the AST of the method code.
I'm using this information for code generation. This is why I can't compile the source file first to get the information from the resulting class file. The code wouldn't compile without errors until I generate some additional classes.
ANTLR. It is a popularly used context-free parser generator in Java. ANTLR can also generate parsers for other languages. ANTLR grammar contains two parts - the lexer and the parser rules.
The first step in writing a parser is to tokenize the input string. This means to separate the input string into short bits that represent the basic entities in the expression. We could do this by hand, reading one character at a time and assembling the tokens character by character.
JavaCC (Java Compiler Compiler) is an open-source parser generator and lexical analyzer generator written in the Java programming language. JavaCC is similar to yacc in that it generates a parser from a formal grammar written in EBNF notation.
SimpleDateFormat parse() Method in Java with Examples The parse() Method of SimpleDateFormat class is used to parse the text from a string to produce the Date. The method parses the text starting at the index given by a start position.
Java 6 supports this as a native part of the compiler and has standard APIs for it (javax.lang.model
). You can read up on it here. It is designed specifically for your use-case (i.e. code generation from annotations and source).
ANTLR (http://www.antlr.org/) has a parser for the Java language and it also suports ASTs.
try
options {
output=AST;
}
in the *.g file (I haven't tried it personally);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With