4 ms·
> People complain that go fmt isn’t customizable and that it puts braces where they don’t want them. I assume this is referring to putting opening braces on t
by chimeracoder 13y ago
> People complain that go fmt isn’t customizable and that it puts braces where they don’t want them.
I assume this is referring to putting opening braces on the next line.
IIRC, this has nothing to do with the `go fmt` tool - unlike in C, this is actually a syntax error in Go: http://play.golang.org/p/bn30UNkr1z http://play.golang.org/p/bn30UNkr1z
One of the reasons for this decision is because it helps enforce a truly context-free grammar for the language.
Ironically, the fact that Go has a nearly context-free grammar is what allows tools like `go fmt` and the obsolete `go fix` to make modifications to the code deterministically, with guarantees that it will not change the behavior of the code.
- lucian1900 13y agoThat is by far not the only way to do have a context-free grammar. Python for example also has a context-free grammar without semicolon insertion.
- chimeracoder 13y agoA) 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.