Parsing / reading C-Header files using Java

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

I would 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 a bigger 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 of them are a bit dynamic, so I do not want to generate Java class files.

Example:

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

Type2, Type3, etc. similar.

Data structure:

type1ffffffffffaaaaa
+5
source share
4 answers

C- Java. , , , .

Eclipse CDT. Eclipse, Eclipse, 3 JAR Eclipse JAR.

CDT, org.eclipse.cdt.core.model.ILanguage, org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage. getTranslationUnit , . org.eclipse.cdt.core.parser.FileContent ( , CDT7, , , ). - FileContent.createForExternalFileLocation(filename) FileContent.create(filename, content). Eclipse IFile, , , .

IASTTranslationUnit . IASTSomething, IASTDeclaration .. org.eclipse.cdt.core.dom.ast.ASTVisitor AST . , .

JAR : org.eclipse.cdt.core.jar, org.eclipse.core.resources.jar, org.eclipse.equinox.common.jar org.eclipse.osgi.jar.

. , : " Eclipse C/++ , , , ++ Parser", ( ).

+12

As already mentioned, CDT is ideal for this task. But, unlike the one described above, I used it from inside the plugin and was able to use IFiles. Then everything is so convenient. To get "ITranslationUnit", simply do:

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

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

ppc = ias.getAllPreprocessorStatements();

To get all the pre-processed code statements, each statement in the element array. Absolutely easy.

+3
source

You can try using ANTLR . To do this, there must already be some existing C grammar .

+2
source

All Articles