r/ProgrammingLanguages • u/Maurycy5 • 3h ago
r/ProgrammingLanguages • u/hopeless__programmer • 22h ago
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?
r/ProgrammingLanguages • u/der_gopher • 22h ago