On Wed, Aug 22, 2018 at 8:45 AM, Abe Dillon <abedil...@gmail.com> wrote: > [Chris Angelico] >> >> Python's parser is *deliberately* incapable of backtracking this far >> in its definition of syntax. > > > Can you explain how far the parser is capable of backtracking? > It seems like <expression> with <signature> would require a similar amount > of backtracking to ternary expressions: > > <expression> if <condition> else <expression> > > If the preceding expression is the same in both cases, then the parser needs > to backtrack just as far when it finds an "if" as when it finds a "with".
I have to defer to someone who knows in more detail here, but the parser will look ahead one "token", whatever a token is. (Technically it doesn't backtrack, it looks ahead - same difference.) In order for the ternary-if to work, the expression before the if has to be of a more restricted type than just "any expression" (for instance, you can't put a lambda function there, unless you parenthesize it). But I'm not fluent in all the details, and I definitely am not comfortable citing exactly what the limits are. The last time I tried editing the grammar, weird things happened. ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/