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

Parsing a nested list with Antlr3, non-LL(*) decision due to recursive rule invocations

I have the following grammar to parse a nested list using Antlr3 parse: list ; list: LBRACK list_element* RBRACK ; list_element: tree_ | list ; tree_: node | ATOM ; node: LBRACK tree_ SEPARATOR tree_ RBRACK ; ATOM: 'nil'; LBRACK: '('; RBRACK:…
Richard Guy
  • 470
  • 4
  • 12
0
votes
1 answer

Why if I add lambda left recursion occurs?

I am trying to write if syntax by using flex bison and in parser I have a problem here is a grammar for if syntax in cpp program : //start rule |statements; block: TOKEN_BEGIN statements ';'TOKEN_END; reexpression: …
0
votes
1 answer

How can I fix my grammar I wrote?

I came up with the following grammar that enforces precedence: A : L ( '[' A ']' L* )* L : M (('+'|'-')M)* M : P (('*'|'/')P)* P : ID | NUM where ID can be a letter and num is an integer. Problem I can parse the following string: a[i +…
GeekyOmega
  • 1,235
  • 6
  • 16
  • 34
0
votes
2 answers

lambda calculus grammar LLR

I am trying to write a lambda calculus parser, the grammar I defined seems not in LLR: E ::= x | \x.E | EE | (E) I reduce the left recursive: E ::= xE' | \x.EE' | (E)E' E'::= EE' | seems not right, can anyone help with?
0
votes
1 answer

Where is the left recursion?

The below is a snippet of an ANTLR grammar which works fine. It is intended to become a tex-parser. If I modify rule everywhere by uncommenting | text, ANTLR reports a left recursion in rule escSeq which I think is funny. I cannot find a left…
user2609605
  • 419
  • 2
  • 14
0
votes
1 answer

Is this grammar left recursive or factor?

I am new to this topic of left recursion and left factoring, please help me in determining whether this grammar is left recursive or left factored, if it is then why ? S-> aAd | bBd | aBe | bAe | cA | cB
android_guy
  • 236
  • 1
  • 7
  • 18
0
votes
1 answer

ANTLR4 Mutually Left-recursive Error When Parsing C++ Source

I'm trying to parse a subset of cpp source syntax. The follow ANTLR4 parser rules are directly copied from the c++ language specification (except hypens are replaced by underscores): abstract_declarator: ptr_operator abstract_declarator? | …
JavaMan
  • 4,954
  • 4
  • 41
  • 69
-1
votes
1 answer

Removing Left recursion in CFG's

How would I go about removing left recursion from this line from a grammar? A -> D | R | r B A n ; | AA | epsilon r, n and ; are terminal symbols. D,R,B and A are all non-terminals. Thanks in advance
-1
votes
1 answer

Can epsilon production be assumed in a left recursive grammar

I have a grammar: S->Sa|Sb I want to know if I can assume S->e as a production in the grammar? I.e., Is S->Sa|Sb is the same as S->Sa|Sb|e ? e = null string (epsilon) I am trying to understand removal of left recursion and I came across this…
-1
votes
1 answer

How to remove left recursion from A -> Aα | ε

I think the left-recursion for this grammar is not removable. Correct me if I am wrong. α is Alpha that is a Non-terminal ε is Epsilon.
-1
votes
1 answer

Removing Left recursion and factoring from a Grammar

I wanted to ask a question regrading elimination of left factoring and recursion in grammar. I've solved such problems in the past but the grammar in this one looks really tricky and it is beyond my comprehension. I personally think there's left…
-2
votes
3 answers

Removing NullPointerException in Java

public class leftrec { static int isleft(String[] left,String[] right) { int f=0; for(int i=0;i
-2
votes
1 answer

recursive descend parser - avoiding left recursion

I have the following productions A -> Aa A -> b so it is clear that there is left recursion like parseA() { parseA();//recursion parsea(); } It is said that the left recursion can be avoided using the following rule A -> bA' A' ->…
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90
-4
votes
1 answer

Remove left recursion from grammar

Remove left recursion from following grammar : Q.1 S -> SXY | a X -> xY | xX Y -> Yy | epsilon Q.2 P -> P H 4 U | p H -> h U -> u | u P I know the rules to remove left recursion, but I am confused. So if someone please post the answer of this…
Meena
  • 1
1 2 3
11
12