Best way to parse Java in Java - 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.

Antlr has a grammar file for Java. See this.

Janino can parse, compile and execute Java code.

You may be looking for something like ANTLR: http://www.antlr.org/

Eclipse exposes the Syntax Tree of it's own Java compiler. You can simply access the elements.
See here.

JavaCC has a Java 1.5 grammar including generics.
the tokens are probably more
fine-grained then what I want
Your first requirement is to get an accurate parse, and you will realistically only get that from an existing parser. What you do with the result is up to you.

Related

Is there a compiler code for java 8 to use with antlr4?

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

Use of ANTLR to multiple Parsing

What I need : Find a way to parse a ttcn-3 source code or a ttcn-3 schema into XML or JAVA
I have looked on the internet to see if I can find an IDE or a program that helps me to parse a code in a ttcn-3 language and I have found the ANTLR website. Another tool for language recognition so I would like to know if someone has used ANTLR or knows if it can help me to parse.
Definitely yes. I know many people who have used ANTLR (including myself) and it can be used to solve your problem and parse your language if you can find a grammar for it.

Creating a XBRL document in Java?

I just started with XBRL.
What Java lib(s) do you use for creating XBRL documents?
I find it hard to find "opensource" java libs for XBRL creation/manipulation.
#edbras: Check out Arelle.org. It's not Java, but it's free and it is python, and some java, which should be close enough. There are other commercial options if you are interested.
regards,
Per Solli
I use JAXB, and seem to work fine. I create the java code with jaxb ref implementation and use it through the factories...

Intellij parsing java code

I want to use a math-expression parser of java code. In particular I would like to convert a math-expression given as String to an abstract syntax tree consisted of separate nodes.
Is there anyone to recommend me a relevant open source tool?
If no, how do you reckon the possibility to exploit Intellij source code to do this work?
Which classes are responsible for code parsing and analysis?
Are they included in idea.jar? How can I easily infiltrate their functionality (methods etc)?
I am speaking exclusively for Intellij.
Take a look at MVEL library.
If you only want the results of the math-expression you should revise the question and the answer i selected months ago:
Java 1.5: mathematical formula parser
Brieff description: use the java integration with dinamyc languajes like javascript to let them do the work for you
I would not use IntelliJ, as much as I love it.
If you need an AST, look no further than ANTLR. If you can write a grammar for your equations, ANTLR can generate a lexer/parser to create it for you.

Coding a parser for a domain specific language in Java

We want to design a simple domain specific language for writing test scripts to automatically test a XML-based interface of one of our applications. A sample test would be:
Get an input XML file from network shared folder or subversion repository
Import the XML file using the interface
Check if the import result message was successfull
Export the XML corresponding to the object that was just imported using the interface and check if it correct.
If the domain specific language can be declarative and its statements look as close as my sentences in the sample above as possible, it will be awesome because people won't necessarily have to be programmers to understand/write/maintain the tests. Something like:
newObject = GET FILE "http://svn/repos/template1.xml"
reponseMessage = IMPORT newObject
newObjectID = GET PROPERTY '/object/id/' FROM responseMessage
(..)
But then I'm not sure how to implement a simple parser for that languange in Java. Back in school, 10 years ago, I coded a language parser using Lex and Yacc for the C language. Maybe an approach would be to use some equivalent for Java?
Or, I could give up the idea of having a declarative language and choose an XML-based language instead, which would possibly be easier to create a parser for? What approach would you recommend?
You could try JavaCC or Antlr for creating a parser for your domain specific language. If the editors of that file are not programmers, I would prefer this approach over XML.
Take a look at Xtext - it will take a grammar definition and generate a parser as well as a fully-featured eclipse editor pluging with syntax highlighting and -checking.
ANTLR should suffice
ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting.
Look at Antlr library. You'll have to use EBNF grammatic to describe your language and then use Antlr to make java classes from your grammatic.
Have a look at how Cucumber defines its test cases:
(source: cukes.info)
http://cukes.info/ - can run in JRuby.
Or, I could give up the idea of having a declarative language and
choose an XML-based language instead,
which would possibly be easier to
create a parser for? What approach
would you recommend?
This could be easily done using XML to describe your test scenarios.
< GETFILE object="newObject" file="http://svn/repos/template1.xml"/ >
Since your example of syntax is quite simple, it should also be possible to simply use StringTokenizer to tokenize and parse these kind of scripts.
If you want to introduce more complex expressions or control structures you probably better choose ANTLR
I realize this thread is 3 years old but still feel prompted to offer my take on it. The questioner asked if Java could be used for a DSL to look as closely as possible like
Get an input XML file from network shared folder or subversion repository
Import the XML file using the interface
Check if the import result message was successfull
Export the XML corresponding to the object that was just imported
using the interface and check if it correct.
The answer is yes it can be done, and has been done for similar needs. Many years ago I built a Java DSL framework that - with simple customization - could allow the following syntax to be used for compilable, runnable code:
file InputFile
message Message
get InputFile from http://<....>
import Message from InputFile
if validate Message export Message
else
begin
! Signal an error
end
In the above, the keywords file, message, get, import, validate and export are all custom keywords, each one requiring two simple classes of less than a page of code to implement their compiler and runtime functions. As each piece of functionality is completed it is dropped into the framework, where it is immediately available to do its job.
Note that this is just one possible form; the exact syntax can be freely chosen by the implementor. The system is effectively a DIY high-level assembly language, using pre-written Java classes to perform all the functional blocks, both for compiling and for the runtime. The framework defines where these bits of functionality have to be placed, and provides the necessary abstract classes and interfaces to be implemented.
The system meets the primary need of clarity, where non-programmers can easily see what's happening. Changes can be made quickly and run immediately as compilation is almost instantaneous.
Complete (open) source code is available on request. There's a generic Java version and also one for Android.

Categories