문제

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