如何编写给定自定义 IDL 的代码生成器?
如果您有一个足够简单的架构,其中包含基本的布尔字符串字段,那么如何使用 C++ 编写代码生成器。 如果你曾经写过,你是怎么开始的? 欢迎文章/推荐。
编辑:请注意,这不是标准的 CORBA idl。
If you have a simple enough schema with basic boolean string fields, how to go about writing a code generator in C++.
If you ever wrote , how did you start. Articles/recommendation welcome.
EDIT: Note that this is not the standard CORBA idl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
总之:
这是一个用 TDD 解决的特别好的问题 - 首先建立一个框架,例如 CPPUnit(或其中之一)许多替代方案)以及愚蠢的简单测试,这些测试只是确保您可以加载文件并通过写出文件来迭代其内容。
以渐进的方式接近解决方案 - 获得一些简单且通用的输出,通过测试确认其有效,然后添加更复杂的变体。 例如:首先处理单参数函数。
对于中等复杂度的代码,只需使用标准 C++ ostream 类有一长串输出语句,这将允许您编写大量混合文字与任何变量的代码。
In summary:
This is a particularly good problem to tackle with TDD - start by setting up a framework such as CPPUnit (or one of many alternatives) with stupidly simple tests that just ensure you can load a file and iterate its contents by writing them out.
Approach the solution very incrementally - get something simple and general output, with a test to confirm it works, then add a more sophisticated variation. eg: handle single parameter functions first.
For code up to a moderate range of complexity, just have a long set of output statements using the standard C++ ostream classes which will allow you to write lots of code mixing literals with any variables.
Comet 是实现 COM 的非常好的库。
Comet is pretty good library to implement COM.
不幸的是,您的问题缺乏有关您到底想要生成什么的详细信息。 不过,我会尝试并建议您查看 C++ boost 库spirit (http://spirit.sourceforge.net< /a>)。 使用 Spirit 您可以创建解析器。 因此,假设您已经为 DSL(领域特定语言 - 您的 IDL)编写了一个精神解析器,在解析 IDL 文件后您将获得一个 AST(抽象语法树),您可以使用访问者模式并创建适当的访问者您可以生成所需的任何输出,例如生成代码。
Unfortunately your question lacks details about what exactly you are trying to generate. However I will take a stab and recommend that you look at the C++ boost library spirit (http://spirit.sourceforge.net). With Spirit you can create parsers. So assuming you have written a spirit parser for your DSL (domain specific language - your IDL) you will get an AST (abstract syntax tree) after parsing your IDL file(s) that you can walk with the visitor pattern and by creating appropriate visitors you can generate whatever output you need, e.g. generating code.