本文介绍了添加注释语法与C ++源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想创建自己的自定义注释(就像在Java风格的注解)在我的C ++源代码。由于标准的C ++语法不允许标注,我想修改/增强自己的需要编译器。

I want to create my own custom annotation (just like the Java style annotation) tags in my C++ source. Since standard C++ syntax doesn't allow annotation, I'd like to modify/enhance the compiler for my own needs.

但是否Visual Studio中暴露其编译器内部的用户修改(例如它的词法分析的输出,抽象语法树等)?如果没有,是否有任何第三方工具来解析C ++语法,让我发出自己定制的C ++源代码的基础上我自己的注释?

But does Visual Studio expose its compiler internals for users to modify (e.g. output of its lexer, abstract syntax tree, etc)? If not, are there any third party tools for parsing c++ syntax and allow me to emit my own customized c++ source base on my own annotation?

推荐答案

标准的解决方案,这样的问题,如果它面临着一种编程语言,不是C ++,是写一个自定义的preprocessor能够理解一些有问题的语言和注释的子集,重写code。与注释除去,然后将其传递到实际的语言编译器。

The standard solution to a problem like this, if it's faced in a programming language that is not C++, is to write a custom preprocessor that understands some subset of the language in question and your annotation, rewrites the code with annotations removed, which is then passed to the actual language compiler.

这是标准的方法,当谈到扩大语言:添加编译器之前的preprocessing步

This is the standard method when it comes to extending languages: Adding an preprocessing step before the compiler.

不幸的是这是几乎不可能的只是解析的功能完整的C ++;处理C ++源$ C ​​$ C几乎总是在创造一个完整的AST发电机结束,由于它的一些繁琐的分析语言功能。

Unfortunately it's next to impossible to just parse feature complete C++; processing C++ source code almost always ends up in creating a full AST generator, due to some of it's tedious to analyze language features.

看着这些声明:

template<bool b, class T> void foo(T &t){if(b) T.do_this(); else T.do_that();}
class foobar { public: virtual void do_this(); virtual void do_that(); };
class barfoo : public foobar { public: virtual void do_this(); virtual void do_that(); };

和现在这个功能。

void bar(float l){ barfoo t; foo<fsqrt(l) > 1, foobar>(t); }

这里的问题是,括号有双重含义,模板和模板参数的比较。决定每个支架所属的唯一途径需要一个完整的语法树。

The problem here is, that the brackets have double meaning, for the template and the comparision for a template parameter. The only way to decide to which each bracket belongs requires a complete syntax tree.

我只知道一个这样的工具,像你描述:Qt工具包的MOC,莫非你有什么建议:用关键词注释的C ++ code 信号的和的插槽。 Qt是开源的,因此它可能会寻找到商务部的消息来源是个好主意。

I know of only one such tool like you describe: The MOC of the Qt toolkit, does what you suggest: C++ code annotated with keywords signal and slot. Qt is open source so it may be a good idea to look into MOC's sources.

写入和读取C ++是很艰难的。

Writing and reading C++ is very hard indeed.

这篇关于添加注释语法与C ++源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:12