有哪些好的工具可以快速开始解析和分析 C/C++ 代码?
What are some good tools for getting a quick start for parsing and analyzing C/C++ code?
特别是,我正在寻找处理 C/C++ 预处理器和语言的开源工具.这些工具最好使用 lex/yacc(或 flex/bison)作为语法,不要太复杂.他们应该处理最新的 ANSI C/C++ 定义.
In particular, I'm looking for open source tools that handle the C/C++ preprocessor and language. Preferably, these tools would use lex/yacc (or flex/bison) for the grammar, and not be too complicated. They should handle the latest ANSI C/C++ definitions.
这是我目前发现的,但还没有详细研究它们(想法?):
Here's what I've found so far, but haven't looked at them in detail (thoughts?):
我希望以此为起点,将 C/C++ 源代码翻译成一种新的玩具语言.
I'm hoping to use this as a starting point for translating C/C++ source into a new toy language.
谢谢!-马特
(2/9 添加):澄清一下:除了 C/C++ 代码本身之外,我还想从预处理器中提取语义信息.我不希望#define foo 42"消失在整数42"中,但仍与名称foo"相连.不幸的是,这排除了一些首先运行预处理器并且只提供 C/C++ 解析树的解决方案)
(Added 2/9): Just a clarification: I want to extract semantic information from the preprocessor in addition to the C/C++ code itself. I don't want "#define foo 42" to disappear into the integer "42", but remain attached to the name "foo". This, unfortunately, excludes several solutions that run the preprocessor first and only deliver the C/C++ parse tree)
解析 C++ 非常困难,因为语法是不可判定的.引用 Yossi Kreinin:
Parsing C++ is extremely hard because the grammar is undecidable. To quote Yossi Kreinin:
异常复杂的语法
杰出"应按字面解释,因为所有流行语言都有上下文无关(或几乎"上下文无关)语法,而 C++ 具有不可判定 语法.如果您喜欢编译器和解析器,您可能知道这意味着什么.如果您不喜欢这种事情,这里有一个简单示例 显示解析 C++ 的问题:AA BB(CC);
是对象定义还是函数声明?事实证明,答案在很大程度上取决于语句之前的代码——上下文".这表明(在直观层面上)C++ 语法对上下文非常敏感.
"Outstandingly" should be interpreted literally, because all popular languages have context-free (or "nearly" context-free) grammars, while C++ has undecidable grammar. If you like compilers and parsers, you probably know what this means. If you're not into this kind of thing, there's a simple example showing the problem with parsing C++: is AA BB(CC);
an object definition or a function declaration? It turns out that the answer depends heavily on the code before the statement - the "context". This shows (on an intuitive level) that the C++ grammar is quite context-sensitive.
这篇关于创建 C/C++ 解析器/分析器的好工具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!