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

Grammar restrictions on token look ahead

I know that there are two types of restriction on grammars that are used with recursive descent parsers. the grammar cannot have any left recursive productions the grammar must not require more than on token look ahead. I understand the first…
James Lockhart
  • 131
  • 1
  • 2
  • 11
3
votes
1 answer

Difference to remove left recursion for + and - or * or /?

To remove the left recursion E->E+T|E-T|T T->T*F|T/F|F for + and *, I am sure it should be E->TE' E'->+TE'|(e) (e) is empty string T->FT' T'->*FT'|(e) but for - or /, I am not sure how to remove left recursion, and I came up with the following…
David
  • 1,646
  • 17
  • 22
3
votes
2 answers

Choice Conflict Involving Two Expansions:

I'm trying to create my own analyser/parser. I have a problem which I understand why it doesn't work but I'm unsure of how to solve it. This is the code for the problem part of my parser. void Expression() : {}{ Term() (( | )…
Feint
  • 67
  • 7
3
votes
1 answer

How to transform a grammar into a top-down parsable grammar

I have this part of a grammar S ‐> S a | S b a | a | S b c S | S b c b | c S | c b and I need to use it in order to create some SD sets and later on a parse table. But, before doing that, I should convert this into a top-down parsable grammar. My…
3
votes
1 answer

ANTLR4 yet another left recursion

I'm very ashamed to ask... I wrote a grammar for the language with typecast from int to bool and vice versa. logic_expr : expr NOT? OR | AND expr | expr '|' expr SMALLER | LARGER | NUMBER | NUMBER_SHORT | IDENT |…
Diogen737
  • 33
  • 2
  • 5
3
votes
2 answers

Grammar to Regular Expression

Which is the procedure steps to find the regular expression that accept the same language of a given Grammar? S --> b | AA A --> aA | Abb | ϵ
3
votes
1 answer

Left Factoring & Removing Left Recursion JavaCC

I have a grammar which I have to use JJTree and JavaCC to create a symbol table and an AST. While I fully understand the sections of my assignment to create the table and tree, the grammar I was given is ambiguous, contains left recursion and…
3
votes
1 answer

Automatic grammar transformation for left-factoring; and left-recursion removal

Standard methods are readily available to transform a context-free grammar which is not LL(1) into an equivalent grammar which is. Are there any tools available which can automate this process? In the examples below I use upper-case lettering for…
user2023370
  • 10,488
  • 6
  • 50
  • 83
3
votes
3 answers

English constraint free grammar prolog

I ran into an infinite recursion problem while trying to implement a very simple constraint free grammar in prolog. Here are my rules: (vp -> verb phrase, np -> noun phrase, ap -> adj phrase, pp -> prep phrase) verb(S) :- member(S, [put, …
Khodeir
  • 465
  • 4
  • 15
3
votes
2 answers

DCG and left recursion

I am trying to implement a dcg that takes a set of strings of the form {a,b,c,d}*.The problem i have is if I have a query of the form s([a,c,b],[]),It returns true which is the right answer but when i have a query of the form s([a,c,f],[]),It does…
Jack welch
  • 1,707
  • 4
  • 25
  • 29
3
votes
1 answer

How can I avoid left-recursion in treetop without backtracking?

I am having trouble avoiding left-recursion in this simple expression parser I'm working on. Essentially, I want to parse the equation 'f x y' into two expressions 'f x' and '(f x) y' (with implicit parentheses). How can I do this while avoiding…
Josh Voigts
  • 4,114
  • 1
  • 18
  • 43
2
votes
0 answers

Prevent left recursion in ANTLR 4 from matching invalid inputs

I am making a simple programming language. It has the following grammar: program: declaration+; declaration: varDeclaration | statement ; varDeclaration: 'var' IDENTIFIER ('=' expression)?';'; statement: exprStmt |…
Foobar
  • 7,458
  • 16
  • 81
  • 161
2
votes
1 answer

Avoiding Left Recursion parsing Lambda Calculus while maintaining Left Associativity

I am trying to parse lambda calculus terms into AST leveraging JavaScript and PEG.JS. The grammar is fairly easy: /***************************************************************** t ::= x …
akaphenom
  • 6,728
  • 10
  • 59
  • 109
2
votes
1 answer

PLY : Parsing error. Rule unexpectedly matching empty set of tokens

So i'm trying to build a parser with PLY, but it doesn't work. It gives me errors that i can't seem to solve. I extracted a small part of the parser that i put in another file, for testing purpose : it doesn't work either. Here is the simplified…
Loïc N.
  • 353
  • 3
  • 17
2
votes
0 answers

Confused about how to remove left recursion from this grammar

I am new here and I have just started the compiler construction course and I am just a beginner. So my question is about removing left recursion from the following grammar: E → E + B | E - B | (E) | B E → B / F | B * F | (B) | F F → id I know the…
Prince
  • 21
  • 3
1 2
3
11 12