我正在阅读《龙书》。引用书中的文本(3.1.4词法错误,第114页)
我读完后有些困惑。我的理解是词法分析器从左到右处理文本,并在模式匹配时返回标记。因此,对于一个需要匹配关键字It is hard for a lexical analyzer to tell, without the aid of other components, that there is a source-code error. For instance, if the string
fi
is encountered for the first time in a C program in the context:fi ( a == f(x) ) ...
a lexical analyzer cannot tell whether
fi
is a misspelling of the keywordif
or an undeclared function identifier. Sincefi
is a valid lexeme for the token id, the lexical analyzer must return the tokenid
to the parser and let some other phase of the compiler - probably the parser in this case - handle an error due to transposition of the letters.
if
的语言,如何可以匹配fi
?有什么想法吗?