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

Prolog how to eliminate left recursion

I have written the DCG (Adjective phrase and prepositional phrase) in prolog, when I tried to run it, by entering ip([every,boy,loved,some,girl]), it shows out of local stack. I realised there is something wrong with the nbar. Can someone help me…
2
votes
2 answers

Antlr4 left recursive rule contains a left recursive alternative which can be followed by the empty string

So I defined a grammar to parse an C style syntax language: grammar mygrammar; program : (declaration)* (statement)* EOF ; declaration : INT ID '=' expression ';' ; assignment : ID '=' expression ';' ; expression : expression (op=('*'|'/')…
RandomEli
  • 1,527
  • 5
  • 30
  • 53
2
votes
1 answer

Scala PackratParsers (parser combinators) and left-associativity

I'm using Scala's PackratParsers (parser combinators) with a left-recursive grammar of the following form lazy val expr: PackratParser[Expr] = ( ... | expr ~ (":" ~ expr).+ ^^ { case expr ~ rest => (expr /: rest)(combineBinary) } |…
Martin Studer
  • 2,213
  • 1
  • 18
  • 23
2
votes
1 answer

Leveraging ANTLR 4's left recursion disambiguation

I want a grammar and evaluator (ANTLR parse tree walker) that contains only binary non-terminals without the need to switch on the operator when visiting an expression node to determine what operation to take (as is the case for a pre-left factored…
Groostav
  • 3,170
  • 1
  • 23
  • 27
2
votes
1 answer

ANTLR4 Mutual left recursion

I just ran into a strange problem with ANTLR 4.2.2: Consider a (simplified) java grammar. This does not compile: classOrInterfaceType : (classOrInterfaceType) '.' Identifier | Identifier ; ANTLR outputs the following…
gexicide
  • 38,535
  • 21
  • 92
  • 152
2
votes
1 answer

Antlr remove left recursion while keeping math-expressions and bool-expressions separately

I'm getting this famous error rule has non-LL(*) decision due to recursive rule invocations for following simple grammar. expr : INTEGER | '(' expr '+' expr ')' ; bool_expr : '(' bool_expr 'and' bool_expr ')' …
Morpheus
  • 1,722
  • 5
  • 27
  • 38
2
votes
2 answers

why top down parser cannot handle left recursion?

I wanted to know why top down parsers cannot handle left recursion and we need to eliminate left recursion due to this as mentioned in dragon book..
2
votes
1 answer

Mutual Left Recursion ANTLR 4

I'm sorry to ask yet another question on mutual left recursion, I feel like mine is unique to my situation, or at least I can't figure out enough to relate it to everyone else's grammars. I'm a bit new to the comp sci world (I'm self taught in java,…
2
votes
2 answers

Left recursion detected in .jj file

Not sure what is causing this really. When I try to compile the file I get an error saying "Left recursion detected expression... -> fragment ... -> expression. The area of code that has this is this section void statement() : {} { identifier()…
Ayohaych
  • 5,099
  • 7
  • 29
  • 51
2
votes
1 answer

Antlr4 left-recursive rule appears to produce right-associative parse

The following grammar illustrates the issue: // test Antlr4 left recursion associativity grammar LRA; @parser::members { public static void main(String[] ignored) throws Exception{ final LRALexer lexer = new LRALexer(new…
2
votes
1 answer

antlr left recursion for nesting boolean expressions

I am writing an antlr grammar in which I'd like to be able to have nested expressions, which can be either "simple" expressions or boolean expressions (with optional parentheses). A simple expression is just one with an lhs and rhs, such as a =…
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
2
votes
0 answers

Need help eliminating left recursion from CFG

How would I eliminate the left recursion in this CFG? -> '|' | -> | -> '*' | -> 'a' | 'b' | 'c' | 'd' | '('')' I came up with this: ->…
2
votes
1 answer

ANTLR : Fixing Left Recursive and Mutually Left Recursive

This is part of a grammar I'm working on as to develop a parser tool which will be important in doing my research. It gives me an error under ANTLR IDE In eclipse saying paraction, action, cspaction are mutually left-recursive. I've scanned the web…
Shiyam
  • 71
  • 5
1
vote
3 answers

Prolog dcg generating all words from language

I'm trying to write some dcg grammar in prolog which will describe language of a^nb^n n>=0 "",ab,aabb,aaabbb itd All what I wrote is s --> slowo. slowo --> [a],slowo,[b],!. slowo --> []. And its good as long as all I want to do is just check if…
whd
  • 1,819
  • 1
  • 21
  • 52
1
vote
0 answers

Left recursive rules that do not conform to a pattern ANTLR can handle

VarsDecl.g4 describes the syntax of variable declarations, such as int a, b, c. grammar VarsDecl; decl : type vars ; type : 'int' # IntType | 'float' # FloatType ; vars : left = vars ',' ID # VarsList | ID …
hengxin
  • 1,867
  • 2
  • 21
  • 42