gpt4 book ai didi

c++ - 解析器如何处理预处理器和条件编译?

转载 作者:行者123 更新时间:2023-11-30 03:19:40 25 4
gpt4 key购买 nike

我正在尝试弄清楚解析器如何处理预处理器和条件编译。以 c++ 为例,预处理器指令是否包含在 c++ 语法规则中,或者它是一种单独的语言,预处理发生在解析之前。在这两种情况下,解析器如何找出所有可能分支中的错误并在预处理之前检索有关原始代码布局的信息(例如发生错误的行数)?

最佳答案

取自the C Preprocessor docs :

The C preprocessor informs the C compiler of the location in your source code where each token came from.

所以在 GCC 的情况下,解析器知道错误发生的位置,因为预处理器告诉它。我不确定这个引用是指预处理标记,还是所有 C++ 标记。

This page有更多关于魔法如何发生的细节。

The cpp_token structure contains line and col members. The lexer fills these in with the line and column of the first character of the token. Consequently, but maybe unexpectedly, a token from the replacement list of a macro expansion carries the location of the token within the #define directive, because cpplib expands a macro by returning pointers to the tokens in its replacement list.

[...] This variable therefore uniquely enumerates each line in the translation unit. With some simple infrastructure, it is straight forward to map from this to the original source file and line number pair

Here是 C++14(?) 标准草案的拷贝。预处理语法在附录 A.14 中。我不确定是否要将其称为单独的语言是否重要。根据 [lex.phases](第 2.2 节),C++ 编译器的行为就好像预处理发生在主要翻译/解析发生之前。

关于c++ - 解析器如何处理预处理器和条件编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53498504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com