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

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

问题描述

我想用ANTLR得到一个C ++ AST,如果可能的话,从我的C#代码库

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

现在,基本的工作流程似乎很清楚对我说:生成的.cs词法分析器和使用ANTLRWorks,将它们添加解析器和C#项目的ANTLR引用,给它一个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.

不过,我'M已经在第二步骤中失败。我下载了C ++语法从 http://www.antlr.org/grammar/list (我试过C ++语法由奥勒梅林特和C ++语法和代码示踪剂ANTLR 3.2的拉敏Zaghi),并通过设置生成C#的词法和语法分析器语言= CSharp3;在语法的选项。但是,我不能让编译包含解析器和词法文件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.

一个问题是,我不知道这是否是语法的问题我使用或可用的版本......有这么多不同版本的C#运行时的,而且试图尝试各种组合,似乎是一个相当无望的任务。在C#目标的ANTLR的,

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#词法分析器需要更改为隐藏,这就是它) ,但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.

有没有人管理解析C ++与ANTLR-生成的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 ++。作为一个方面说明,如果你能找到一个解析C ++在Java中,你可以使用 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天全站免登陆