parsinglr-grammar

How does LR parsing select a qualifying grammar production (to construct the parse tree from the leaves)?


I am reading a tutorial of the LR parsing. The tutorial uses an example grammar here:

S -> aABe
A -> Abc | b
B -> d

Then, to illustrate how the parsing algorithm works, the tutorial shows the process of parsing the word "abbcde" below.

enter image description here

I understand at each step of the algorithm, a qualifying production (namely a gramma rule, illustrated in column 2 in the table) is searched to match a segment of the string. But how does the LR parsing chooses among a set of qualifying productions (illustrate in column 3 in the table)?


Solution

  • An LR parse of a string traces out a rightmost derivation in reverse. In that sense, the ordering of the reductions applied is what you would get if you derived the string by always expanding out the rightmost nonterminal, then running that process backwards. (Try this out on your example - isn’t that neat?)

    The specific mechanism by which LR parsers actually do this involves the use of a parsing automaton that tracks where within the grammar productions the parse happens to be, along with some lookahead information. There are several different flavors of LR parser (LR(0), SLR(1), LALR(1), LR(1), etc.), which differ on how the automaton is structured and how they use lookahead information. You may find it helpful to search for a tutorial on how these automata work, as that’s the heart of how LR parsers actually work.