質問

I'm writing a small compiler for a interest and I need to know what stage in the wrong keyword is detected (a keyword that is not in the language) during lexical analysis or parsing ?

役に立ちましたか?

解決

You'll detect this during parsing. It's not that you have a "wrong keyword", it's that you have an identifier (i.e. a variable name) appearing in a place where you don't expect. So, if your source code looks like:

reeeturn 3;

From the compiler's perspective, you're just using some variable named reeeturn. That could be an error because a variable with that name isn't defined. Or, in this case, it's probably a syntax error to have a number follow an identifier.

But there's no lexical error here. It's a totally valid sequence of tokens: identifier, number, semicolon.

他のヒント

This is probably during lexical analysis. Lexical analysis is the compiler phase where the input file is cut apart into pieces and tagged with what those pieces mean, whereas parsing takes those existing pieces and uses them to assemble an AST. Without seeing the code I can't be certain about this, but based on this reasoning I'd suspect the error is in the scanner and not the parser.

Hope this helps!

It depends on the language.

The lexing phase is responsible for creating a token stream from the source file. If the "wrong keyword" is still a valid token in the language, it will be tokenized properly - for example, in C, the "wrong keyword" will be tokenized to an identifier. It is only later during parsing that the mistake will be revealed.

On the other hand, in a language in which the "wrong keyword" cannot be any other valid token (e.g. a language using sigils for variables), the lexer itself will complain.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top