3 ms·
A) Python is not context-free; this is a common misconception: http://trevorjim.com/python-is-not-context-free/ http://trevorjim.com/python-is-not-context-free/
by chimeracoder 13y ago
A) Python is not context-free; this is a common misconception: http://trevorjim.com/python-is-not-context-free/ http://trevorjim.com/python-is-not-context-free/
B) No one feature of a grammar provides context-freedom. It's not meaningful to compare Go's grammar to another grammar; it's meaningful to compare Go's grammar as-is to the way it would be if it allowed both forms.
In this case, I believe the same-line braces rule is not strictly necessary by itself (they could have gotten a context-free grammar other ways), but this was (part of) one approach.
- lucian1900 13y agoIt is context free. Just because two tokens (INDENT, OUTDENT) are different to lex doesn't mean the grammar isn't context free. The only reason same-line braces are required is semicolon insertion. There are other ways to express the grammar of a language without semicolons and remain context-free.
- chimeracoder 13y ago> There are other ways to express the grammar of a language without semicolons and remain context-free. Yes, you could use s-expressions, for example. I never said that this was the only way; I just explained part of the reason for the choice.
- lucian1900 13y agoNo, there are other ways to express the exact same syntax with a context-free grammar, for example making newlines a significant token.
- swift 13y agoIMO that article doesn't really say anything interesting. There is no clear dividing line between the lexer, the parser, and semantic analysis. You have a great deal of freedom to move work between these phases, and what the grammar you're using at the parser level looks like can change drastically as a result. If Python's lexer is not regular, it doesn't change the fact that the parser uses a context-free grammar over the resulting lexemes. Whether that's the right approach is an engineering tradeoff and nothing more.