Questions tagged [left-recursion]

Left recursion means a nonterminal symbol can derive to something longer with itself as the leftmost symbol. Applies to questions about using left-recursive grammars, finding them or converting to them, even from them.

44 questions
24
votes
4 answers

Why is left recursion bad?

In compiler design, why should left recursion be eliminated in grammars? I am reading that it is because it can cause an infinite recursion, but is it not true for a right recursive grammar as well?
user56833
9
votes
2 answers

Correct name for a recursive descent parser that uses loops to handle left recursion?

This grammar is left recursive: Expression ::= AdditionExpression AdditionExpression ::= MultiplicationExpression | AdditionExpression '+' MultiplicationExpression | AdditionExpression '-'…
9
votes
1 answer

Arithmetic expressions grammar transformation

In the article Parsing Expressions by Recursive Descent by Theodore Norvell (1999) the author starts with the following grammar for arithmetic expressions: E --> E "+" E | E "-" E | "-" E | E "*" E | E "/" E | E "^" E | "(" E ")" | v which is quite…
8
votes
1 answer

Left-Factoring a grammar into LL(1)

I have a homework assignment where I need to convert a grammar into LL(1). I've already removed the left recursion, but I'm having trouble doing left-factoring. All of the examples I've found are simple, and look something like this: A -> aX |…
8
votes
1 answer

Left recursion and left factoring -- which one goes first?

if I have a grammar having a production that contains both left recursion and left factoring like $\qquad \displaystyle F \to FBa \mid cDS \mid c$ which one has priority, left recursion or left factoring?
6
votes
2 answers

How does this left-associative recursive descent parser work?

For personal enlightenment, I'm trying to write a recursive descent parser for lambda calculus without abstraction, i.e., just identifiers and function application. The BNF grammar that describes the language could be this, where is a terminal…
4
votes
3 answers

Why not use Right Recursion to avoid Left Recursion?

I am reading about Representative Grammars from book, where I encountered the following grammar: $$ E \rightarrow E + T \ | \ T $$ $$ T \rightarrow T * F \ | \ F $$ $$ F \rightarrow (E) \ | \ id$$ The above grammar is left-recursive, which is bad.…
forthright48
  • 43
  • 1
  • 5
4
votes
1 answer

Removing Left Recursion from Context-Free Grammars - Ordering of nonterminals

I have recently implemented the Paull's algorithm for removing left-recursion from context-free grammars: Assign an ordering $A_1, \dots, A_n$ to the nonterminals of the grammar. for $i := 1$ to $n$ do begin $\quad$ for $j:=1$ to $i-1$ do…
3
votes
1 answer

Conversion of ambiguous left recursive grammar to LL(1)

I have been strugling with this LL(1) transformation for far longer than expected and am getting quite desperate. We are given a grammar $ G = \left\{V_N, V_T, S, \Phi \right\}$, with the non-terminal set $V_N = \left\{S\right\}$, terminal set $V_T…
3
votes
1 answer

LL grammars and left-recursiviity

Why LL(k) and LL(∞) are incompatible with left-recursion? I understand that a LL(k) language can support left-recursivity provided that with k-overahead tokens can be resolved any ambiguity. But, with a LL(∞) grammar, which type of ambiguities can't…
ABu
  • 529
  • 2
  • 11
3
votes
2 answers

How to modify semantic actions when removing left-recursion from a grammer

Is there any algorithm that tells us how to modify semantic actions associated with a left-recursive grammar? For example, we have the following grammar, and its associated semantic actions: $ S \rightarrow id = expr $ { S.s = expr.size } S…
Arani
  • 523
  • 4
  • 11
3
votes
0 answers

Automatic tool for resolving left-recursion within CFG

Though facing the fear that someone might not like my question but does somebody know a useful tool to either resolve left recursion or to simplify a context-free grammar automatically ? I need to resolve a rather long chain of left-recursion in…
2
votes
1 answer

Explanation of Grammar Ambiguity

Consider now the grammar G S -> (S + S) S -> (S * S) S -> a Is it ambiguous? Answer: No because there are disambiguating parentheses and so no left or right recursion. Can someone explain how the parentheses are disambiguating. I could understand if…
Daniel
  • 23
  • 2
2
votes
1 answer

Can I remove left recursion on this grammar

$S \rightarrow Sa \mid S \mid \epsilon$ This is a weird case where I have only one non-terminal. I'm trying to apply the algorithm 4.19 in the dragon book. It don't think it should be applicable but I think it gives something like that : $S…
2
votes
1 answer

Indirect Left recursion

I'm resolving indirect left recursion for these production rules: S -> Aa / a eq1 A -> Sb / b. eq2 Where S is the starting symbol. Now I can do this in two ways: Putting A in eq1 So I'll get the solution (sol1): S -> Sba /a /ba and then S ->…
user10859
  • 21
  • 4
1
2 3