LALR parser


In computer science, an LALR parser or Look-Ahead LR parser is a simplified version of a canonical LR parser, to parse a text according to a set of production rules specified by a formal grammar for a computer language.
The LALR parser was invented by Frank DeRemer in his 1969 PhD dissertation, Practical Translators for LR languages, in his treatment of the practical difficulties at that time of implementing LR parsers. He showed that the LALR parser has more language recognition power than the LR parser, while requiring the same number of states as the LR parser for a language that can be recognized by both parsers. This makes the LALR parser a memory-efficient alternative to the LR parser for languages that are LALR. It was also proven that there exist LR languages that are not LALR. Despite this weakness, the power of the LALR parser is sufficient for many mainstream computer languages, including Java, though the reference grammars for many languages fail to be LALR due to being ambiguous.
The original dissertation gave no algorithm for constructing such a parser given a formal grammar. The first algorithms for LALR parser generation were published in 1973. In 1982, DeRemer and Tom Pennello published an algorithm that generated highly memory-efficient LALR parsers. LALR parsers can be automatically generated from a grammar by an LALR parser generator such as Yacc or GNU Bison. The automatically generated code may be augmented by hand-written code to augment the power of the resulting parser.

History

In 1965, Donald Knuth invented the LR parser. The LR parser can recognize any deterministic context-free language in linear-bounded time. Rightmost derivation has very large memory requirements and implementing an LR parser was impractical due to the limited memory of computers at that time. To address this shortcoming, in 1969, Frank DeRemer proposed two simplified versions of the LR parser, namely the Look-Ahead LR and the Simple LR parser that had much lower memory requirements at the cost of less language-recognition power, with the LALR parser being the most-powerful alternative. In 1977, memory optimizations for the LR parser were invented but still the LR parser was less memory-efficient than the simplified alternatives.
In 1979, Frank DeRemer and Tom Pennello announced a series of optimizations for the LALR parser that would further improve its memory efficiency. Their work was published in 1982.

Overview

Generally, the LALR parser refers to the LALR parser, just as the LR parser generally refers to the LR parser. The "" denotes one-token lookahead, to resolve differences between rule patterns during parsing. Similarly, there is an LALR parser with two-token lookahead, and LALR parsers with k-token lookup, but these are rare in actual use. The LALR parser is based on the LR parser, so it can also be denoted LALR = LALR or more generally LALR = LALR. There is in fact a two-parameter family of LALR parsers for all combinations of j and k, which can be derived from the LR parser, but these do not see practical use.
As with other types of LR parsers, an LALR parser is quite efficient at finding the single correct bottom-up parse in a single left-to-right scan over the input stream, because it does not need to use backtracking. Being a lookahead parser by definition, it always uses a lookahead, with being the most-common case.

Relation to other parsers

LR parsers

The LALR parser is less powerful than the LR parser, and more powerful than the SLR parser, though they all use the same production rules. The simplification that the LALR parser introduces consists in merging rules that have identical kernel item sets, because during the LR state-construction process the lookaheads are not known. This reduces the power of the parser because not knowing the lookahead symbols can confuse the parser as to which grammar rule to pick next, resulting in reduce/reduce conflicts. All conflicts that arise in applying a LALR parser to an unambiguous LR grammar are reduce/reduce conflicts. The SLR parser performs further merging, which introduces additional conflicts.
The standard example of an LR grammar that cannot be parsed with the LALR parser, exhibiting such a reduce/reduce conflict, is:
S → a E c
→ a F d
→ b F c
→ b E d
E → e
F → e
In the LALR table construction, two states will be merged into one state and later the lookaheads will be found to be ambiguous. The one state with lookaheads is:
E → e.
F → e.
An LR parser will create two different states, neither of which is ambiguous. In an LALR parser this one state has conflicting actions, a "reduce/reduce conflict"; the above grammar will be declared ambiguous by a LALR parser generator and conflicts will be reported.
To recover, this ambiguity is resolved by choosing E, because it occurs before F in the grammar. However, the resultant parser will not be able to recognize the valid input sequence b e c, since the ambiguous sequence e c is reduced to c, rather than the correct c, but b E c is not in the grammar.

LL parsers

The LALR parsers are incomparable with LL parsers: for any j and k both greater than 0, there are LALR grammars that are not LL grammars and conversely. In fact, it is undecidable whether a given LL grammar is LALR for any.
Depending on the presence of empty derivations, a LL grammar can be equal to a SLR or a LALR grammar. If the LL grammar has no empty derivations it is SLR and if all symbols with empty derivations have non-empty derivations it is LALR. If symbols having only an empty derivation exist, the grammar may or may not be LALR.