问题描述
如果我的程序有一个字符串s1 =like / * this,s2 =like / *这是一个注释* / this和s3 =像// 这不是注释
在s1和s3中,/ 和// *是字符串的一部分。
在s2中,它是要在输出屏幕上显示的用户的注释。
c / c ++编译器使用什么算法? (我的猜测是,编译器只是忽略了所有的文本)
If my program has a string s1= "like/*this" , s2="like /*this is a comment */this" and s3 = "like //this is not a comment"In s1 and s3, "/" and "//*" are part of the string.In s2, It is a comment for the users to be displayed on the output screen.What algorithm does the c/c++ compiler use for this? (My guess is, the compiler just ignores all text inside "")
推荐答案
字符是字符串的一部分。从C标准第6.4.9章(注释):
No, inside strings there are no comments, all the characters are part of the string. From the C standard, chapter 6.4.9 (Comments):
然后,一个类似的字符串可以被用来识别多字节字符, //
注释的规则。
Then a similar rule for the //
comments.
此外,还有一个很好的脚注说明, c $ c> / * 在注释中无法识别,注释不嵌套。
Also, there is a nice foot-note clarifying that since the /*
is not recognized inside a comment, comments do not nest.
关于编译器使用的算法... well ,当令牌化输入文件时,编译器知道它是否在字符串内部(它必须知道其自己的状态),是否容易切换到注释模式。
About the algorithm used by compilers... well, when tokenizing the input file, the compiler knows if it is inside a string or not (it must know its own state), is it is easy to switch to comment mode or not.
这篇关于编译器(C / C ++)如何识别注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!