PhilW
PhilW

Reputation: 751

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

Upvotes: 7

Views: 10231

Answers (4)

Matthias
Matthias

Reputation: 1354

As mentioned already, CDT is perfect for this task. But unlike described above I used it from within a plugin and was able to use IFiles. Then everything is so mouch easier. To get the "ITranslationUnit" just do:

ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create(myIFile);
IASTTranslationUnit ias = tu.getAST();

I was i.e. looking for a special #define, so I could just:

ppc = ias.getAllPreprocessorStatements();

To get all the preprocessed code statements, each statement in array-element. Perfectly easy.

Upvotes: 5

Philipp Wendler
Philipp Wendler

Reputation: 11433

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

Upvotes: 19

Ricardo Jl Rufino
Ricardo Jl Rufino

Reputation: 597

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

Upvotes: 8

Matej
Matej

Reputation: 6209

You can try to use ANTLR. There should be already some existing C grammar available for it.

Upvotes: 3

Related Questions