I have a basic question on Lexical Analysis. As lexical analyzer (which is a DFA which accepts lexemes and produces token) can produce tokens which can be Identifier([a-z]/[A-Z]/[0-9] knowing digit not the first char), Keywords (if, else,..) , Operators & Delimiters.

So if a lexeme like 'whil' comes. Then will it be a lexical error stating Its not a valid keyword or will it be accepted as Identifier(variable), as lexical analyzer do not consider whether the syntax is right or not ?

Does Lexical Analyzer use different DFA for Identifiers, Keywords, Operators ?

有帮助吗?

解决方案

Then will it be a lexical error stating Its not a valid keyword

No.

or will it be accepted as Identifier(variable)

Yes, and then there will be a syntax error from the parser.

as lexical analyzer do not consider whether the syntax is right or not ?

The lexical analyser doesn't know anything about syntax. That's the parser's job.

其他提示

To identify keywords, It uses the same regexp for identifier, and then checks the keyword table, whether it's a keyword. If yes, then keyword, else it is an identifier

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top