1
$\begingroup$

Please look at this grammar: $$𝑆 β†’ 𝑆𝐴𝐡 \ | \ 𝐴𝑏 \ | \ 𝑏 \\ 𝐴 β†’ 𝑆𝐡 \ | \ π‘Ž \ | \ 𝐡𝑆 \\ 𝐡 β†’ 𝐴𝑆 \ | \ d$$

I eliminated B from the grammar and it converted to:

$$ S β†’ SAAS \ | SAd \ | \ Ab | \ b \\ A β†’ SAS \ | \ Sd \ | \ a \ | ASS \ | dS $$

But I can't eliminate loop recursion between A and S.Was it a good solution I removed the B at first? How can I solve that?

any help is much appreciated.

$\endgroup$
1
$\begingroup$

$$𝑆 β†’ YX \ | \ Y \\ Y β†’ Ab \ | \ b \\ X β†’ ABX \ | \ AB \\ A β†’ SB \ | \ BS \ | \ a \\ 𝐡 β†’ 𝐴𝑆 \ | \ d$$

enter image description here

At first I say which rules have a left recursion, considering that the middle tree which is A(the one that A node is the root) has not called itself in its children from the beginning of the left of each child so there is no left recursion in tree which A is root.Similarly the right tree(the one that B node is the root) in its children has not called itself from the beginning of the left of each child, so they do not need to remove the left recursion, so They they have the previous rule (the fourth and fifth lines of my answer Aβ†’SB|BS|a,𝐡→𝐴𝑆|d ).

So our problem is the tree with the S root. I come and look at the children of the S root, there is only one child who starts with S from the beginning of the left of child, so I just have to correct one left recursion to right recursion,

Now I know that there are two children left of the tree with S root, since I only have a left recursion which I want to convert to the right recursion, So I have to put those two children on the left side of this conversion to the right! (For example, if we have two of these conversions we have to pack each of two children(with no recursion) with those two conversions.)

So I write the second line of my answer (Y→Ab|b) and then I write the third line of my answer (X→ABX|AB), which is actually the left-to-right conversion, The first line that these two children connect to each other, and the last two lines are the two trees in the middle and right.

$\endgroup$
2
  • 1
    $\begingroup$ Can you explain how you eliminated the left recursion? Your figure is hard to interpret. $\endgroup$ – Yuval Filmus May 3 at 20:49
  • $\begingroup$ @YuvalFilmus Sorry I was late for your feedback, I edited the path I took β€Œ I hope it's right but it may not be right and I am sorry about that, let me know if I made a mistake or I was not clear. $\endgroup$ – ryhn May 4 at 1:48

Your Answer

By clicking β€œPost Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.