pixx@pixx@merveilles.town

(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.

2 3

불러오는 중…

pixx@pixx@merveilles.town
@pixx@merveilles.town 님에게 답글

Generalized LL parsing, in even shorter, reframes how we think of grammar rules. Instead of a sequence of events, we view them as a series of sequential conditions.

A := A B
A := C

Either alternate of A satisfies the condition in the first alternate ("If it begins with an A, and has a B afterwards, it's an A."; both a standalone C, or an A comprised of _another A_ followed by a B - either one is an A, and satisfies this condition).

Since there's multiple possible rules that can satisfy this condition, we must explicitly track the _condition itself_, so that if _any A ever matches_, we know that the condition has been met, since naive recursion is impossible.

but for a single-definition B := C, where B is not a possible first expansion of C, we don't need to do so

Because when there's only one condition, we can just check "Hey, does it match?"

Which is why GLL, despite being able to handle _any_ grammar in O(n^3), remains O(n) for LL grammars: because this is _already a correct way to think about LL_!

pixx@pixx@merveilles.town
@pixx@merveilles.town 님에게 답글

Okay, I was able to - I think - explain GLL to someone who _doesn't know anything about compilers_ today in like three minutes. I really should clean up the post and do a proper explanation, it's _incredibly_ simple when you understand it.

I went with an even easier one this time: English.

If we look at a term in the language as a noun with its descriptors (adjectives), and at terms where adjectives are written out afterwards (as in "the apple, red and large, loomed..."), then we can say that a term is either a noun, or a term that has an adjective afterwards.

In classic parsing, to parse a sentence, we try to parse the terms and how they relate - but if we ask "is this a valid term?", we can't answer that without _first asking if it's a valid term_. This is obviously ridiculous.

So, instead, we can just reframe the problem a little bit: whenever we see that we're stuck, because we need more information to proceed, we just _note that down_ - that we can't check if this definition of a term works _until we know if there's an underlying term first_.

And then we move on. When we notice that there's a noun - and thus a term - we can reference the note from earlier and go "oh, so we should check if there's an adjective afterwards."

ANd if there is, we've now found a term, so we do the same thing again.

It's really that simple. Back in technical compiler land, to go from LL to GLL, _all we do_ is keep track of the stack of what we're parsing ("sentence -> term") and, when infinite recursion is detected ("Okay, term := term adjective - we're already looking for a term at the current position, this wouldn't work"), we literally just note down "Okay, if we ever find a term at this position, we should come back and try this rule again".

And then, when popping entries off the stack, we check those notes.

So why the hell are all the descriptions this[1] fucking awful???

[1] rahul.gopinath.org/post/2022/0

pixx@pixx@merveilles.town
@pixx@merveilles.town 님에게 답글

Part of it, I think, is that most explanations of GLL focus very heavily on the "we need to extract one parse tree for every possible parse, which means we need a lot of complicated logic around this!" when... you don't, actually, have to do that at all.

Most LL implementations don't try this. Generalizing it to support recursive grammars _does not_ need to mean adding in this additional complexity, just because it makes it _possible_!