使用ANTLR用C#解析C++ [英] Use ANTLR to parse C++ with C#

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

问题描述

如果可能,我正在尝试使用 ANTLR 从我的 C# 代码库中获取 C++ AST.

I'm trying to use ANTLR to get a C++ AST, if possible from my C# code base.

现在,基本工作流程对我来说似乎很清楚:使用 ANTLRWorks 生成 .cs 词法分析器和解析器,将它们和 ANTLR 引用添加到 C# 项目,给它一个 C++ 源代码,使用结果数据结构.

Now, the basic workflow seems clear to me: Generate .cs lexer and parser using ANTLRWorks, add them and the ANTLR-references to a C# project, give it a C++ source, work with resulting data structures.

但是,我已经在第二步失败了.我从 http://www.antlr.org/grammar/list 下载了 C++ 语法(我试过了Aurelian Melinte 的C++ 语法"和 Ramin Zaghi 的ANTLR 3.2 的 C++ 语法和代码跟踪器")并通过设置language = CSharp3;"为 C# 生成词法分析器和解析器.在语法的选项中.但是,我无法编译包含解析器和词法分析器文件的 C# 项目.

However, I'm already failing at the second step. I downloaded C++ grammars from http://www.antlr.org/grammar/list (I tried "C++ grammar " by Aurelian Melinte and "C++ grammar and code tracer for ANTLR 3.2" by Ramin Zaghi) and generated the lexer and parser for C# by setting "language = CSharp3;" in the grammar's options. However, I can't get to compile the C# project containing the parser and lexer files.

一个问题是,我不知道这是我使用的语法问题还是可用版本的问题……ANTLR、C# 运行时和 C# 目标有很多不同的版本试图尝试每一种组合似乎是一项相当无望的任务.

A problem is that I have no idea whether this is a problem of the grammar that I use or of the versions that are available... There are so many different versions of ANTLR, of the C# runtimes and of the C# Targets that attempting to try every combination seems to be a rather hopeless task.

然而,当前的组合似乎工作正常,一个小的示例语法出来了,只有一个错误(c#词法分析器中的HIDDEN"需要更改为隐藏",仅此而已),但是C++解析器/词法分析器仍然给我很多编译器错误,主要是处理预处理器指令和数组声明.

However, the current combination seems to work fine, a small example grammar comes out with just one error ("HIDDEN" in the c# lexer needs to be changed to "Hidden" and that's it), but the C++ parser/lexer still gives me lots of compiler errors, mostly dealing with preprocessor directives and array declarations.

有没有人设法用 ANTLR 生成的 C# 文件解析 C++?有没有人知道这应该如何工作?

Did anyone ever manage to parse C++ with the ANTLR-generated C# files? Does anyone have any idea how this is supposed to work?

推荐答案

问题在于两种语法中都有嵌入代码,而且代码是用 C++ 编写的.嵌入代码在复杂的语法中很常见,所以你需要在 C# 中找到解析 C++ 的语法,而不是仅仅解析 C++.附带说明一下,如果你能找到一个在 Java 中解析 C++ 的,你可以使用 IKVM 来使用它来自 C#.

The problem is that there is embedded code in both grammars, and that code is written in C++. Embedded code is very common in complex grammars, so you need to find a grammar for parsing C++ in C#, as opposed to just parsing C++. As a side note, if you are able to find one that parses C++ in Java, you can use IKVM to use it from C#.

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

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