Is this rule a left recursive production?

  • MHB
  • Thread starter mathmari
  • Start date
In summary, the conversation discusses the concept of left-recursive productions and how they affect the determinism of a grammar. It also explores different methods for making a grammar deterministic, including avoiding dilemmas and left recursion. The conversation ends with a discussion on whether a specific grammar, $X \to aT, T \to XT|\varnothing$, is deterministic or not.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

I have a question..
Is the rule $$X \to XX|a$$ a left recursive production?
 
Physics news on Phys.org
  • #2
Not a specialist in this, but since $X$ generates a sentential form $XX$ with $X$ as the leftmost symbol, yes, $X$ is left-recursive. I am looking at Wikipedia, which has definitions for a left-recursive nonterminal and a left-recursive grammar, but not a left-recursive production.
 
  • #3
Evgeny.Makarov said:
Not a specialist in this, but since $X$ generates a sentential form $XX$ with $X$ as the leftmost symbol, yes, $X$ is left-recursive. I am looking at Wikipedia, which has definitions for a left-recursive nonterminal and a left-recursive grammar, but not a left-recursive production.

To make the grammar deterministic do I have to do the following changes?
$$ X \to aT ,T\to XT|\varnothing$$
 
Last edited by a moderator:
  • #4
Or is there an other way to make the grammar deterministic?
 
  • #5
mathmari said:
To make the grammar deterministic do I have to do the following changes?
$$ X \to aT ,T\to XT|\varnothing$$

mathmari said:
Or is there an other way to make the grammar deterministic?

I have a 3 step plan! ;)

1. What does a deterministic grammar look like?
2. Can you give a regular expression that describes the language?
3. Can you write that regular expression as a deterministic grammar?
 
  • #6
I like Serena said:
1. What does a deterministic grammar look like?

A grammar is not determinitic if we have at least one of these two factors:
1. <<dilemma>>: $Q \to kA$ and $Q \to kB$
2. <<left recursion>>: $Q \to QY|X$

So to construct the determinitic version of the grammar we do the following:
For 1. $Q \to kX$ and $X \to A|B$
For 2. $Q \to X|XT, T \to YT| \varnothing$So, for my case, is $ X \to aT, T \to XT| \varnothing$ correct?
 
  • #7
mathmari said:
A grammar is not determinitic if we have at least one of these two factors:
1. <<dilemma>>: $Q \to kA$ and $Q \to kB$
2. <<left recursion>>: $Q \to QY|X$

This states when the grammar is guaranteed to be not deterministic.
The negation does not necessarily make it deterministic.

Suppose for instance we have a rule like $Q \to aQb$.
That is not a dilemma, nor a left recursion.
Does that make it deterministic?

Perhaps you can find a form that is guaranteed to be deterministic?
So to construct the determinitic version of the grammar we do the following:
For 1. $Q \to kX$ and $X \to A|B$
For 2. $Q \to X|XT, T \to YT| \varnothing$

So, for my case, is $ X \to aT, T \to XT| \varnothing$ correct?
 
  • #8
I like Serena said:
This states when the grammar is guaranteed to be not deterministic.
The negation does not necessarily make it deterministic.

Suppose for instance we have a rule like $Q \to aQb$.
That is not a dilemma, nor a left recursion.
Does that make it deterministic?

Perhaps you can find a form that is guaranteed to be deterministic?

Besides not having a dilemma nor a left recursion, what else should be satisfied so that the grammar is deterministic?

Does the part of the grammar $X \to aT, T \to XT| \varnothing$, have something that makes the grammar non deterministic?
 
  • #9
mathmari said:
Besides not having a dilemma nor a left recursion, what else should be satisfied so that the grammar is deterministic?

Does the part of the grammar $X \to aT, T \to XT| \varnothing$, have something that makes the grammar non deterministic?

It is at least not obviously deterministic.

As it is we can substitute the first rule in the second and get:
$$T \to aTT$$
Now without knowing what else $T$ might map to, this does not look good.
It suggests that we need to keep track of the first $T$ to be able to verify if the second $T$ "fits", which is what a deterministic language will not allow.
 

FAQ: Is this rule a left recursive production?

What is a left recursive production?

A left recursive production is a production rule in a grammar that has a non-terminal symbol as its first element and that same symbol appears again as the first element on the right side of the rule. This means that the rule can be applied repeatedly without ever reaching a terminal symbol, resulting in an infinite loop.

How can I identify if a rule is left recursive?

To identify a left recursive production, you can check if the first symbol on the right side of the rule is the same as the non-terminal symbol on the left side. If it is, then the rule is left recursive.

Why is left recursion a problem in grammars?

Left recursion can be a problem in grammars because it can lead to infinite loops and cause the parser to never terminate. This can result in incorrect or incomplete parsing of the input.

How can I eliminate left recursion from a grammar?

To eliminate left recursion, you can use a technique called left factoring, where you create new production rules to replace the left recursive rule. Another approach is to use right recursion instead of left recursion, where the recursive symbol appears on the right side of the rule.

Can a grammar have both left and right recursive productions?

Yes, a grammar can have both left and right recursive productions. However, it is important to note that left recursion can cause problems in parsing, while right recursion is typically easier to handle for parsers. Therefore, it is generally recommended to eliminate left recursion if possible.

Similar threads

Replies
2
Views
3K
Replies
16
Views
3K
Replies
5
Views
3K
Replies
3
Views
2K
Replies
18
Views
3K
Replies
5
Views
2K
Replies
13
Views
4K
Replies
11
Views
4K
Back
Top