A lookahead left-to-right parser generator is a software tool that reads a BNF grammar and creates an LALR parser which is capable of parsing files written in the computer language defined by the BNF grammar. LALR parsers are desirable because they are very fast and small in comparison to other types of parsers. There are other types of parser generators, such as Simple LR parser, LR parser, GLR parser, LL parser and GLL parser generators. What differentiates one from another is the type of BNF grammar which they are capable of accepting and the type of parsing algorithm which is used in the generated parser. An LALR parser generator accepts an LALR grammar as input and generates a parser that uses an LALR parsing algorithm. In practice, LALR offers a good solution, because LALR grammars are more powerful than SLR, and can parse most practical LL grammars. LR grammars are more powerful than LALR, but canonical LR parsers can be extremely large in size and are considered not practical. Minimal LR parsers are small in size and comparable to LALR parsers.
History
Frank DeRemer invented LALR parsers with his PhD dissertation, called "Practical LR Translators", in 1969, at MIT. This was an important breakthrough, because LR translators, as defined by Donald Knuth in his 1965 paper, "On the Translation of Languages from Left to Right", were much too large for implementation on computer systems in the 1960s and 70's. An early LALR parser generator and probably the most popular one for many years was "yacc", created by Stephen Johnson in 1975 at AT&T Labs. Another, "TWS", was created by Frank DeRemer and Tom Pennello. Today, there are many LALR parser generators available, many inspired by and largely compatible with the original Yacc, for example GNU bison, a pun on the original Yacc/Yak. See Comparison of deterministic context-free language parser generators for a more detailed list.
Overview
The LALR parser and its alternatives, the SLR parser and the Canonical LR parser, have similar methods and parsing tables; their main difference is in the mathematical grammar analysis algorithm used by the parser generation tool. LALR generators accept more grammars than do SLR generators, but fewer grammars than full LR. Full LR involves much larger parse tables and is avoided unless clearly needed for some particular computer language. Real computer languages can often be expressed as LALR grammars. In cases where they can't, a LALR grammar is usually adequate. If the parser generator allows only LALR grammars, the parser typically calls some hand-written code whenever it encounters constructs needing extended lookahead. Similar to an SLR parser and Canonical LR parser generator, an LALR parser generator constructs the LR state machine first and then computes the lookahead sets for all rules in the grammar, checking for ambiguity. The Canonical LR constructs full lookahead sets. LALR uses merge sets, that is it merges lookahead sets where the LR core is the same. The SLR uses FOLLOW sets as lookahead sets which associate the right hand side of a LR core to a lookahead terminal. This is a greater simplification that in the case of LALR because many conflicts may arise from LR cores sharing the same right hand side and lookahead terminal, conflicts that are not present in LALR. This is why SLR has less language recognition power than LALR with Canonical LR being stronger than both since it does not include any simplifications.