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
0
votes
1 answer

Does Boost Spirit X3 support left recursion?

One of the shortcomings/implementation challenges of recursive-descent parsers is dealing with left recursion, e.g. := '+' | The parser needs to parse an expr before it can parse an expr... Now,…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
0
votes
1 answer

How to eliminate this left recursion

I'm doing an assignment in compiler contruction and I'm having trouble with left recursion. JavaCC gives me an error "Left recursion detected" for expression() and condition(), shown below. The second line of each are the same, so I assume that is…
Jack Finan
  • 99
  • 1
  • 8
0
votes
2 answers

How can I solve left recursion in cfg parser?

How can I solve this left recursion? I have used long time to find a solution but I struggle with it. sentence: Bob gives Max cars. The main point I wanted to show here is the left recursion which occurs. Which is NP -> NP, how can I solve this…
0
votes
0 answers

Implementing Elimination of Immediate Left-Recursion in Java

I am working on implementing a generic code to solve left recursion problem in a grammar using java so my code is working as follows I am reading an input like this as each line goes to the next line: E E+T|T T T*F|F F (E)|id|number and the…
0
votes
1 answer

Preferred way of removing left recursion of EBNF

Many textbooks propose different techniques of removing left recursion. Some of them alter the associativity, which is something I would like to avoid. What is the most straightforward way to remove left recursion in the following (example)…
Leandros
  • 16,805
  • 9
  • 69
  • 108
0
votes
1 answer

Grako left recursion

I'm trying to use grako to describe a simple left-recursive grammar but I have trouble to do so. Right-recursion does work without any problem : symbol = /[a-z]/ ; condition = symbol "AND" condition | symbol ; start = condition $ ; According to all…
Jbb
  • 483
  • 3
  • 15
0
votes
1 answer

Are these grammars left recursive and why?

I've got these grammars to solve the left recursion. But why are these grammars left recursive? They are not following the schema A -> Aa | b: 1., S → 0S1 | 01 2., S → + SS | * SS
0
votes
1 answer

Removing mutual left-recursion from left-recursive rules

With ANTLR 4.6, snapshot of 11/23/2016. I have two rules which are each left-recursive. I expanded several of the alternatives to expose the left recursion. ANTLR4 handles this, because the left recursion is explicit. However, the two rules are also…
Robert B
  • 3,195
  • 2
  • 16
  • 13
0
votes
1 answer

Left recursive call error and translation of grammars

I'm having problems with a slightly different grammar, it's a grammar with a single rule (a giant one), and with several calls to itself. I do not know how to represent this grammar using Xtext, it is an academic work, a language to specify…
lpFranz
  • 406
  • 5
  • 22
0
votes
1 answer

Removing direct left recursion in JavaCC

I have the following in a JavaCC file: void condition() : {} { expression() comp_op() expression() | condition() ( | ) condition() } where is "&&" and is "||". This is causing problems due to the fact that it is direct…
Aine
  • 195
  • 1
  • 2
  • 10
0
votes
0 answers

context free grammar and removing left recursion

The following grammar has left recursion E -> E + T|T T -> T * F|F F -> F*|a | b How to remove it? Is there any general procedure for it?
Shadow
  • 1
0
votes
0 answers

ANTLR4 mutually left-recursive error

I currently do a recognizer in ANTLR4 and I have an error: The following sets of rules are mutually left-recursive [expr, index_expr, member_acc, expr1] This is my code: num_opds: INTLIT|FLOATLIT; bool_opds: TRUE|FALSE; str_opds:…
Alex
  • 25
  • 6
0
votes
0 answers

Does YACC (LALR) parses left recursive grammars

I was doing a simple C parser project. This problem occurred when I was writing the grammar for if-else construct. the grammar that I have written is as following: iexp: IF OP exp CP block eixp END{ printf("Valid if-else…
Madhusoodan P
  • 681
  • 9
  • 20
0
votes
1 answer

convert to LL(k) grammar

I was trying to solve a question for practice, but I met a problem when I tried to compare the sample answer with mine. Here is the grammar before conversion: E-> S* S-> SD S-> D D-> [D] D-> x The start symbol is E and the other non-terminal…
dajavanoob
  • 15
  • 7
0
votes
2 answers

Parsing + and * in boolean expressions by recursive descent

I am writing a recursive descent parser for Boolean expressions, for example: (1 * 0) (0 + ~1) (0 * (1 + c) Where 1 is 'True', 0 is 'False', + is 'or', * is 'and', ~ is 'not' and 'c' is just some variable name (it could be any single alphabetic…
K810
  • 3
  • 2