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

  • MHB
  • Thread starter mathmari
  • Start date
In summary, the conversation discusses left recursive productions in a grammar and the criteria for determining if a production is left recursive. The conversation also touches upon the reasons for wanting to eliminate left recursion and how to rephrase a left recursive production to be right recursive.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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
  • #2
mathmari said:
Hey! :eek:

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?
 
  • #3
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?
 
  • #4
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.
 
  • #5
Nice! :eek:

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?
 
  • #6
mathmari said:
Nice! :eek:

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.
 
  • #7
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! :eek:
And something else. The part $$S \to Sa|b$$ contains left recursive production, right?
How can I eliminate it?
 
  • #8
mathmari said:
Ok! :eek:
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?
 
  • #9
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! :eek:
 

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

1. What is a left recursive production?

A left recursive production is a rule in a grammar that allows for the leftmost symbol on the right-hand side of the production to be the same as the leftmost symbol on the left-hand side of the production. This can cause issues in parsing and can lead to infinite loops.

2. Why is left recursion a problem in parsing?

Left recursion can cause issues in parsing because it can lead to infinite loops. When a parser encounters a left recursive production, it may continue to expand the same rule over and over again, never reaching the end of the input. This can result in a parsing error or an infinite loop.

3. How can we solve the problem of left recursion?

There are a few different ways to solve the problem of left recursion. One way is to rewrite the grammar to eliminate left recursion. Another option is to use a parsing algorithm that can handle left recursion, such as the LR or LL algorithm. Additionally, some parser generators have built-in tools for handling left recursion.

4. What are some common techniques for eliminating left recursion?

Some common techniques for eliminating left recursion include left factoring, which involves combining similar productions to eliminate left recursion, and left embedding, which involves moving the left recursive part of a production to the end. Another technique is to use a recursive descent parser, which is specifically designed to handle left recursion.

5. Can left recursion ever be beneficial?

While left recursion is generally seen as a problem in parsing, there are some cases where it can be beneficial. For example, in certain grammars, left recursion can result in a more efficient parsing process. However, in most cases, it is best to avoid left recursion to prevent issues with parsing.

Similar threads

Replies
1
Views
1K
Replies
4
Views
2K
Replies
26
Views
2K
Replies
3
Views
1K
Replies
1
Views
1K
Replies
11
Views
763
Replies
2
Views
4K
Replies
2
Views
1K
Replies
29
Views
5K
Back
Top