简体   繁体   English

在哪里可以找到 C++ 语言的标准 BNF 或 YACC 语法?

[英]Where can I find standard BNF or YACC grammar for C++ language?

I'm trying to work on a kind of code generator to help unit-testing an legacy C/C++ blended project.我正在尝试开发一种代码生成器来帮助对遗留的 C/C++ 混合项目进行单元测试。 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.拜托,任何人都可以给我一个标准的语法链接,用 yacc 语言更好地描述。

Hope I'm not reinventing wheel, please help me out in that case.希望我不是在重新发明轮子,在这种情况下请帮助我。

Best Regards, Kevin最好的问候,凯文

From the C++ FAQ Lite :来自C++ FAQ Lite

38.11 Is there a yacc-able C++ grammar? 38.11 有没有 yacc-able C++ 语法?

The primary yacc grammar you'll want is from Ed Willink.您需要的主要 yacc 语法来自 Ed Willink。 Ed believes his grammar is fully compliant with the ISO/ANSI C++ standard , however he doesn't warrant it: "the grammar has not," he says, "been used in anger." Ed 相信他的语法完全符合ISO/ANSI C++ 标准,但他不保证:“语法没有,”他说,“被用于愤怒。” You can get the grammar without action routines or the grammar with dummy action routines .您可以获得没有动作例程的语法或带有虚拟动作例程的语法 You can also get the corresponding lexer .您还可以获得相应的 lexer For those who are interested in how he achieves a context-free parser (by pushing all the ambiguities plus a small number of repairs to be done later after parsing is complete), you might want to read chapter 4 of his thesis .对于那些对他如何实现无上下文解析器感兴趣的人(通过推动所有歧义以及在解析完成后进行少量修复),您可能需要阅读他论文的第 4 章。

There is also a very old yacc grammar that doesn't support templates, exceptions, nor namespaces;还有一个非常古老的 yacc 语法,它不支持模板、异常和名称空间; plus it deviates from the core language in some subtle ways.加上它以一些微妙的方式偏离了核心语言。 You can get that grammar here or here .您可以在此处此处获得该语法。

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 .我最近在语法网站上找到了 C++ 的一些语法文件(C++ 1998: ISO/IEC 14882:1998和 C++ 2008: ISO/IEC SC22/WG21 N2723= 08-0233 网站)。 The grammars are represented in Enahnced BNF, DMS BNF, BGF, SDF and Rascal notation.语法以Enahnced BNF、DMS BNF、BGF、SDF 和 Rascal表示法表示。 It's a pity, though, that the C++ grammars don't seem to get updated (no C++2003 or C++11).遗憾的是,C++ 语法似乎没有得到更新(没有 C++2003 或 C++11)。

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; GCC-XML会将C++“编译”成XML文件,里面有很多有用的信息; it may be enough for your purposes.它可能足以满足您的目的。

Unfortunately, GCC-XML is only 1/4-maintained, and getting it to work can be...interesting.不幸的是,GCC-XML 只维护了 1/4,让它工作可能是……有趣的。 Good luck, if you go this route.祝你好运,如果你 go 这条路线。

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.我认为这个工具完全符合您的需求。

Jared's link is the closest thing to a context-free grammar you can get. Jared 的链接是您可以获得的最接近上下文无关语法的东西。 Certain things do need to be delayed for later, but that is by some arguments better than the context-sensitive grammar of C++.某些事情确实需要延迟到以后,但那是 arguments 比 C++ 的上下文敏感语法更好。

To make things worse, C++1x will complexify the grammar significantly.更糟糕的是,C++1x 将显着复杂化语法。 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.为了完美解析 C++,解析器需要实现足够多的标准来正确执行重载解析,包括模板参数推导,这反过来需要概念机制、lambdas,实际上几乎所有语言,除了两阶段名称查找和异常规范,如果我没记错的话,不需要实际实现来成功解析程序。

In effect, you are halfway to a compiler if you can parse C++.实际上,如果您可以解析 C++,那么您已经完成了编译器的一半。

Our DMS Software Reengineering Toolkit can be obtained with a robust, full featured C++ parser.我们的 DMS 软件再造工具包可通过功能强大的全功能 C++ 解析器获得。 See http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html This builds ASTs and symbol tables, and can infer the type of any expression.请参阅http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html这将构建 AST 和符号表,并且可以推断任何表达式的类型。 DMS enables one to carry out arbitrary analyses and transformations on the C++ code. DMS 使人们能够对 C++ 代码进行任意分析和转换。

One "simple" transformation is instrumenting the code to collect test coverage data;一种“简单”的转换是检测代码以收集测试覆盖率数据; we offer this as a COTS tool.我们将其作为 COTS 工具提供。 See this paper to understand how DMS does it: http://www.semanticdesigns.com/Company/Publications/TestCoverage.pdf请参阅本文以了解 DMS 是如何做到的: 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.编辑 2013 年 9 月(这个答案有点陈旧):DMS 的 C++ 解析器/名称解析/控制流分析在 ISO-、GNU- 和 Microsoft 变体中处理完整的 C++11。 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.与 GCC 或 Clang 不同,它具有驱动解析过程的显式语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM