有向图和有向图不能一起使用吗? [英] Digraph and trigraph can't work together?

查看:120
本文介绍了有向图和有向图不能一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有向图和有向图,这是我无法理解的代码。 (是的,我承认这非常丑陋。)

I'm learning digraph and trigraph, and here is the code which I cannot understand. (Yes, I admit that it's extremely ugly.)

此代码可以编译:

#define _(s) s%:%:s

main(_(_))
<%
    __;
%>t

此代码也可以编译:

#define _(s) s??=??=s

main(_(_))
<%
    __;
%>

但是,以下两个代码都不能编译:

However, neither of the following two pieces of code can compile:

#define _(s) s%:??=s

main(_(_))
<%
    __;
%>

#define _(s) s??=%:s

main(_(_))
<%
    __;
%>

这确实使我感到困惑:由于前两段代码可以编译,因此我想对有向图进行扩展和trigraph都发生在宏扩展之前。那么,为什么当有向图和有向图一起使用时,为什么不能编译呢?

This does confuse me: Since the first two pieces of code can compile, I suppose the expansion of digraph and trigraph both take place before the macro expansion. So why can't it compile when digraph and trigraph are used together?

推荐答案

有向图和有向图完全不同。在翻译的第1阶段(请参见注1),即在将源代码分离为标记之前,替换了Trigraph。有向标是标记,它们是其他标记的替代拼写,因此它们只有在将源分离为标记之后才有意义。 ( digraph一词不是很准确;之所以使用它是因为它类似于 trigraph,但是有向图的集合包括%:%:,它由四个字符组成。 )

Digraphs and trigraphs are totally different. Trigraphs are replaced during phase 1 of translation, [see Note 1] which is before the source code has been separated into tokens. Digraphs are tokens which are alternate spellings for other tokens, so they are not meaningful until after the source has been separated into tokens. (The word "digraph" is not very accurate; it is used because it resembles "trigraph", but the set of digraphs includes %:%: which consists of four characters.)

因此 ?? = 替换为在完成任何令牌分析之前。但是%:只是一个令牌,与的含义相同。

So ??= is replaced with a # before any token analysis is done. But %: is just a token, with the same meaning as #.

此外,%:%:是与 ## 含义相同的令牌。但是%:#是两个令牌(%:),这是非法的,因为字符串化运算符(无论是拼写为%:还是)宏参数。 [请参见注2]如果是由三维字母替换的结果,那么这也不会变得越来越不合法。

Moreover, %:%: is a token with the same meaning as ##. But %:# is two tokens (%: and #), which is not legal since the stringify operator (whether spelled %: or #) can only be followed by a macro parameter. [See Note 2] And it does not become any less illegal if the # were the result of a trigraph substitution.

有向图和有向图之间的一个重要区别,如 chqrlie答案中的搞笑片段所示,是有向图也适用于字符串。即使键盘缺少方括号和八字体,也可以使用有向标来编写C代码,但它们无助于您打印出这些字符。

One important difference between digraphs and trigraphs, as illustrated by the hilarious snippet in chqrlie's answer, is that trigraphs also work in strings. Digraphs allow you to write C code even if your keyboard lacks brackets and octothorpi, but they don't help you print those characters out.


  1. & 5.1.1.2,翻译阶段 ,第1段:


翻译的语法规则中的优先级由以下阶段指定。

The precedence among the syntax rules of translation is specified by the following phases.


  1. 如有必要,将物理源文件多字节字符以实​​现定义的方式映射到源字符集(为行尾指示符引入换行符)。三字母组合序列由相应的单字符内部表示形式代替。



  • § 6.10.3.2,#运算符 ,第1段:


    函数替换宏的替换列表中的每个预处理令牌应为
    ,后跟一个参数作为替换列表中的下一个预处理令牌。

    Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter as the next preprocessing token in the replacement list.


  • 这篇关于有向图和有向图不能一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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