(warning: draft. And compiler-heavy.)
Classic recursive-descent parsing has an obvious problem: left-recursion becomes infinite recursion in the call stack.
A := A B | C
Technically, that grammar allows for `C B` to be a valid `A`, and same for `C B B`, and any number of Bs at the end. It's functionally equivalent to
A := C B*
This sort of equivalence is not always possible. Generalized LL parsing (GLL) is, in essence, a modified recursive descent which solves this, but all of the information I can find on it is dense academic bullshit.
GLL does two things: 1) It yields every possible successful parse tree, instead of just one successful one, and 2) it handles left-recursion automatically. It does these while still, fundamentally, being recursive descent, instead of needing to move to PEG or GLR or what have you.
But the 'magic' is actually really stupidly obvious once you struggle through the dozens of papers of overly dense language and symbols:
- First, when encountering a set of alternates, try _all of them_.
- Second, when encountering left-recursion, don't actually try parsing. Instead, _document that rule_ as a trigger.
Take that rule, A := A B. When you encounter that rule, _instead of actually trying to parse for an A_, register a trigger ("Whenever an A is matched, we must try to match A := A B, starting at the B, against that index"), and then _skip that alternate_.
Then, whenever matching a rule, check if there's any triggers against it and, if so, fire them - did we match an alternate that could fulfull a left-recursive rule? Okay, now we need to try that left-recursive rule!
If you just start from that first principle ("We can't actually parse the left-recursive rule until we know if an alternate matches"), and extrapolate the second principle from it ("Okay, so we need to try all alternates to know if there's any other rule that matches"), everything else emerges naturally.
How do you do that? Well, you try all alternates, and when we encounter one we can't, we note it down and give up, using that note to try again later when we have the info we'd need to do it.