Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing / reading C-Header files using Java

I have a C-Header file defining a couple of stucts, containing multiple char arrays.

I'd like to parse these files using Java. Is there a library for reading C-Header files either into a structure or is there a stream parser that understands C-Header files?

Just for more background (I'm just looking for a C-Header parser, not a solution for this particular problem): I have a text file containing data and a C-Header file explaining the structure. Both are a bit dynamic, so I don't want to generate Java class files.

example:

#define TYPE1
typedef struct type1
{
char name1[10];
char name2[5];
}
#endif

Type2, Type3 etc are similar.

Data structure:

type1ffffffffffaaaaa
like image 406
PhilW Avatar asked Apr 24 '12 14:04

PhilW


2 Answers

You can use an existing C parser for Java. It does a lot more than parsing header files, of course, but that shouldn't hurt you.

We use the parser from the Eclipse CDT project. This is an Eclipse plugin, but we sucessfully use it outside of Eclipse, we just have to bundle 3 JAR files of Eclipse with the parser JAR.

To use the CDT parser, start with an implementation of org.eclipse.cdt.core.model.ILanguage, for example org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage. You can call getTranslationUnit on it, passing the code and some helper stuff. A code file is represented by a org.eclipse.cdt.core.parser.FileContent instance (at least in CDT7, this seems to change a lot). The easiest way to create such an object is FileContent.createForExternalFileLocation(filename) or FileContent.create(filename, content). This way you don't need to care about the Eclipse IFile stuff, which seems to work only within projects and workspaces.

The IASTTranslationUnit you get back represents the whole AST of the file. All the nodes therein are instances of IASTSomething types, for example IASTDeclaration etc. You can implement your own subclass of org.eclipse.cdt.core.dom.ast.ASTVisitor to iterate through the AST using the visitor pattern. If you need further help, just ask.

The JAR files we use are org.eclipse.cdt.core.jar, org.eclipse.core.resources.jar, org.eclipse.equinox.common.jar, and org.eclipse.osgi.jar.

Edit: I had found a paper which contains source code snippets for this: "Using the Eclipse C/C++ Development Tooling as a Robust, Fully Functional, Actively Maintained, Open Source C++ Parser", but it is no longer available online (only as a shortened version).

like image 170
Philipp Wendler Avatar answered Oct 09 '22 21:10

Philipp Wendler


Example using Eclipse CDT with only 2 jars.
- https://github.com/ricardojlrufino/eclipse-cdt-standalone-astparser
In the example has a class that displays the structure of the source file as a tree and another example making interactions on the api ...

A detail is that with this api(Eclipse CDT Parser) you can do the parsing from a string in memory.

Another example of usage is:
https://github.com/ricardojlrufino/cplus-libparser
Library for metadata extraction (information about classes, methods, variables) of source code in C / C ++.

See file: https://github.com/ricardojlrufino/cplus-libparser/blob/master/src/main/java/br/com/criativasoft/cpluslibparser/SourceParser.java

like image 41
Ricardo Jl Rufino Avatar answered Oct 09 '22 20:10

Ricardo Jl Rufino