Simple LR Parser - Lookahead Sets

Lookahead Sets

To understand the differences between SLR and LALR, you should first understand their many similarities and how they both make shift-reduce decisions. Please read article LR parser now for that background, up through the section on reductions' Lookahead Sets.

The one difference between SLR and LALR is how their generators calculate the lookahead sets of input symbols that should appear next, whenever some completed production rule is found and reduced.

SLR generators calculate that lookahead by an easy approximation method based directly on the grammar, ignoring the details of individual parser states and transitions. This ignores the particular context of the current parser state. If some nonterminal symbol S is used in several places in the grammar, SLR treats those places in the same single way rather than handling them individually. The SLR generator works out Follow(S), the set of all terminal symbols which can immediately follow some occurrence of S. In the parse table, each reduction to S uses Follow(S) as its LR(1) lookahead set. Such follow sets are also used by generators for LL top-down parsers. A grammar that has no shift/reduce or reduce/reduce conflicts when using Follow sets is called an SLR grammar.

LALR generators calculate lookahead sets by a more precise method based on exploring the graph of parser states and their transitions. This method considers the particular context of the current parser state. It customizes the handling of each grammar occurrence of some nonterminal S. See article LALR parser for further details of this calculation. The lookahead sets calculated by LALR generators are a subset of (and hence better than) the approximate sets calculated by SLR generators. If a grammar has table conflicts when using SLR follow sets, but is conflict-free when using LALR follow sets, it is called a LALR grammar.

Read more about this topic:  Simple LR Parser

Famous quotes containing the word sets:

    Eddie did not die. He is no longer on Channel 4, and our sets are tuned to Channel 4; he’s on Channel 7, but he’s still broadcasting. Physical incarnation is highly overrated; it is one corner of universal possibility.
    Marianne Williamson (b. 1953)