为什么C / C ++预处理程序在此处添加空格? [英] Why is C/C++ preprocessor adding a space here?

查看:93
本文介绍了为什么C / C ++预处理程序在此处添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对预处理器有一个小问题,这使我感到困惑,并且在文档/预处理器/语言规范中找不到对此的任何解释。

I have a tiny problem with a preprocessor that puzzles me and I cannot find any explanation to it in the documentation/preprocessor/language spec.

#define booboo() aaa
booboo()bbb
booboo().bbb

被预处理为:

aaa bbb   <--- why is space added here
aaa.bbb

在处理了三连字符,连续的行和注释之后,预处理器对预处理器指令进行工作并划分输入进入预处理令牌和空格。 booboo的替换清单包含一个pp令牌,即标识符 aaa。 booboo()bbb分为pp令牌: booboo,(,), bbb。 booboo,(,)的序列被认为是功能宏调用,应将其扩展为 aaa,输出中的恕我直言应类似于 aaabbb。我说过,因为-对人类而言-看起来像一个令牌,而编译器将获得2个令牌 aaa和 bbb,因为没有使用允许pp令牌级联的 ##运算符。为什么/什么规则使cpp(c预处理程序)在'booboo()。bbb'导致'aaa.bbb'没有空间的情况下在'aaa'和'bbb'之间放置额外的空间?

After handling trigraphs, continued lines and comments, preprocessor works on preprocessor directives and divides input into preprocessing tokens and whitespace. booboo's replacement list comprises one pp-token which is identifier 'aaa'. booboo()bbb is divided into pp-tokens: 'booboo', '(', ')', 'bbb'. Sequence of 'booboo', '(', ')' is recognised as functional macro invocation and it should be expanded to 'aaa' and imho in output should look like 'aaabbb'. I said look like since - to human - it would look like one token whereas compiler would get 2 tokens 'aaa' and 'bbb' since no '##' operator was used that allows pp-token concatenation. Why/what rule makes cpp (c preprocessor) place additional space between 'aaa' and 'bbb' when 'booboo().bbb' results in 'aaa.bbb' without space?

这是因为cpp试图使输出(主要是对人类)是明确的吗?人类无法识别 aaabbb是由2个​​标记组成的,因为它只能看到标记的拼写。我对吗?我已经阅读了有关预处理器的C99文档和有关cpp的gcc文档。我什么都没看到。

Is this because cpp tries to make output (which is for humans mostly) unambinuous? Human is not able to tell that 'aaabbb' is composed from 2 tokens as it sees token's spelling only. Am I right? I've read C99 documentation about preprocessor and gcc's documentation for cpp. I see nothing about it.

如果我是对的,我们在这里也有类似情况:

If I am right we have similar situation here:

#define baba() +
baba()+
baba()-

结果:

+ +
+-

否则(如果输出为 ++),它将看起来像人类的 ++令牌,但会有2个令牌 +和 +。 cpp是否像‘##’运算符那样检查连接是否产生了有效的令牌,但在所示情况下想要阻止人员执行连接? '+-'不是模棱两可的,因此不会添加空间

Otherwise (if '++' is the output) it would look to a human like '++' token but there would be 2 tokens '+' and '+'. Is it like with '##' operator that cpp checks if concatenation produces valid token but in shown cases wants to prevent human that concatenation was performed? '+-' is not ambiguous hence no space added

推荐答案

预处理的结果是将源文件转换为以下内容的列表:令牌。在您的情况下,令牌化后的令牌列表如下所示:

The result of preprocessing is to transform the source file into a list of tokens. In your case the list of tokens would look like, after tokenization:

....
booboo()
bbb
....

然后在宏替换后:

....
aaa
bbb
....

然后,编译器将令牌列表转换为可执行文件。

Then the compiler translates the list of tokens into an executable.

您看到的空白只是实现细节,编译器等已选择在显示中间结果时对预处理令牌进行布局。该标准对任何中间处理文件一无所知。也不要求有一个单独的程序来进行预处理。

The whitespace you are seeing is just an implementation detail that your compiler etc. has chosen to lay out the preprocessing tokens when displaying an intermediate result to you. The standards say nothing about any intermediate processing files. It is not required that there be a separate program to do preprocessing either.

这篇关于为什么C / C ++预处理程序在此处添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆