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

How to handle left recursion in FParsec

My languages uses s-expressions with one additional feature - a dot operator for accessing elements from an array or struct. Currently, my parser works on this code using the access operator - ; Parition a sequence into a pair of sequences. ; NOTE:…
Bryan Edds
  • 1,696
  • 12
  • 28
4
votes
1 answer

Left-factoring grammar of coffeescript expressions

I'm writing an Antlr/Xtext parser for coffeescript grammar. It's at the beginning yet, I just moved a subset of the original grammar, and I am stuck with expressions. It's the dreaded "rule expression has non-LL(*) decision" error. I found some…
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
4
votes
3 answers

Why does haskell stop when trying to add a string to the end of this string?

I am trying to add a string to the end of a string in Haskell. albumStr = "" main = do let albumStr = albumStr ++ "nothing" print albumStr Whenever i run this it just gets stuck on " in the console and i have to terminate it. Why? and how do…
Dean
  • 80
  • 7
4
votes
2 answers

Parsing left-recursive grammar in sum-type inifinite recursion

I'm trying to write a parser for the Tiger language from modern compiler implementation in ML, and am stuck on one of the recursive types. I have the following type data LValue = …
Stuart
  • 644
  • 1
  • 6
  • 13
4
votes
2 answers

Prolog DCG: Match different symbols on a chain

I'm trying to match some sentences (e.g. 001 [0,0,1], (1+(1/0)) ['(',1,+,'(',1,/,0,')',')'], and so on. I've made myself following small DCG. g3 --> s3. s3 --> e3. e3 --> eAdd. e3 --> eMin. e3 --> eMul. e3 --> eDiv. e3 --> n3. eAdd -->…
user452306
  • 139
  • 4
  • 9
4
votes
1 answer

Translate grammar production to Parsec

I'm trying to convert the following grammar production callExpr: primaryExpr | callExpr primaryExpr to a Parsec expression in Haskell. Obviously the problem is that it's left-recursive, so I'm trying to parse it recursive-ascent style. The…
4
votes
1 answer

Parse function call with PyParsing

I'm trying to parse a simple language. The trouble comes with parsing function calls. I'm trying to tell it that a function call is an expression followed by left parenthesis, argument list, and right parenthesis. I have something like this: expr =…
kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
4
votes
1 answer

Left recursive ANTLR grammar

I have written a grammar but getting a left recursive error. grammar Lang; options { output = AST; language = C; ASTLabelType= pANTLR3_BASE_TREE; backtrack = true; } start : primary_expression+ ; primary_expression …
4
votes
1 answer

Removing Left Recursion from CFG

The following grammar has left recursion: T -> Tx | TYx | YX | x X -> xx Y -> Yy | Yx | y How do you go about removing left recursion. I read the wikipedia explanation, but I'm fairly new to CFGs so it did not make a lot of sense. Any help is…
3
votes
5 answers

Grammar involving braces

I'm trying to solve DCG grammar in prolog and succeeded upto a point, i'm stuck in evaluating the expressions involving braces like these. expr( T, [’(’, 5, +, 4, ’)’, *, 7], []), expr(Z) --> num(Z). expr(Z) --> num(X), [+], expr(Y), {Z is…
Abhilash Muthuraj
  • 2,008
  • 9
  • 34
  • 48
3
votes
2 answers

Practical solution to fix a Grammar Problem

We have little snippets of vb6 code (the only use a subset of features) that gets wirtten by non-programmers. These are called rules. For the people writing these they are hard to debug so somebody wrote a kind of add hoc parser to be able to…
nickik
  • 5,809
  • 2
  • 29
  • 35
3
votes
1 answer

How to avoid left-recursion infinite loops in Fastparse?

I had a parser that worked well in Scala Packrat parser combinators. I would like to try something faster with the Fastparse library. However, it cannot handle left-recursion infinite loops. Is there any standard way to cope with that? sealed trait…
dawid
  • 663
  • 6
  • 12
3
votes
1 answer

Recursive definitions with scala-parser-combinators

I have been trying to build a SQL parser with the scala-parser-combinator library, which I've simplified greatly into the code below. class Expression case class FalseExpr() extends Expression case class TrueExpr() extends Expression case class…
3
votes
1 answer

Removing Left Recursion with Terminals

How do I remove left recursion on the following rule: S -> aSAbb | aA I understand how to perform it on S -> SA | A which becomes S -> A | AS'; S' -> A | AS', but the terminals throw me off in this question. EDIT: Sorry, apparently I was confused as…
A B
  • 31
  • 3
3
votes
2 answers

Removing Left Recursion in a Context Free Grammar

Trying to figure out removing left recursion in context free grammars. I'm used to certain forms, but this one has me a bit boggled. S --> S {S} S | (A) | a A --> {S} A | epsilon I also have to design a decent parser, which I can do. However,…
Mercfh
  • 31
  • 1
  • 2
1
2
3
11 12