在哪里可以找到 C++ 的标准 BNF 或 YACC 语法? 语言?
我正在尝试开发一种代码生成器来帮助对遗留的 C/C++ 混合项目进行单元测试。 我没有发现任何类型的独立工具可以从声明生成存根代码。 所以我决定建造一个,这应该不会那么难。
请任何人都可以给我指一个标准语法链接,用 yacc 语言更好地描述。
希望我没有重新发明轮子,在这种情况下请帮助我。
此致, 凯文
I'm trying to work on a kind of code generator to help unit-testing an legacy C/C++ blended project. I don't find any kind of independent tool can generate stub code from declaration. So I decide to build one, it shouldn't be that hard.
Please, anybody can point me a standard grammar link, better described by yacc language.
Hope I'm not reinventing wheel, please help me out in that case.
Best Regards,
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来自 C++ FAQ Lite:
38.11 是否有 yacc 可用的 C++ 语法?
From the C++ FAQ Lite:
38.11 Is there a yacc-able C++ grammar?
我最近找到了一些 C++ 语法文件(C++ 1998:ISO/IEC 14882:1998 和 C++ 2008:ISO/IEC SC22/WG21 N2723=08-0233)在 语法软件网站。 语法以增强型 BNF、DMS BNF、BGF、SDF 和 Rascal 表示法表示。 但遗憾的是,C++ 语法似乎没有更新(没有 C++2003 或 C++11)。
I've recently found some grammar files for C++ (C++ 1998: ISO/IEC 14882:1998 and C++ 2008: ISO/IEC SC22/WG21 N2723=08-0233) at the grammarware website. The grammars are represented in Enahnced BNF, DMS BNF, BGF, SDF and Rascal notation. It's a pity, though, that the C++ grammars don't seem to get updated (no C++2003 or C++11).
Jared 的链接是您可以获得的最接近上下文无关语法的链接。 某些事情确实需要推迟到以后,但从某些角度来看,这比 C++ 的上下文相关语法更好。
更糟糕的是,C++1x 将使语法显着复杂化。 为了达到 C++ 的完美解析,解析器需要实现足够的标准来正确执行重载解析,包括模板参数推导,这反过来又需要概念机制、lambda 以及实际上几乎所有的语言,除了两阶段名称查找和异常规范,如果我没记错的话,不需要实际实现来成功解析程序。
实际上,如果您能够解析 C++,那么您就已经成功了一半。
Jared's link is the closest thing to a context-free grammar you can get. Certain things do need to be delayed for later, but that is by some arguments better than the context-sensitive grammar of C++.
To make things worse, C++1x will complexify the grammar significantly. To get as far as a perfect parse of C++, a parser will need to implement enough of the standard to correctly do overload resolution, including template argument deduction, which in turn will require the concepts mechanism, lambdas, and in effect almost all of the language, except for two-stage name lookup and exception specifications which, if I recall correctly, do not need actual implementation to parse a program successfully.
In effect, you are halfway to a compiler if you can parse C++.
对于另一种方法,您可以考虑搭载现有的编译器。
GCC-XML 会将 C++“编译”成包含大量有用信息的 XML 文件; 这可能足以满足您的目的。
不幸的是,GCC-XML 只维护了 1/4,让它工作起来可能会……很有趣。 如果你走这条路,祝你好运。
For another approach, you could consider piggy-backing on an existing compiler.
GCC-XML will "compile" C++ into XML files with a lot of useful information; it may be enough for your purposes.
Unfortunately, GCC-XML is only 1/4-maintained, and getting it to work can be...interesting. Good luck, if you go this route.
我最近发现了这个。 我还没有尝试过,所以不确定它是否有效。 您能否提供有关您正在尝试开发的工具的更多信息? 我下载此语法是因为我正在开发一个检测工具,这样我就可以为我的单元测试添加覆盖率信息框架。
重新阅读您的评论后...
我认为这个工具完全满足您的需求。
I found this one recently. I haven't tried it out, so am not sure if it works. Could you give more info on the tool you're trying to develop? I downloaded this grammar because I'm working on an instrumentation tool so I can add coverage info for my unit test framework.
After re-reading your comment...
I think this tool exactly fit your needs.
我们的 DMS 软件重组工具包可以通过强大的、
全功能的 C++ 解析器。 看
http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html
这会构建 AST 和符号表,并且可以推断任何表达式的类型。
DMS 使人们能够进行任意分析和转换
关于 C++ 代码。
一种“简单”的转换是检测代码以收集测试覆盖率
数据; 我们将其作为 COTS 工具提供。 请参阅本文以了解 DMS 的工作原理:
http://www.semanticdesigns.com/Company/Publications/TestCoverage.pdf
2013 年 9 月编辑(这个答案有点陈旧):DMS 的 C++
解析器/名称解析/控制流分析处理完整的 C++11,
在 ISO、GNU 和 Microsoft 变体中。 它还会解析(并保留)
包含大多数预处理器条件的源代码。 它有明确的语法
与 GCC 或 Clang 不同,驱动解析过程。
Our DMS Software Reengineering Toolkit can be obtained with a robust,
full featured C++ parser. See
http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html
This builds ASTs and symbol tables, and can infer the type of any expression.
DMS enables one to carry out arbitrary analyses and transformations
on the C++ code.
One "simple" transformation is instrumenting the code to collect test coverage
data; we offer this as a COTS tool. See this paper to understand how DMS does it:
http://www.semanticdesigns.com/Company/Publications/TestCoverage.pdf
EDIT September 2013 (This answer was getting a bit stale): DMS's C++
parser/name resolution/control flow analysis handles full C++11,
in the ISO-, GNU- and Microsoft variants. It will also parse (and retain)
source code containing most preprocessor conditionals. It has an explicit grammar
driving the parsing process, unlike GCC or Clang.