In computer science, LR parsers are a type of bottom-up parser that analyse deterministic context-free languages in linear time. There are several variants of LR parsers: SLR parsers, LALR parsers, canonical LR(1) parsers, minimal LR(1) parsers, and generalized LR parsers (GLR parsers). LR parsers can be generated by a parser generator from a formal grammar defining the syntax of the language to be parsed. They are widely used for the processing of computer languages. An LR parser (left-to-right, rightmost derivation in reverse) reads input text from left to right without backing up (this is true for most parsers), and produces a rightmost derivation in reverse: it does a bottom-up parse – not a top-down LL parse or ad-hoc parse. The name "LR" is often followed by a numeric qualifier, as in "LR(1)" or sometimes "LR(k)". To avoid backtracking or guessing, the LR parser is allowed to peek ahead at k lookahead input symbols before deciding how to parse earlier symbols. Typically k is 1 and is not mentioned. The name "LR" is often preceded by other qualifiers, as in "SLR" and "LALR". The "LR(k)" notation for a grammar was suggested by Knuth to stand for "translatable from left to right with bound k." LR parsers are deterministic; they produce a single correct parse without guesswork or backtracking, in linear time. This is ideal for computer languages, but LR parsers are not suited for human languages which need more flexible but inevitably slower methods. Some methods which can parse arbitrary context-free languages (e.g., Cocke–Younger–Kasami, Earley, GLR) have worst-case performance of O(n3) time. Other methods which backtrack or yield multiple parses may even take exponential time when they guess badly. The above properties of L, R, and k are actually shared by all shift-reduce parsers, including precedence parsers. But by convention, the LR name stands for the form of parsing invented by Donald Knuth, and excludes the earlier, less powerful precedence methods (for example Operator-precedence parser). LR parsers can handle a larger range of languages and grammars than precedence parsers or top-down LL parsing. This is because the LR parser waits until it has seen an entire instance of some grammar pattern before committing to what it has found. An LL parser has to decide or guess what it is seeing much sooner, when it has only seen the leftmost input symbol of that pattern.
Overview
Definition of LR(k) grammars
While LR(k) grammars are mainly used for parsing, the following definition of the LR(k) grammars instead uses the dual perspective: That of beginning with the start symbol S and repeatedly applying a grammar's production rules so as to produce a sequence of sentential forms. By a sentence, we mean a sentential form without any nonterminal symbols. A right-sentential form is any sentential form which may be obtained by beginning with the start symbol S and repeatedly applying productions rules only to the rightmost nonterminals. The entire LR framework uses only right-sentential forms. The definition of LR(k) is now short: A context-free grammar G is called LR(k) if for every pair of derivations of right-sentential forms which matches the following pattern, we may conclude that α=α′ and B=B′:S⟹∗αBc1c2⋯ckγ⟹αβc1c2⋯ckγS⟹∗α′B′c1c2⋯ckγ′⟹αβc1c2⋯ckγ′Here:
α, α′, β denote any string of terminals and nonterminals, γ and γ′ denote any string of terminals, B and B′ denote nonterminals, and c1,c2,…,ck: are all terminals whenever either γ or γ′ is non-empty, or (whenever γ and γ′ are simultaneously empty) begin with an initial segment c1,c2,…,cj consisting only of terminals, followed by a remainder cj+1,cj+2,…,ck which are all empty.
Further terminology and discussion The prefix α is called the viable prefix, the substring β is called the handle, and the string c1c2⋯ck is called the lookahead. Observe that from the definition of "right-sentential form", we have that c1,c2,…,ck, γ and γ′consist of only terminal symbols. Note that parsing consists of applying production rules backwards, which is called reducing. Furthermore, a parser will visit the symbols in an input string from left to right, which is backwards to the above derivations. The intuition behind the LR(k) property is then that the only context needed to reduce β to B is (i) the entire viable prefix α (ii) the handle β itself (iii) and k additional symbols of lookahead. Notably, it doesn't matter whether γ=γ′ or not. Whether or not a CFG is LR(k) for a fixed k is decidable. Whether there exists any k for which a CFG G is LR(k) is not decidable. In practice, k=1 is chosen. Any deterministic context-free language admits an LR(1) grammar, but this is in general neither small nor unique, and each of those grammars will result in a different parse tree for the same string.
Bottom-up parse tree for example A * 2 + 1
An LR parser scans and parses the input text in one forward pass over the text. The parser builds up the parse tree incrementally, bottom up, and left to right, without guessing or backtracking. At every point in this pass, the parser has accumulated a list of subtrees or phrases of the input text that have been already parsed. Those subtrees are not yet joined together because the parser has not yet reached the right end of the syntax pattern that will combine them. At step 6 in an example parse, only "A * 2" has been parsed, incompletely. Only the shaded lower-left corner of the parse tree exists. None of the parse tree nodes numbered 7 and above exist yet. Nodes 3, 4, and 6 are the roots of isolated subtrees for variable A, operator *, and number 2, respectively. These three root nodes are temporarily held in a parse stack. The remaining unparsed portion of the input stream is "+ 1".
Shift and reduce actions As with other shift-reduce parsers, an LR parser works by doing some combination of Shift steps and Reduce steps.
… excerpt ends here. Continue reading the full article.


