C詞法分析中的“貪心法”

       還記得上大學那會兒,不管是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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章