5 ms·
Don't design them. Contextual grammars are a pain to deal with, both for the implementer and the user. Alternatively, you can split the parsing into stage. Tak
by loup-vaillant 6y ago
Don't design them. Contextual grammars are a pain to deal with, both for the implementer and the user.
Alternatively, you can split the parsing into stage. Take XML for instance. The fact that it's opening and closing tags must be marked by the same identifier makes it context sensitive. You can however get around that by simply registering opening and closing tags in the parsing stage, and verify they match only later.
Really, though: if you have the liberty to design the syntax yourself, context sensitivity is generally best avoided. Heck, you probably want to limit yourself to a subset of context free grammars, like LR (which is guaranteed non-ambiguous). You can restrict even further, and stick to LL(1) —which goes a long way if you parse binary operators in a later stage, with shunting yard or precedence climbing.
Easier parsing means easier tooling, more predictable syntax, easier error handling… Sure, if you don't have a choice (compatibility or business imperative), you can introduce some context sensitivity. Ideally though, you want to use the least powerful grammar you can get away with.
- gumby 6y agoGreat comment