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
0 answers

LPEG - "Rule may be left-recursive" error, despite being a terminable grammar

I'm trying to use LPEG to build a preprocessor for GLSL. I've managed to get #define and #undef statements working no problem, but my issues comes when trying to work with #ifdef statements. My thought was that I could build a rule that would…
Hoeloe
  • 650
  • 4
  • 21
1
vote
2 answers

Can this grammar be parsed by both predictive recursive descent parser and the parser with backtracking

I have the following grammar: IdentifierName :: IdentifierStart IdentifierName IdentifierPart Which using the word git should be parsed into the following parse tree: IdentifierName / \ …
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
1
vote
0 answers

antlr4: actions 'causing' left-recursion errors

The grammar below is failing to generate a parser with this error: error(119): Sable.g4::: The following sets of rules are mutually left-recursive [expression] 1 error(s) The error disappears when I comment out the embedded actions, but comes back…
1
vote
1 answer

Eliminating Left Recursion from this weird Expression Grammar

I am trying to write an Expression Grammar where there are 3 operators '+','-' and '/'. The multiplication operator is implied by juxtaposition as in: (1+2)(3+4 5) Here is the Grammar: S -> A ('+' A)* A -> B ('-' B)* B -> C ('/' C)* C -> D ( D…
1
vote
1 answer

ANTLR4 - Eliminate indirect mutually left-recursive set of rules

I'm writing a grammar for the language LUA using Antlr sintaxe, but I'm getting a mutual left recursion error between exp_prefixo, variavel and chamada_de_funcao. I read a lot of solutions given in other posts, but couldn't make it work for my…
1
vote
1 answer

Left recursion without following string

I have two following defintions of wikipedia: Left recursion A grammar is left-recursive if and only if there exists a nonterminal symbol A that can derive to a sentential form with itself as the leftmost symbol. Symbolically, A ⇒ Aα where ⇒…
user2506227
1
vote
0 answers

Resolving ambiguities in an expression using LL(1) grammar

I need to construct a parser for an expression which is allowed to have only 2 operators (AND & OR) and such that both operators cannot be presenet in a enclosing paranthesis. Eg1: (A AND B OR C) --> invalid (A AND (B OR C)) --> valid Eg2: (A AND…
DhiwaTdG
  • 748
  • 1
  • 10
  • 26
1
vote
1 answer

Resolving left-recursion in my grammar

My grammar has a case of left-recursion in the sixth production rule. I resolved this by replacing Rule 6 and 7 like this: I couldn't find any indirect left recursions in this grammar. The only thing that bothers me is the final production rule,…
1
vote
1 answer

Remove left recursive call graph

I am currently working on an Xtext grammar and got some issues with left recursive graphs. I already eliminated all direct left recursions in my grammar but now I have some indirect left recursions, which are shown in the IDE with the message This…
boindiil
  • 5,805
  • 1
  • 28
  • 31
1
vote
1 answer

Stack overflow when using parser combinators

import scala.util.parsing.combinator._ object ExprParser extends JavaTokenParsers { lazy val name: Parser[_] = "a" ~ rep("a" | "1") | function_call lazy val function_call = name ~ "(" ~> name <~ ")" } recurs indefinitely for…
1
vote
0 answers

ANTLR4-Mutually left-recursive grammar

i have the following antlr4 grammar and i get mutually left-recursive error , how can i fix it ? expr : expr_prefix term ; expr_prefix : expr_prefix term addop | () ; term : factor_prefix factor; factor_prefix : factor_prefix factor…
1
vote
1 answer

How to resolve left recursions in PEG

The problem is, that PEGs (parsing expression grammars) do not allow left-recursive rules. I have read the available answers on this topic, however problem specific (like this one) or quite simple (e.g. x = symbol:(x '.')). I've created the…
Dänu
  • 5,791
  • 9
  • 43
  • 56
1
vote
1 answer

Does Chomsky Normal Form have left recursion?

one of famous form of CFG is CNF and as you know its have two non terminal as its RHS or one terminal as its RHS and empty RHS,if exists, appear only in RHS of root as described in this Wiki , but I'm not sure does CNF allow us to have left…
1
vote
2 answers

How to solve this Grammar through SLR?

I want to solve this Grammar. S->SS+ S->SS* S->a I want to construct SLR sets of items and parsing table with action and goto. Can this grammar parse without eliminate left recursion. Is this Grammar SLR.
1
vote
0 answers

Removing Left Direct and Indirect Recursion

S->(L)|a L->L,S|S //Step 1 i=1,j=1 //do nothing //Step 2 //Substituting S in L production by productions of S i=2,j=1 to 1 S->(L)|a L->L,(L)|(L)|L,a|a //removing left recursion S->(L)|a …