4 ms·
Note that "context free grammars", in the technical sense, is completely the wrong thing. Eg, enforcing consistent indentation is not context free: foo()
by a1369209993 7y ago
Note that "context free grammars", in the technical sense, is completely the wrong thing. Eg, enforcing consistent indentation is not context free:
foo()
{
return;
}
↓↓↓↓↓↓↓↓↓↓↓
aaaa{
bbbbreturn;
cccc}
↓↓↓↓↓↓↓↓↓↓↓
aaaabbbbcccc
This is equivalent to aⁿbⁿcⁿ, which is pretty much the canonical example of a non-context-free language.
What you acually want is not context-free grammar, but, as TFA says:
> the code should be parseable without having to look things up in a symbol table
- qznc 7y agoThere is a trick for indentation: Use a context-sensitive lexer in front of a context-free parser. The lexer serves indent and dedent tokens to the parser. With this trick, you can parse Python with a context-free grammar.
- bmn__ 7y agoThere are plenty, the majority even, of programming languages that do not enforce consistent indentation. You make a false generalisation when you claim that CFG are completely the wrong thing.
- tom_mellior 7y ago> This is equivalent to aⁿbⁿcⁿ Is it? You only indent with spaces, so a = b = c = ' ', and the problem you claim disappears.
- gbear605 7y agoI'm not sure that's a solution. The problem is verifying that the three 'n's are equal, not that 'a', 'b', and 'c' are equal. Solving this cannot be done in a context free grammar [0]. [0]: https://cs.stackexchange.com/questions/33153/is-an-bn-cn-context-free https://cs.stackexchange.com/questions/33153/is-an-bn-cn-con...
- tom_mellior 7y agoYeah, you're right. I got confused by the original phrasing.
- gbear605 7y agoI'd suggest that generally the grammar should be context-free, but that a few features = can be non-context-free. Formatting like this should specifically be a separate check IMO.