4 ms·
Python's grammar is so very rich. Many programs are rejected for "syntactic" reasons if they do not fit into the abstract syntax [0]; I suppose that this could
by lidHanteyk 7y ago
Python's grammar is so very rich. Many programs are rejected for "syntactic" reasons if they do not fit into the abstract syntax [0]; I suppose that this could be some sort of "AbstractSyntaxError", if Python had such a thing.
For some examples:
x == not y
Rejected by the parser itself.
not x = y
Accepted by the parser and AST builder, and only rejected by the bytecode compiler.
f(*xs,)
Rejected by the parser itself.
...
Rejected somewhere between the parser and the AST builder. Valid uses of Ellipsis are actually special-cased; it is not a standard part of the abstract syntax.
[0] https://github.com/python/cpython/blob/master/Parser/Python.asdl https://github.com/python/cpython/blob/master/Parser/Python....
- kccqzy 7y agoWhy is x == not x rejected by the parser? In many other languages this is perfectly valid and, depending on the semantics, may allow the compiler that into false. (Of course this can't generally be the case where operators are user-overloadable.)
- js2 7y agoBecause that's how the grammar is defined: https://docs.python.org/3.8/reference/grammar.html https://docs.python.org/3.8/reference/grammar.html The RHS of the == (comp_op) has to be an expr. You can force "not x" to be an expr by parenthesizing it: x == (not x)
- kccqzy 7y agoThat interesting. On the other hand, not x == x is totally valid.
- lidHanteyk 7y agoI don't know man, I didn't do it.