Is left recursion present in this grammar with the given productions?

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the presence of left recursion in given grammar productions, specifically examining the productions $S \to aSb$ and $S \to Sa|b$. Participants explore definitions of left recursion, dilemmas in grammar, and methods to eliminate left recursion.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants propose that the production $S \to aSb$ does not contain left recursion because $S$ is not the left-most symbol in its production.
  • Others question whether the production $S \to aSa|bSb|c$ presents a dilemma, discussing the need for the same left-most terminal symbol to determine ambiguity.
  • There is a suggestion that the production $S \to Sa|b$ contains left recursion and a query about how to eliminate it.
  • Participants discuss converting left recursion to right recursion as a method to create a regular grammar.
  • A proposed right recursive form for the production $S \to Sa|b$ is $S \to b|bS', S' \to aS'| \varnothing$.

Areas of Agreement / Disagreement

Participants generally agree on the definitions of left recursion and the implications for grammar structure, but there are differing views on specific productions and whether they contain left recursion or dilemmas. The discussion remains unresolved regarding the exact nature of these productions.

Contextual Notes

Some participants express uncertainty about the term "dilemma" in the context of grammar and its implications for parsing. There are also discussions about the conditions under which left recursion can be eliminated or transformed.

Who May Find This Useful

Readers interested in formal grammar, parsing techniques, and the properties of context-free grammars may find this discussion relevant.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

Does the following part of a grammar contains left recursive productions?
$$S \to aSb$$

A left recursive production is of the form $I \to IA|B$.
 
Technology news on Phys.org
mathmari said:
Hey! :o

Does the following part of a grammar contains left recursive productions?
$$S \to aSb$$

A left recursive production is of the form $I \to IA|B$.

Hey yourself! :p

From wiki:
In terms of context-free grammar, a non-terminal r is left-recursive if the left-most symbol in any of r’s productions (‘alternatives’) either immediately (direct/immediate left-recursive) or through some other non-terminal definitions (indirect/hidden left-recursive) rewrites to r again.


Is S either directly or indirectly the left-most symbol in its production?
 
I like Serena said:
Hey yourself! :p

From wiki:
In terms of context-free grammar, a non-terminal r is left-recursive if the left-most symbol in any of r’s productions (‘alternatives’) either immediately (direct/immediate left-recursive) or through some other non-terminal definitions (indirect/hidden left-recursive) rewrites to r again.


Is S either directly or indirectly the left-most symbol in its production?


Since at the left and at the right side of $S$ is a symbol, $S$ is not the left-most symbol in its production, so the production is not left recursive, is it?
 
mathmari said:
Since at the left and at the right side of $S$ is a symbol, $S$ is not the left-most symbol in its production, so the production is not left recursive, is it?

Yep.
 
Nice! :o

Could you also tell me if the production $S \to aSa|bSb|c$ has a dilemma?
We have dilemma when $I \to rK$ and $I \to rL$.

The production $S \to aSa|bSb|c$ means that $S \to aSa$, $S \to bSb$, $S \to c$, is this of the form above? At the first two cases there is the same symbol $S$ at the right side of the production. Or does it have to have only the same terminal symbols to have dilemma?
 
mathmari said:
Nice! :o

Could you also tell me if the production $S \to aSa|bSb|c$ has a dilemma?
We have dilemma when $I \to rK$ and $I \to rL$.

The production $S \to aSa|bSb|c$ means that $S \to aSa$, $S \to bSb$, $S \to c$, is this of the form above? At the first two cases there is the same symbol $S$ at the right side of the production. Or does it have to have only the same terminal symbols to have dilemma?

Well, I can't find "dilemma" with Google in this context.
But yeah, it would need to have the same left-most terminal symbol.

Typical reason why we would like to know, is to be able to uniquely decide, based on the next symbol on the input, which rule we should apply.
That makes for an unambiguous language with a one-token-look-ahead.
As a consequence, it's relatively easy to create a parser for it.
 
I like Serena said:
Well, I can't find "dilemma" with Google in this context.
But yeah, it would need to have the same left-most terminal symbol.

Typical reason why we would like to know, is to be able to uniquely decide, based on the next symbol on the input, which rule we should apply.
That makes for an unambiguous language with a one-token-look-ahead.
As a consequence, it's relatively easy to create a parser for it.

Ok! :o
And something else. The part $$S \to Sa|b$$ contains left recursive production, right?
How can I eliminate it?
 
mathmari said:
Ok! :o
And something else. The part $$S \to Sa|b$$ contains left recursive production, right?
How can I eliminate it?

Well, you can't usually eliminate a recursion.
But you can convert it to a right recursion, which is needed to turn a grammar into a regular grammar (that is easy to recognize by a deterministic state machine).

What kind of strings can your production rule make?
Can you rephrase it to be right recursive?
 
I like Serena said:
What kind of strings can your production rule make?
Can you rephrase it to be right recursive?

This production rule creates strings of the form $baa...aa$,so can we rephrase this rule to be right recursive as followed?
$$S \to b|bS', S' \to aS'| \varnothing $$
 
  • #10
mathmari said:
This production rule creates strings of the form $baa...aa$,so can we rephrase this rule to be right recursive as followed?
$$S \to b|bS', S' \to aS'| \varnothing $$

Yep!
Note that you can limit yourself to:
$$S \to bS', S' \to aS'| \varnothing $$
 
  • #11
I like Serena said:
Yep!
Note that you can limit yourself to:
$$S \to bS', S' \to aS'| \varnothing $$

Nice! Thanks a lot! :o
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 26 ·
Replies
26
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 29 ·
Replies
29
Views
6K
Replies
40
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K