c - 预处理器指令的语言与 C 的语法弱相关是什么意思?

标签 c c-preprocessor

Wikipedia article on the C Preprocessor说:

The language of preprocessor directives is only weakly related to the grammar of C, and so is sometimes used to process other kinds of text files.

预处理器语言与 C 语法有何不同?有什么好处? C 预处理器是否已用于其他语言/目的?

它是否可以用来区分内联函数和宏,因为内联函数具有普通 C 函数的语法,而宏使用的语法略有不同?

最佳答案

维基百科文章并不是 C 编程语言的真正权威来源。 C preprocessor grammar C 语法的一部分。然而,它与 phrase structure grammar 完全不同。即这两个根本不相关,除了它们都理解输入由 C 语言标记组成,(尽管 C 预处理器具有 preprocessing numbers 的概念,这意味着像 123_abc 这样的东西是合法的预处理 token ,但它不是有效的标识符)。

预处理完成后,使用短语结构语法进行翻译之前(预处理程序指令现已删除,宏扩展等),

Each preprocessing token is converted into a token. (C11 5.1.1.2p1 item 7)


对任何其他语言使用 C 预处理器确实是滥用。原因是预处理器要求文件包含正确的 C 预处理标记。它不适用于任何其他语言。甚至 C++,及其最近的扩展,例如 raw string literals , 不能被 C 预处理器预处理!

以下是 cpp (GNU C preprocessor) manuals 的摘录:

The C preprocessor is intended to be used only with C, C++, and Objective-C source code. In the past, it has been abused as a general text processor. It will choke on input which does not obey C's lexical rules. For example, apostrophes will be interpreted as the beginning of character constants, and cause errors. Also, you cannot rely on it preserving characteristics of the input which are not significant to C-family languages. If a Makefile is preprocessed, all the hard tabs will be removed, and the Makefile will not work.

关于c - 预处理器指令的语言与 C 的语法弱相关是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45344492/

相关文章:

c - 将 .o 文件移动到不同目录的 Makefile

c++ - "Triangle"与 VS2012?

使用预处理器比较定义的原始数据

c - 在下面的代码片段中定义宏有什么好处?

c++ - 具有某种条件的宏

c - GNU cpp 是否奇怪地为零参数的宏解释 C99 标准?

在调用函数 "bad"后,CUDA 无法再将数据从设备复制到主机

C 中使用指针的转换

c - 找不到 pidgin 的源码包 (Linux Mint)

c - 在 C 预处理器中为其自身定义一些内容