ArticleslgStudy

computer science

LR parser

LR parser is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand LR parser rather than just read about it. In short: 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 parser — main illustration
LR parser — illustration

Key takeaways

  • LR parser belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect LR parser to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of LR parser from memory before moving on to harder problems.

Reference excerpt

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.

Illustrations

LR parser: Bottom-up parser at step 6
Bottom-up parser at step 6
LR parser: Bottom-up parse of 1+1
Bottom-up parse of 1+1

Worked examples

Example 1 — a first encounter with LR parser

Start with the simplest possible case. Write down what LR parser claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to LR parser before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about LR parser ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of LR parser

In research
LR parser appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses LR parser in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
LR parser is common in secondary-school and first-year university syllabi. It links to neighbouring topics Parsing algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for LR parser outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “LR parser” →

How to study LR parser in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what LR parser means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain LR parser out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is LR parser in simple terms?

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).

Why does LR parser matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study LR parser?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on LR parser.

Tags

  • Parsing algorithms

Keep exploring