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

Indirect left recursion in my grammar?

My grammar appears to have a case of indirect left recursion, looked at some of the other similar question, cannot quite make a mental connection between them and my grammar, I can't get my head around how to solve it. A ::= A' a | A |…
0
votes
1 answer

I am getting a left recursion error in my antlr parser grammar

I am getting the error [fatal] rule statement has non-LL(*) decision due to recursive rule invocations reachable from alts 6,7. Resolve by left-factoring or using syntactic predicates or using backtrack=true option. I don't know exactly…
0
votes
1 answer

Eliminate left recursion for my grammar

Can you help me with this example? How should i do left recursion elimination with this size? i know how to do it for simpler examples. Expr1 ::= Number | String | `true` | `false` | `undefined` | Expr1…
Ezis
  • 73
  • 8
0
votes
2 answers

delete left recursion in a grammar

I have this grammar: agent = nil | @ | id | act . agent | agent + agent | agent "|" agent | agent \ restriction | agent [relabeling] | agent where agent_frame end | automation | (agent) where the priorities are: "where" < "+" < "|"…
Domenico
  • 101
  • 7
0
votes
0 answers

ANTLR3 mutually left-recursive Rule

Every solution I found on SO was "switch to ANTLR4" which isn't really an option because I am using antlr4ruby (which is ANTLR3, the 4 is meant as "for"). I want to build a rule for property access, it should match something like…
s-ol
  • 1,674
  • 17
  • 28
0
votes
2 answers

Why is this left-recursive and how do I fix it?

I'm learning ANTLR4 and I'm confused at one point. For a Java-like language, I'm trying to add rules for constructs like member chaining, something like that: expr1.MethodCall(expr2).MethodCall(expr3); I'm getting an error, saying that two of my…
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
0
votes
1 answer

ANTLR failure due to left recursion. How to solve?

Here is a simple grammar for an SQL select statement grammar SQL; @rulecatch { //We want to stop parsing when SyntaxException is encountered //So we re-throw the exception catch (SyntaxException e) { throw e; } } eval :…
Joe
  • 2,386
  • 1
  • 22
  • 33
0
votes
1 answer

Parsing Cool Language with antlr, cant print the desired out put

I'm a writing a parser/lexer for COOL ( classroom object-oriented language ). u can see the grammar in the following link: ( LAST PAGE OF THE MANUAL ) http://theory.stanford.edu/~aiken/software/cool/cool-manual.pdf I am using ANTLR to write this…
0
votes
1 answer

How to modify grammar to remove left recursive error in ANTLR4?

I'm trying to parse a language. The follow ANTLR4 parser rules are directly copied from the language specification : physical_value : raw_value DIV factor MUL factor PLUS offset ; raw_value : (physical_value MINUS offset) DIV factor ; but…
sara
  • 81
  • 1
  • 7
0
votes
1 answer

Eliminate left recursion with only e terminal

If there is a grammar: A -> AA | e where e is epsilon (the empty string) is there any way to eliminate left recursion from this grammar?
0
votes
1 answer

Removing Left Recursion Process

I'm just curious about something when doing left recursion, i've done this question and when i've done left recursion i've added F to everything in S. Do we always do this for left recursion (My teacher hasn't explained it very well). 1) S -> aSb |…
0
votes
1 answer

Eliminating left recursion for a specific grammar

I've looked through a million examples/tutorials but I still can't manage to eliminate left recursion for this grammar: S --> C C --> Dc|c D --> Cd|d Any ideas?
0
votes
1 answer

Constructing an LL(1) grammar with substituion, factoring and left recursion removal

Using any of the techniques (substitution, factoring, left-recursion removal), construct an LL(1) grammar accepting the same language as G. G over Σ = {0, 1, 2}: S → Y | 1X X → 1X | 0 Y → Y0 | 1X1 | 2X2 I did this so far: X is left…
0
votes
0 answers

Left-recursion removal, to get the equivalent grammar

S → Y | 0Z Y → Y1 | 1Z | 1Z 00 Z → 1Z | 0 I know the Y is left recursive but why is the Z not left recursive? For Y i got Y -> 1ZF | 1Z00 F -> 1F | e Then factoring Y -> 1ZX | 1ZX F -> 1F | e X -> F | 00F Where 'e' is empty
0
votes
0 answers

How to perform Left-Factoring on a Grammar, make it LL(1)

This is the grammar I have that is not left recursive. I have to make it LL(1). How do I go about doing this? S -> Exp eof Exp -> Term Exp2 Exp2 -> + Term Exp2 | - Term Exp2| e Term -> Factor Term2 Term2 -> * Factor Term2 | / Factor Term2 | e…
1 2 3
11
12