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

How to remove left recursion in he following?

Here is the productions. A-> Aa| b|c; Now shall i do A->bA' A'-> aA' | e (empty transition) A->c Will it be the right answer? That is either 'b' or 'c' can be used?
Kraken
  • 23,393
  • 37
  • 102
  • 162
0
votes
1 answer

Turn grammar into LL1 Grammar

I am revising for an exam tomorrow, and going over a previous years one. In the test was the grammar. Expression -> Foo "+" Bar "end" Foo -> [a-z0-9]+ | Expression Bar -> Expression Foo | a*b*c+ I have tried and spent hours researching on how to do…
James
  • 97
  • 5
0
votes
1 answer

Converting the regular expression to a grammar

I have the following regular expression: (12)*[34]+(5[67])* How can I convert the expression to a grammar that is left recursive? I came up with this. I did not follow any methods or anything... G --> GAB34C A --> A12 | epsilon B --> B34 |…
kiwi kiwi
  • 21
  • 1
0
votes
1 answer

How to remove left recursion in a grammar with both left recursion and right recursion existing?

The grammar is as follows: E->a E->E+E E->S,E E->(E) S-> bS' S'-> ;bS' S'-> I have no idea how to remove the left recursion cause E contain terminals and non-terminals. And there're both left recursion and right recursion.
0
votes
0 answers

ANTLR4 grammar for String, Boolean, and Numeric expressions that are not mutually left-recursive

I'm trying to create a grammar for expression evaluation that differentiates between String, Boolean, and Numeric expressions. Here's the relevant grammar so far: functionInvocation: IDENTIFIER '(' ( IDENTIFIER '=' expression ( ',' IDENTIFIER '='…
John Arrowwood
  • 2,370
  • 2
  • 21
  • 32
0
votes
0 answers

What is the solution of this grammar after left recursion elimination?

Given the following grammar: 1. S -> Sa 2. S -> bS 3. S -> c As we see that this grammar is affected by left recursion and want to eliminate it from the grammar. so now want to know which solution should be correct after elimination. Solution 1: 1.…
0
votes
1 answer

Antlr Indirect Left Recursion

I've seen this question asked multiple times, and also seen people "solve" it... but it either confused me or didn't solve my specific situation: Here's approximately what's going on: block: statement*; statement: |…
1Mangomaster1
  • 346
  • 1
  • 6
  • 16
0
votes
2 answers

How to remove left recursion from a grammar with beta missing?

Here's the problem: A -> A*B | A+CDE | Aab All of the productions start with A. I guess that satisfies the rule? As you can see, it is missing beta. How do I perform left recursion on it? Can left recursion be even performed on it? What I have…
Sakib Arifin
  • 255
  • 2
  • 13
0
votes
0 answers

Parsing a custom boolean logical expression in java

I'm trying to create a logical boolean expression parser that evaluates if the words in the expression correlate to the content of the document. After many many hours of research (I had no idea about parsers and all their theory before this), I find…
Jafeth
  • 3
  • 4
0
votes
2 answers

Understanding what makes a rule left-recursive in antlr

I've been trial-and-erroring to figure out from an intuitive level when a rule in antlr is left-recursive of not. For example, this (Removing left recursion) is left-recursive in theory, but works in Antlr: // Example input: x+y+1 grammar…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
0
votes
0 answers

How to correctly transform the following grammar to fall into LL(1)

Recently I'm trying to write a compiler for a modified subset of OCaml, through the official documentation I've written a similar grammar given below as what they do to the expressions:(since its a slightly modified version so there will be some…
0
votes
0 answers

Xtext left recursion issue in an expression language

I've been recently working on a small demo expression grammar in Xtext: The Grammar grammar org.example.expressions.Expressions with org.eclipse.xtext.common.Terminals generate expressions…
Tomer Vax
  • 11
  • 4
0
votes
0 answers

How can I manage parenthesis for logical expressions using ANTLR4 left recursion?

I am trying to build a complex ANTLR4 grammar for a language where one of its parts consists of a set of logical expressions. This is the part of my grammar which defines such expressions: expression: '(' expression ')' …
pabpazjim
  • 13
  • 2
0
votes
1 answer

Error about Immediate left recursion in ANTLR4

I write follow grammar in a .g4 file: expr :function=expr'('((arg',')*arg)?')' #callFun |object=expr'.'SYMBOL #objCall |coll=expr'['arg']' …
0
votes
1 answer

Converting a context free grammar into a regular grammar

E -> EAE | (E) | -E | id A -> + | - | * | / The terminal set is {id, -,+,*,/} and the starting symbol is E. I want to convert this grammar to a regular grammar. I tried canceling the left recursion of this grammar and I got: E -> (E)X | -EX |…
Randy
  • 3
  • 2