4 ms·
Most languages are context sensitive. Most language tools are context free. How did we go so wrong?
by chrisseaton 6y ago
Most languages are context sensitive.
Most language tools are context free.
How did we go so wrong?
- MaxBarraclough 6y agoCould this be viewed as supply exceeding demand? Context-free grammars are ripe for theoretical computer science work even if they're not practically relevant. On the flipside, I suppose the constraints of context-free grammars are seen as a price not worth paying when designing a language.
- throwaway_pdp09 6y agoCan you give an example (or three) where that lets us down? That would be very helpful to me I suspect.
- chrisseaton 6y agoExamples of which bit? Languages that are context-sensitive? C, C++, Java, JavaScript. Examples of tools based on the starting expectation that languages are context-free? Yacc, Bison, Jay. Examples of the problems this causes? Well we're using the wrong tool for the job, right from the start. Instead of using an appropriate tool the first thing we do is bend our tools out of shape. We use side-effect actions to subvert the tool's model in an uncontrolled way. We don't get the full benefits of the tool's original model and can't rely on its guarantees.
- WalterBright 6y agoLanguages being context sensitive results from hacking in new language features. It's a constant struggle to keep D context free :-/
- Kranar 6y agoStrictly speaking, D is not context free nor is any statically typed programming language. That said most languages, including D, have supersets that are context free and can be used to build an AST which can then be refined further with context sensitive checks (ie. variables are used after declaration) and then even further with type checks. Many language tools don't need a full blown language parser and can get by with a context-free parser for the language's superset. Things like autocompletion, spell checking, documentation/highlighting can all be implemented using context-free parsers.
- WalterBright 6y agoI suppose we could argue about the definition of context free, but the compiler lexes it without any reference to the parser, and the parser builds the AST without any reference to symbols or semantic meaning. > spell checking ... using context-free parsers Only up to a point. D's spell checker uses the symbols in scope as its "dictionary". This means it is very good at guessing what you meant instead of what is typed, as the dictionary is very targeted. I.e. it uses the context, and the spell checker is part of the semantic pass.
- Kranar 6y agoA context free grammar has a formal and rigorous definition. One way to see that Dlang does not have a strictly context free grammar for its AST is the following snippet of code: A[B] C; That is parsed differently depending on whether B is a constant integer expression in which case it parses to the declaration of an array of type A with a size of B, or whether B is a typename in which case it parses into an hash map with keys of type B and values of type A. This disambiguation can not be specified by the context free grammar and instead must be delayed until the semantic analysis (which is usually defined by a context sensitive grammar). Pretty much all statically typed languages work this way with various kinds of ambiguities. In practice, it's not a particularly big deal. But that's kind of my point, there's no need to work exceedingly hard to force a language construct to be context free, especially if making it context free results in a lack of readability or awkward syntax. It's perfectly fine to specify a superset of the language that is context free, parse an AST for the superset of that language and then resolve AST ambiguities in later phases, such as the semantic analysis phase or the type checking phase. Almost any tool written to assist an IDE with autocompletion or other functionality will have no problem working with the language superset.
- WalterBright 6y ago> That is parsed differently It isn't parsed differently. Its semantic meaning depends on what B is, but not the parse.
- kingaillas 6y ago
- pfalcon 6y ago> Most languages are context sensitive. Most language tools are context free. How did we go so wrong? We never did. We always knew that any real-world useful grammar is multi-level and attribute (well, it's constraint-based, though depending on your definition of "attribute grammar", those are equivalent). That's why we so much like recursive-descent parsers: adding any multi-level constraints are so easy to them - you have a full Turing-complete language at your disposal, unlike simplistic 1-dimensional DSLs of most parser generators.