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

  • Thread starter Thread starter mathmari
  • Start date Start date
AI Thread Summary
The discussion centers on the identification and elimination of left recursive productions in context-free grammar. The initial query concerns whether the production S → aSb is left recursive, which is clarified to be non-left recursive since S is not the left-most symbol in its production. The conversation then shifts to the production S → aSa | bSb | c, where participants explore the concept of a "dilemma" in grammar, concluding that a dilemma occurs when multiple productions share the same left-most terminal symbol, which is necessary for unambiguous parsing.Further, the discussion addresses the production S → Sa | b, confirming it contains left recursion and discussing methods to eliminate it. Participants suggest converting it to right recursion, leading to a proposed rephrasing of the grammar that maintains the intended string structure while ensuring it can be recognized by a deterministic state machine. The final consensus emphasizes the importance of structuring grammar for effective parsing and clarity in language design.
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
Views
1K
Replies
4
Views
3K
Replies
26
Views
2K
Replies
11
Views
1K
Replies
2
Views
5K
Replies
2
Views
1K
Replies
29
Views
5K
Replies
7
Views
3K
Back
Top