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

Elimination of left recursion

I am trying to remove left recursion from following grammar: S -> id = E S -> id [ E ] = E E -> E [ E ] E -> id I tried to follow the left recursion removal algorithm that is presented at https://en.wikipedia.org/wiki/Left_recursion, but the E -> E…
atsa
  • 13
  • 5
1
vote
1 answer

Mutually left-recursive?

I'm working on a parser for a grammar in ANTLR. I'm currently working on expressions where () has the highest order precedence, then Unary Minus, etc. When I add the line ANTLR gives the error: The following sets of rules are mutually left-recursive…
1
vote
0 answers

Trying to resolve left-recursion trying to build Parser with ANTLR

I’m currently trying to build a parser for the language Oberon using Antlr and Ecplise. This is what I have got so far: grammar oberon; options { language = Java; //backtrack = true; output = AST; } @parser::header {package…
Mickey
  • 943
  • 1
  • 19
  • 41
1
vote
1 answer

Left recursion elimination issue

So I have this left recursive grammar E → E Op1 E2 | E2 As it stand, it is left recursion, so I eliminated the left recursion by putting in another step: E → X E2 X → E Op1 E2 | ε I have a sinking feeling however that I eliminated it wrongly,…
Doh
  • 113
  • 1
  • 2
  • 6
1
vote
1 answer

Intellij Antlr4 Plugin Left direct recursion doesn't work

I'm trying to make parser using Antlr4 for the sql select statement, in which contains the following part expr: '1' | expr('*'|'/'|'+'|'-'|'||') expr; // As the re-factored form of expression: compound expression; WS :[ \t\r\n]+ -> skip ; I…
1
vote
2 answers

Eliminate left recursion in XText

This is part of the grammar of the NuSMV language: BasicExpression: Constant | '(' BasicExpression ')' | '!' BasicExpression | BasicExpression '&' BasicExpression; Constant: BooleanConstant BooleanConstant: 'TRUE' | 'FALSE'; Unfortunately XText is…
Shevach
  • 717
  • 9
  • 25
1
vote
1 answer

Eliminating Left Recursion

So I have some grammar that doesn't work for a top-down parser due to it having left recursion: L::= A a | B b A::= L b | a a B::= b B b | b a So in order to fix this, I have to remove the left recursion. To do this, I do a little…
user2869231
  • 1,431
  • 5
  • 24
  • 53
1
vote
1 answer

Grammars - Left recursion

I've a problem in understanding left recursion. I know that this is a left recursion: A->Aa Can you tell me if this is left recursion? A->aA And can you explain me why this is an indirect left recursion? D ---> dcD' | SD' D' --> DbaD' | DcD' |…
testermaster
  • 1,031
  • 6
  • 21
  • 40
1
vote
1 answer

How to remove indirect left-recursion from grammar

I have some mutually left-recursive ANTLR code: expr: int_liter | bool_liter | char_liter | str_liter | pair_liter | ident | array_elem | unary_oper expr | expr binary_oper expr | OPEN_PARENTHESES expr CLOSE_PARENTHESES ; array_elem: expr …
1
vote
1 answer

What exactly makes grammar rules left-recursive in antlr?

So, I was wondering what makes a parser like: line : expression EOF; expression : m_expression (PLUS m_expression)?; m_expression: basic (TIMES basic)?; basic : NUMBER | VARIABLE | (OPENING expression CLOSING) |…
Sanuuu
  • 243
  • 1
  • 10
1
vote
1 answer

Why there isn't a option for left-recursion removal of a grammar in Antlr?

Several times I've encountered left recursion while I'm in the process of writing a grammar in Antlr that's why I asked myself why there isn't a automatic tool which removes it. After I did some research I found two methods which deal with this…
sve
  • 4,336
  • 1
  • 19
  • 30
1
vote
1 answer

Cannot use any {} statement in a left-recursive alternative

I am using the Java target. I have the following simple grammar: grammmar example; alpha : alpha 'something' | 'otherthing' ; Now I want my parser to print Hello World for something and Hello World for otherthing…
Rishabh Garg
  • 296
  • 3
  • 12
1
vote
2 answers

Removing left recursion

I am feeling quite nostalgic so I've decided to write an adventure game creator that allows complex sentences to be entered by the user. I have hand rolled a lexer and parser that uses the visitor pattern and it works quite well, until I encountered…
Intrepid
  • 2,781
  • 2
  • 29
  • 54
1
vote
2 answers

Problems with LL(1) grammar

I have a 26 rule grammar for a sub-grammar of Mini Java. This grammar is supposed to be non-object-oriented. Anyway, I've been trying to left-factor it and remove left-recursion. However I test it with JFLAP, though, it tells me it is not LL(1). I…
Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
0
votes
1 answer

Change DCG to be deterministic

how to change this gramma to be deterministic e --> []. e --> "*". e --> s_e. e --> e, s_e. s_e --> ("a",e);("b",e). I just dont know where to put cut to avoid backtracking.
whd
  • 1,819
  • 1
  • 21
  • 52