How to analyze Java source files with Clojure - clojure

I'm trying to analyze Java source files with Clojure but I couldn't find a way to do that.
First, I thought using Eclipse AST plugin(by copying necessary JAR's to my Clojure project) but I gave up after seeing Eclipse AST's API(visitor based walker).
Then I've tried creating a Java parser with ANTLR. I can only find one Java 1.6 grammar for ANTLR( http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g ) and it doesn't compile with latest ANTLR(here's the errors I'm getting ).
Now I have no idea how can I do that. At worst I'll try to go with Eclipse AST.
Does anyone know a better way to parse Java files with Clojure?
Thanks.
Edit: To clarify my point:
I need to find some specific method calls in Java projects and inspect it's parameters(we have multiple definitions of the method, with different type of parameters). Right now I have a simple solution written in Java(Eclipse AST) but I want to use Clojure in this project as much as possible.

... and it doesn't compile with latest ANTLR ...
I could not reproduce that.
Using ANTLR v3.2, I got some warnings, but no errors. Using both ANTLR v3.3 and v3.4 (latest version), I have no problems generating a parser.
You didn't mention how you're (trying) to generate a lexer/parser, but here's how it works for me:
java -cp antlr-3.4.jar org.antlr.Tool Java.g
EDIT 1
Here's my output when running the commands:
ls
wget http://www.antlr.org/download/antlr-3.4-complete.jar
wget http://openjdk.java.net/projects/compiler-grammar/antlrworks/Java.g
java -cp antlr-3.4-complete.jar org.antlr.Tool Java.g
ls
As you can see, the .java files of the lexer and parser are properly created.
EDIT 2
Instead of generating a parser yourself (from a grammar), you could use an existing parser like this one (Java 1.5 only AFAIK) and call it from your Clojure code.

It depends a bit on what you want to do - what are you hoping to get from the analysis?
If you want to actually compile Java or at least build an AST, then you probably need to go the ANTLR or Eclipse AST route. Java isn't that bad of a language to parse, but you still probably don't want to be reinventing too many wheels..... so you might as well build on the Eclipse and OpenJDK work.
If however you are just interesting in parsing the basic syntax and analysing certain features, it might be easier to use a simpler general purpose parser combinator library. Options to explore:
fnparse (Clojure, not sure how well maintained)
jparsec (Java, but can probably be used quite easily from Clojure)

Related

How can I parse C++ code in a prebuild event?

I have a prebuild-event tool (written in Ruby) in my C++ toolchain, that generates additional C++ code from existing C++ source code. I would like to replace this tool with a faster generator and using clang would be the best option.
Is there a way to write a C++ application that parses C++ source code of a file, so I can implement this prebuild tool in Clang? I am looking for a keyword or page with how to start. Any help is highly appreciated!
Parsing C++ is not a simple thing. Compile-time trickery and implicit semantics make it incredibly hard. Because of this I would suggest going with Clang. Its developers made it possible to use Clang as a library. Check out this guide to see different interfaces Clang has. If you want real C++ experience you might want to choose LibTooling.
I want to warn you though that in order for any C/C++ parser to work as expected they absolutely need compilation options used by the real compiler. Without include directories or macro definitions the code can make little to no sense. Basically your build system should tell your custom tool how to compile each file. The simplest approach would be using compilation database. It is a go-to solution for many Clang-based tools. However, it looks like you're making it a part of your build system, so maybe incorporating your tool and using options directly from the build system can be not such of a burden for you.
I hope this information is helpful!

can I use cmake/make to compile all and only the cpp/hpp files I used?

One of many nice features of java is that if I type javac x.java, it will compile the classes in x.java and any other classes mentioned in x, and recursively look for other required classes. I can then find the .class files, put them in a jar and I have a minimal executable for x. How would I do the same for c++? I expect I need to do it with cmake, but "minimal" does not seem to be in the modern vocabulary.
I am trying to get opencv4 running on a raspberry pi - lots of guides on the web; primarily targeting python and the rest don't work in my experience. OpenCV is classic bloatware and the solution is to automate the build process rather than simplify it.
I feel I ought to be able to start with a relevant example application and run, for example:
g++ facedetect.cpp
then (manually) compile the missing bits.
There are however missing .hpp files that are constructed by the cmake/make process and the only option seems to be to build the entire edifice first.
OpenCV4 is a CMake based project. No need to combine the classes and source files etc, that is what CMake is doing for you! You can just use this guide which has every step written out for you.

Antlr c++ target sample

I am new to antlr.
Can somebody provide a working example for any simple grammar in antlr for C++ target. I tried antlrworks and created the lexer and parser. But am not able to proceed while getting it compiled. Searched in codeproject also for a working sample, but dint find any.
I am planning to use Visual Studio 2013. Thanks in advance...
I've used ANTLR3 in a C++ application for years already, but actually used the C target, not C++. The latter proved to be a problem both in terms of compilation speed (for a highly complex parser) and complexity (all based on templates). So I settled on the C target, which is easy to integrate into C++ application. You only need a small C++ wrapper for your app and you will get a really fast parser.
In case of C++ target try this branch: https://github.com/ibre5041/antlr3/tree/master/runtime/Cpp/tests. This is more up to date version, including various performance bug fixes, memory leak fixes and also AST generation.
In the tests directory you find some examples.
Now I'm not sure whether it will work with MSVC 2013, I recall I used some C++11.
It would be best if you compiled the whole tool from these sources, not only the runtime is different, but also generated source codes slightly differ.
No not use Java 8 to compile and run antlr tool. For some mysterious reason JRE8 generates different sources then JRE7, when using the same tool .jar.
When having your grammar compiled (sources are generated), then you have to create, a traits class to be used as "configuration" for generated sources.

Usage example to generate and use ASTs using Eclipse CDT in a stand alone application

I want to implement error checking rules for lint styled static analysis of simple codes (single function, about 30-100 lines) in c, cpp, java and python. The main requirement for solving this is being able to generate ASTs.
I observed that the Eclipse IDE does a lot of static analysis, AST generation and processing using the plugins CDT, JDT, DLTK. I found that JDT could be used in standalone applications not requiring Eclipse to generate ASTs. However I wasn't able to find a working demo for a standalone implementation using CDT.
Is it possible to use them without having Eclipse or the editor modules running? Any suggestions on their usage/implementation to generate and process ASTs?
It is possible to do single java/C file ASTs generation using jdt.core/cdt.core. If you want to get some information on semantic level, you have to do it in Eclipse plugin environment(Actually you can replace or remove some dependences with JavaPlugin or CPlugin to skip this restriction). I am not sure if you just need single file generation or a project level generation.

Is there any Maven like tool that works for many languages?

I'm trying to find a tool like Maven (standardized build & packaging, artifact repo, etc) that supports more than Java (and C/C++), specifically Python and .NET (C#) as well. Especially good dependency management is desired.
We're running a mixed shop of languages at our place, and the current homegrown python-based "über-buildsystem" should probably go away. Note that it doesn't attempt to do detailed builds, just handles dependencies between modules/projects, downloads compiled artifacts (a few hundred megs per library in the C++ case) and invokes msbuild/easy_install/etc on the existing .sln/.vcproj/etc files to get the real build done.
It works, but mostly because I built it and know what it can and cannot do, and I extend it when I need new stuff. It's not really helping the other developers getting in to it, and I feel more and more that we shouldn't focus on that. (Even though it's not that much work and there doesn't seem to be a compelling alternative.)
So:
Python's setuptools only seem to be for Python (and some c++ -> .pyd compilation).
Maven mostly does Java, and some C/C++, mostly for JNI.
For C/C++ there is... nothing? (qmake/cmake for building)
For .NET there are some stuffs, but do they work well outside Visual Studio and the MSFT toolchain?
Building another Maven (but with more flexible support) on top of Ant (using Beanshell mostly) is something I've done already. The current Python-stuff is an offshoot of that.
Update:
Ant+Ivy would require building quite a bit on top, as I indicated above, but it's probably a better foundation than what we have today, since we get more build tasks and an artifact repo out of the box. (See Maven Like dependency management for C++)
All help warmly appreciated!
Try waf.