Questions tagged [left-recursion]

A special kind of recursion, defined through a particular grammar property: grammar is left-recursive if we can find some non-terminal A which will eventually derive a sentential form with itself as the left-symbol.

Left-recursive grammar can be immediate or indirect:

Immediate left recursion

Immediate left recursion occurs in rules of the form

A → Aa | b

where a and b are sequences of nonterminals and terminals, and b doesn't start with A. For example, the rule

Expr → Expr + Term

is immediately left-recursive.

Indirect left recursion

Indirect left recursion in its simplest form could be defined as:

A → Ba | C
B → Ab | D

possibly giving the derivation A ⇒ Ba ⇒ Aba ⇒ ...

More generally, for the nonterminals A0, A1, ..., An, indirect left recursion can be defined as being of the form:

A0 → A1a_1 | ...
A1 → A2a_2 | ...
...
An → A0a_n+1 | ...

where a_1, a_2, ..., a_n are sequences of nonterminals and terminals.

Source: Wikipedia

179 questions
1
vote
1 answer

How to parse a recursive left syntax rule with FParsec?

I usually use FParsec for LL grammars, but sometimes it happens that in a whole grammar only one element requires left recursive parsing (so the grammar is no longer LL). Currently I have such a situation, I have a large LL grammar implemented with…
Foxy
  • 980
  • 8
  • 17
1
vote
1 answer

How do I write this `expression` PEG grammar so that it is not recursive, or so that I can use the prec climber?

I am creating a parser for solidity and I am using the PEST parser for rust. I am trying to write the grammar for expression, but there are many cases of left recursion. I have looked online and know that you can use the prec climber but I am not…
0xKitsune
  • 131
  • 6
1
vote
0 answers

why is only left recursion eliminated in top down parsing?

I recently came across parsers in the systems software course. I read that, to make the top down parsing using backtracking efficient, it is needed to eliminate the left recursion in the production rules (for eliminating the infinite looping). For…
1
vote
1 answer

Left-recursion in ANTLR

The stm and stmList gives me this error, it seems that ANTLR see it like a possible infinite recursion loop. How can I avoid it? The following sets of rules are mutually left-recursive [stmList] stmList: stm stmList | ; stm: ifStm | whStm; ifStm:…
Duy Duy
  • 521
  • 1
  • 4
  • 9
1
vote
1 answer

ANTLR4 self and mutual left-recursion

Is there a simple transformation or workaround to make this work in ANTLR4? a : a p | b q | c ; b : b r | a s | d ; That is, a and b are self-left-recursive and mutual-left-recursive, the other rules (c, d, p, q, r, s) are just…
TFuto
  • 1,361
  • 15
  • 33
1
vote
0 answers

Eliminating left recursion from function call parsing

First of all, I know there are a lot of answers and resources already available regarding this topic. However, I'm having a really hard time understanding the grammar notation that is often used in these answers. So I was hoping someone could…
1
vote
2 answers

Removing indirect left recursion from this grammar

I'm trying to figure out how to remove the indirect left recursion from the logical keyword expressions within my Rust port of a Ruby parser (https://github.com/kenaniah/ruby-parser/blob/master/ruby-parser/src/parsers/expression/logical.rs). The…
Kenaniah
  • 5,171
  • 24
  • 27
1
vote
0 answers

Left Recursion Elimination, Star Enclosure

I have a grammar that has left recursion I tried removing them and I was able to remove two of them but for one of them I don't know what to do! Here is the grammar: R -> R "|" S R -> S S -> S.T S -> T T -> T* T -> U U -> (R) U -> a U -> "ε"…
1
vote
1 answer

Left recursion elimination questions

I'm working on remove left recursion in grammar. (3 grammars) 1. A->Ab | aC B->BaBB | BA C->bC | BA 2. T->Txxy | TaabT | TTa 3. A-> BA | Baa B-> Ab | Abb I've tried to do…
JUN_0
  • 49
  • 1
  • 5
1
vote
0 answers

Reading input in order, removing left-recursion

I am in the middle of a Compilers class. Conditions so far: I have all my tokens stored in vectors, one as String literals, the other as Int to store what type (keyword, identifier, operator, etc.) of token they are. I would like your advice on how…
1
vote
0 answers

How to eliminate both direct and indirect left recursion?

For this grammar: A -> B a | A a | c B -> B b | A b | d How to remove the left recursion? ====== My attempts ====== Eliminate direct left_rec first. A -> (B a | c) A' A' -> a A' | ε B -> (A b | d) B' B' -> b B' | ε Then indirect. Put B into…
1
vote
0 answers

Tree of unary operators

I want to parse expressions like a ++a a++ ++a++ ++a++++ The pre-increment operator has precedence over the post-increment operator. The parsers I have are these: public static readonly TokenListParser Increment =…
SuperJMN
  • 13,110
  • 16
  • 86
  • 185
1
vote
2 answers

Overcoming infinite left recursion in TextX parser

I am writing a parser for an existing language, using the TextX Python Library (based on the Arpeggio PEG parser) But when I try to use it to parse a file, I get the exception: RecursionError: maximum recursion depth exceeded while calling a Python…
Chen Levy
  • 15,438
  • 17
  • 74
  • 92
1
vote
1 answer

Removal of indirect left recursion (I don't understand formal symbols)

I've tried looking for answers to my solution but I can't seem to wrap my head around the generalized solutions. It doesn't help that I can't figure out which of my elements map to capital letters and which are supposed to be represented by small…
markonius
  • 625
  • 6
  • 25
1
vote
1 answer

Creating parse tree to determine correctness of the LL Grammar given

I am have a program that will currently generate a output of tokens that is to be used for a input on this next program. It is going to be checking the correctness of the syntax of the code. I am running into issues on how I would start converting…