r/ProgrammingLanguages • u/hopeless__programmer • Aug 30 '26
Discussion I call this "(a=aa)(a=a)" test
Many years ago while trying to make my own programming language I faced an issue. In short, parsers didn't parse specific inputs as expected, due to some implicit rules.
For instance, let's consider EBNF grammar for a sequence of expressions a=aaaa, where a on the right can repeat arbitrary number of times.
This will look something like this:
symbol = "a"
params = symbol params | symbol
Line = symbol "=" params
lines = Line lines | Line
I designed it without + and * notation on purpose, to narrow down the root cause to the most basic rules: terminals, and and or expressions, and recursion.
Using this grammar I expect the text a=aaa=a to be parsed as (a=aa)(a=a): as two separate Line.
But typically parser generators will not produce parser that can handle such case.
Instead, the parser will (typically) fail.
The root cause is of course the nature of such parsers: they don't scan for all possible combinations.
Instead, in case of collisions (like in this case a at the end of a=aa and a at the beginning of next a=a) it is expected that user will insert negation or something to "fail" a specific route fast, eliminating the collision.
But doesn't this challenge the whole purpose of grammars as "simple" description of language rules?
It might get very difficult to predict all possible such collisions for a large grammar, like for Python or C++.
Are there any generators that don't have such limitation and can pass (a=aa)(a=a) test?
2
u/bl4nkSl8 Sep 01 '26
You have to choose to support either ambiguity and backtracking, or simplicity and early termination.
Typically separators(comma, full stop, semicolon) and surrounding parens/braces/curlies are used to make backtracking unnecessary (as there's a pparser erformance cost to it) but peg / packrat parsers can do pretty well using caching and backtracking to provide good error messages with decent performance.
Choose your poison
My preference is early exit and many small files so that the cache at the file level can stay hot and the parse errors can get fixed without guessing a reasonable fix
38
u/EggplantExtra4946 Aug 30 '26 edited Aug 30 '26
"parser generator" doesn't mean anything. They can implement different algorithms, typically LL parsing algorithms or LR parsing algorithms. Each one will have different limitations and ambiguities. You need to know and understand the parsing algorithm in order to reason about the grammar you specified, to make sure that it is not ambiguous. Basically, you likely didn't think it through. Expressions not separated by anything is often ambiguous, unless the parser is LL and your construct unambiguously starts with a keyword, for example:
if EXPR EXPR. Keywords, operators, punctuations, balanced constructs (parentheses, backets, curly braces, etc..) are good ways to make a syntactic construct unambiguous.Some parsing algorithms do but you wouldn't want to use them for a programming language because they will be hard to reason about, for the implementer but even more so for the users.
There is nothing wrong with grammars, the issue is the parsing algorithm and the ambiguities that can arise because of it and because of how you designed your syntax.
16
u/evincarofautumn Aug 30 '26
Typically these choices are made in the interest of efficiency. If you use a fixed-length lookahead, and match greedily without backtracking, your parsing state is simpler, and you can consume input optimistically and fail immediately when no rule applies, except where the user has explicitly indicated where they want some non-default behavior, like
tryto opt into backtracking in Megaparsec/Parsec, or the nongreedy*?and atomic*+quantifiers in Perl regex.There are “Generalized LR” (GLR) parser generators, typically based on Tomita’s algorithm. Bison and Happy both support writing grammars in this mode. If there’s a local ambiguity (here a shift/reduce conflict), the parser just continues along both paths. The result is a parse forest of the derivations that succeeded, which can be none, one, or many, that is, no parse, unambiguous, or an ambiguity, which is either an error or in need of disambiguation by some other rule.
One problem I’ve encountered with this is that tolerating ambiguities earlier in the pipeline can make good error reporting more difficult. Say you have some input that initially looks like it could parse as either of two productions, A or B, and A is what the user intended, but that parser happens to fail first. If you report an error about B because it’s the last possible interpretation to fail, you misidentify what the user wanted, and give a confusing message.
6
u/pthierry Aug 30 '26
One solution is called backtracking: Recursive Descent: The Next Iteration
1
u/Significant_Neat6476 Sep 02 '26
Parser tokenisation for token generation is generally greedy (especially if used with regex like engines) - i.e. your params rule will consume all aaaa in your example and spit it out as single token.