C编译器如何解析以下C语句? [英] How does the C compiler parse the following C statement?
问题描述
请考虑以下行:
int i;
printf("%d",i);
词法分析器是否会进入字符串来解析%
和 d
作为单独的令牌,还是将%d解析为一个令牌?
Will the lexical analyzer go into the string to parse %
and d
as separate tokens, or will it parse "%d" as one token?
推荐答案
有两个解析器在这里工作:第一,C编译器,将解析C文件,基本上忽略字符串的内容(虽然现代C编译器将解析字符串,以帮助捕获错误的格式字符串 - %
转换说明符与传递给要转换的 printf()
。
There are two parsers at work here: first, the C compiler, that will parse the C file and basically ignore the content of the string (though modern C compilers will parse the string as well to help catch bad format strings — mismatches between the %
conversion specifier and the corresponding argument passed to printf()
to be converted).
下一个解析器是内置到C运行时库中的字符串格式解析器。当调用 printf
时,将在运行时调用此函数来解析格式字符串。这个解析器当然比较简单。
The next parser is the string format parser built into the C runtime library. This will be called at runtime to parse the format string when you call printf
. This parser is of course very simple in comparison.
我没有检查,但我猜想C编译器,帮助检查错误的格式字符串将实现一个 printf
类解析器作为后处理步骤(即使用自己的词法分析器)。
I have not checked, but I would guess that the C compilers that help checking for bad format strings will implement a printf
-like parser as a post-processing step (i.e. using its own lexer).
这篇关于C编译器如何解析以下C语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!