ArticleslgStudy

science

Left recursion

Left recursion is a 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 Left recursion rather than just read about it. In short: In the formal language theory of computer science, left recursion is a special case of recursion where a string is recognized as part of a language by the fact that it decomposes into a string from that same language (on the left) and a suffix (on the right). For instance, 1+2+3 can be recognized as a sum because it can be broken into 1+2, also a sum, and +3, a suitable suffix.

Left recursion — main illustration
Left recursion — illustration

Key takeaways

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

Reference excerpt

In the formal language theory of computer science, left recursion is a special case of recursion where a string is recognized as part of a language by the fact that it decomposes into a string from that same language (on the left) and a suffix (on the right). For instance, 1+2+3 can be recognized as a sum because it can be broken into 1+2, also a sum, and +3, a suitable suffix. In terms of context-free grammar, a nonterminal is left-recursive if the leftmost symbol in one of its productions is itself (in the case of direct left recursion) or can be made itself by some sequence of substitutions (in the case of indirect left recursion).

Definition A grammar is left-recursive if and only if there exists a nonterminal symbol A that can derive to a sentential form with itself as the leftmost symbol. Symbolically,

A⇒+Aα, where ⇒+ indicates the operation of making one or more substitutions, and α is any sequence of terminal and nonterminal symbols.

Direct left recursion Direct left recursion occurs when the definition can be satisfied with only one substitution. It requires a rule of the form

A→Aα where α is a sequence of nonterminals and terminals . For example, the rule

𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛+𝑇𝑒𝑟𝑚 is directly left-recursive. A left-to-right recursive descent parser for this rule might look like

and such code would fall into infinite recursion when executed.

Indirect left recursion Indirect left recursion occurs when the definition of left recursion is satisfied via several substitutions. It entails a set of rules following the pattern

A0→β0A1α0 A1→β1A2α1 ⋯ An→βnA0αn where β0,β1,…,βn are sequences that can each yield the empty string, while α0,α1,…,αn may be any sequences of terminal and nonterminal symbols at all. Note that these sequences may be empty. The derivation

A0⇒β0A1α0⇒+A1α0⇒β1A2α1α0⇒+⋯⇒+A0αn…α1α0 then gives A0 as leftmost in its final sentential form.

Uses Left recursion is commonly used as an idiom for making operations left-associative: that an expression a+b-c-d+e is evaluated as (((a+b)-c)-d)+e. In this case, that evaluation order could be achieved as a matter of syntax via the three grammatical rules

𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝑇𝑒𝑟𝑚 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛+𝑇𝑒𝑟𝑚 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛−𝑇𝑒𝑟𝑚 These only allow parsing the 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛 a+b-c-d+e as consisting of the 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛 a+b-c-d and 𝑇𝑒𝑟𝑚 e, where a+b-c-d in turn consists of the 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛 a+b-c and 𝑇𝑒𝑟𝑚 d, while a+b-c consists of the 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛 a+b and 𝑇𝑒𝑟𝑚 c, etc.

Removing left recursion Left recursion often poses problems for parsers, either because it leads them into infinite recursion (as in the case of most top-down parsers) or because they expect rules in a normal form that forbids it (as in the case of many bottom-up parsers). Therefore, a grammar is often preprocessed to eliminate the left recursion.

Removing direct left recursion The general algorithm to remove direct left recursion follows. Several improvements to this method have been made. For a left-recursive nonterminal A, discard any rules of the form A→A and consider those that remain:

A→Aα1∣…∣Aαn∣β1∣…∣βm where:

each α is a nonempty sequence of nonterminals and terminals, and each β is a sequence of nonterminals and terminals that does not start with A. Replace these with two sets of productions, one set for A:

A→β1A′∣…∣βmA′ and another set for the fresh nonterminal A′ (often called the "tail" or the "rest"):

A′→α1A′∣…∣αnA′∣ϵ Repeat this process until no direct left recursion remains. As an example, consider the rule set

𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛+𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛∣𝐼𝑛𝑡𝑒𝑔𝑒𝑟∣𝑆𝑡𝑟𝑖𝑛𝑔 This could be rewritten to avoid left recursion as

𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝐼𝑛𝑡𝑒𝑔𝑒𝑟𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛′∣𝑆𝑡𝑟𝑖𝑛𝑔𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛′ 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛′→+𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛∣ϵ

Removing all left recursion The above process can be extended to eliminate all left recursion, by first converting indirect left recursion to direct left recursion on the highest numbered nonterminal in a cycle.

Inputs A grammar: a set of nonterminals A1,…,An and their productions Output A modified grammar generating the same language but without left recursion For each nonterminal Ai: Repeat until an iteration leaves the grammar unchanged: For each rule Ai→αi, the αi being a sequence of terminals and nonterminals: If αi begins with a nonterminal Aj and j<i: Let βi be αi without its leading Aj. Remove the rule Ai→αi. For each rule Aj→αj: Add the rule Ai→αjβi. Remove direct left recursion for Ai as described above. Step 1.1.1 amounts to expanding the initial nonterminal Aj in the right hand side of some rule Ai→Ajβ, but only if j<i. If Ai→Ajβ was one step in a cycle of productions giving rise to a left recursion, then this has shortened that cycle by one step, but often at the price of increasing the number of rules. The algorithm may be viewed as establishing a topological ordering on nonterminals: afterwards there can only be a rule Ai→Ajβ if j>i. Note that this algorithm is highly sensitive to the nonterminal ordering; optimizations often focus on choosing this ordering well.

Pitfalls Although the above transformations preserve the language generated by a grammar, they may change the parse trees that witness strings' recognition. With suitable bookkeeping, tree rewriting can recover the originals, but if this step is omitted, the differences may change the semantics of a parse. Associativity is particularly vulnerable; left-associative operators typically appear in right-associative-like arrangements under the new grammar. For example, starting with this grammar:

𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛−𝑇𝑒𝑟𝑚∣𝑇𝑒𝑟𝑚 𝑇𝑒𝑟𝑚→𝑇𝑒𝑟𝑚∗𝐹𝑎𝑐𝑡𝑜𝑟∣𝐹𝑎𝑐𝑡𝑜𝑟 𝐹𝑎𝑐𝑡𝑜𝑟→(𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛)∣𝐼𝑛𝑡𝑒𝑔𝑒𝑟 the standard transformations to remove left recursion yield the following:

𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛→𝑇𝑒𝑟𝑚 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛′ 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛′→−𝑇𝑒𝑟𝑚 𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛′∣ϵ 𝑇𝑒𝑟𝑚→𝐹𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚′ 𝑇𝑒𝑟𝑚′→∗𝐹𝑎𝑐𝑡𝑜𝑟 𝑇𝑒𝑟𝑚′∣ϵ 𝐹𝑎𝑐𝑡𝑜𝑟→(𝐸𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛)∣𝐼𝑛𝑡𝑒𝑔𝑒𝑟 Parsing the string "1 - 2 - 3" with the first grammar in an LALR parser (which can handle left-recursive grammars) would have resulted in the parse tree:

This parse tree groups the terms on the left, giving the correct semantics (1 - 2) - 3. Parsing with the second grammar gives

… excerpt ends here. Continue reading the full article.

Illustrations

Left recursion: Right-recursive parsing of a double subtraction
Right-recursive parsing of a double subtraction

Worked examples

Example 1 — a first encounter with Left recursion

Start with the simplest possible case. Write down what Left recursion claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In 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 Left recursion 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 Left recursion 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 Left recursion

In research
Left recursion appears in 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 Left recursion 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
Left recursion is common in secondary-school and first-year university syllabi. It links to neighbouring topics Control flow, Formal languages, Parsing, so understanding it makes those chapters shorter.
In everyday life
Look for Left recursion 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 “Left recursion” →

How to study Left recursion in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Left recursion 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 Left recursion out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Left recursion in simple terms?

In the formal language theory of computer science, left recursion is a special case of recursion where a string is recognized as part of a language by the fact that it decomposes into a string from that same language (on the left) and a suffix (on the right). For instance, 1+2+3 can be recognized a…

Why does Left recursion matter?

Because it connects several 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 Left recursion?

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 Left recursion.

Tags

  • Control flow
  • Formal languages
  • Parsing
  • Recursion

Keep exploring