C词法分析中的“贪心法”

来源:互联网 发布:淘宝咋注册 编辑:程序博客网 时间:2024/05/16 06:15
       还记得上大学那会儿,不管是C语言期末考还是计算机二级,总有类似i++, --j, 甚至i+++j这样的题目,前者这种先使用后加减或者先加减后使用还相对好理解些,但后者就比较难把握了。但最近看了《C Traps and Pitfalls》一书中关于词法分析的描述,突然茅塞顿开。
       Andrew Koenig将此类问题很形象的形容成“贪心”(译者),K&R将此表述为“If the input stream has been parsed into tokens up to a given character, the next token is taken to include the longest string of characters which could possibly constitute a token.’’ 如此就很好理解如i+++j此类问题的含义,不过对于一些特殊的符号,还是需要注意编译器的版本。


作者:nicholasxia

转载请注明出处:http://blog.csdn.net/nicholasxia/article/details/7990413


原创粉丝点击