Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to parse Java in Java

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.

like image 564
Jeeyoung Kim Avatar asked May 04 '10 05:05

Jeeyoung Kim


People also ask

How do I parse a file in Java?

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. }

How is parsing done in Java?

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.

What kind of parser does Java use?

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.

Why do we parse in Java?

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.


1 Answers

Antlr has a grammar file for Java. See this.

like image 177
Chuk Lee Avatar answered Sep 25 '22 21:09

Chuk Lee