I am reading the dragon book. Quoting the text from the book (3.1.4 Lexical errors, Pno 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.
I am bit confused after reading this. My understanding was lexical analyser starts processing the text from left to right and return tokens whenever the pattern matches. So for a language where if
is the keyword to match, how can fi
match?
Any thoughts?