解析/阅读使用Java C-头文件 [英] Parsing / reading C-Header files using Java

查看:207
本文介绍了解析/阅读使用Java C-头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C头文件定义了几个stucts的,包含多个字符数组。

我想解析使用Java这些文件。是否有阅读C-头文件或者放入结构或库是否有理解C头文件的流解析器?

只是为了更多的背景(我只是在寻找一个C-标题分析器,而不是为这个特殊问题的解决方案):
我有一个包含数据和C头文件解释结构的文本文件。两者都有点动态的,所以我不想生成Java类文件。

例如:

 的#define TYPE1
typedef结构TYPE1
{
CHAR名1 [10];
炭NAME2 [5];
}
#万一

2型,3类等是相似的。

数据结构:

  type1ffffffffffaaaaa


解决方案

您可以使用Java的一个现有的C解析器。它比解析头文件,当然多了不少,但不应该伤害你。

我们使用来自 Eclipse CDT的项目解析器。这是一个Eclipse插件,但我们成功地使用Eclipse之外,我们只需要捆绑的Eclipse 3 JAR文件的解析器JAR。

要使用CDT解析器,先从 org.eclipse.cdt.core.model.ILanguage 的实现,例如 org.eclipse .cdt.core.dom.ast.gnu.c.GCCLanguage 。您可以拨打 getTranslationUnit 就可以了,路过code和一些辅助的东西。一个code文件再由 org.eclipse.cdt.core.parser.FileContent 实例(至少在CDT7 psented $ P $,这似乎改变很多)。创建这样的对象,最简单的方法是 FileContent.createForExternalFileLocation(文件名) FileContent.create(文件名,内容) 。这样,你不需要关心Eclipse的的IFile 的东西,这似乎只有在项目和工作区的工作。

IASTTranslationUnit 等你回来再presents文件的全AST。所有节点都是其中的 IASTSomething 类型,例如 IASTDeclaration 等,您可以实现自己的<$的子类的实例C $ C> 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

修改:我发现了一个文件,其中包含源$ C ​​$ C段为这样的:
使用Eclipse C / C ++开发工具作为一个强大的,功能齐全,积极维护,开源C ++分析器,但它已不再可在网上(只缩短的版本)。

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

解决方案

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

这篇关于解析/阅读使用Java C-头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆