Why isn't this a syntax error in python?

醉酒当歌 提交于 2019-11-28 09:34:54

Whitespace between tokens

Except at the beginning of a logical line or in string literals, the whitespace characters space, tab and formfeed can be used interchangeably to separate tokens. Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token (e.g., ab is one token, but a b is two tokens).

So in this case, 1if is not a valid token, so the whitespace is optional. The 1 is interpreted as an integer literal of which the if is not a part. So if is interpreted separately and recognized as a keyword.

In xif however, an identifier is recognized, so Python is not able to see that you wanted to do x if there.

The Python lexer generates two tokens for the input 1if: the integer 1 and the keyword if, since no token that begins with a digit can contain the string if. xif, on the other hand, is recognized as a valid identifier; there is no reason to believe that it is an identifier followed by a keyword, and so is passed to the parser as a single token.

With my limited knowledge of lexical processing and tokenizing I'd say what you're seeing is that any piece that can be lexical parsed as "different" (i.e. numbers/dictionaries, etc...) from the if are being done so. Most languages ignore spaces and I imagine that Python does the same (excluding, of course indentation levels). Once tokens are generated the grammar itself doesn't care, it most likely looks for an [EXPRESSION] [IF] [EXPRESSION] [ELSE] [EXPRESSION] grouping, which, again with your examples, would work fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!