As the title says, I want to parse some Java source code in Java. I'm pretty sure there are other java libraries that already perform this, but I couldn't find any.
You'll have to write some code. This code will invoke the parser... it will return you a CompilationUnit: InputStream in = null; CompilationUnit cu = null; try { in = new SEDInputStream(filename); cu = JavaParser. parse(in); } catch(ParseException x) { // handle parse exceptions here. }
There are many Java classes that have the parse() method. Usually the parse() method receives some string as input, "extracts" the necessary information from it and converts it into an object of the calling class. For example, it received a string and returned the date that was "hiding" in this string.
JavaCC. JavaCC is the most widely used parser generator for Java. The grammar that comes with it contains all the actions and the custom code needed to build parsers in Java. Compared to ANTLR, the grammar file is not that clean and includes a significant part of Java source code.
In Java, Strings are parsed into Decimal, Octal, Binary, Hexadecimal, etc. It is done if your application is taking input from the user in the form of string but somewhere in your application you want to use that input in the form of an integer or of double type. It is not same as type casting.
Antlr has a grammar file for Java. See this.
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