user2506227
user2506227

Reputation:

Left recursion without following string

I have two following defintions of wikipedia:

Left recursion

A grammar is left-recursive if and only if there exists a nonterminal symbol A that can derive to a sentential form with itself as the leftmost symbol. Symbolically, A ⇒ Aα where ⇒ indicates the operation of making one or more substitutions, and α is any sequence of terminal and nonterminal symbols.

and

Direct left recursion

Direct left recursion occurs when the definition can be satisfied with only one substitution. It requires a rule of the form A ⇒ Aα where α is a sequence of nonterminals and terminals,

https://en.wikipedia.org/wiki/Left_recursion

and an example:

S → AB
A → B | ab | abc
B → A | d | cd

It's not much about left recursion itself, but more about α. Is α allowed to be the empty word (as a terminal) or an empty string? In all examples I could found with indirect left recurson there was always an α like

A→Bab or B→Ab

But I'm not sure how to argue, if A → B and B → A (so without any α to follow). Is it still a left recursion by definition?

Upvotes: 1

Views: 314

Answers (1)

rici
rici

Reputation: 241861

α can be any sequence of terminals​ and non-terminals, including the empty sequence.

However, a production of the form A → A is pointless. Worse, it creates ambiguity because it could be applied any number of times in a derivation, so the gramar cannot be parsed deterministically. That is equally true if there is a circular indirect derivation (A ⇒ A in your notation). That is why you rarely find such cases in examples. But they are still certainly left-recursive. (They are also right-recursive.)

Unit productions (A → B where A and B are different) are quite common, and should not be problematic. Obviously they need to be taken into account while analysing the grammar. But I don't think that was your question.

Upvotes: 2

Related Questions