0

I am currently having the following grammar which I am asked if it is SLR(1) or not. The grammar is:

E -> E + A + A | E - A + A | E + A - A | E - A - A | T .
T -> T + A | T - A | A .
A -> A * B | A / B | B .
B -> ( E ) | x .

The grammar is ambigius and with the help of the software of grammophone in github ( http://mdaines.github.io/grammophone/#/ just copy paste the grammar into the edit section) i am locating a conflict of shift/reduce in the line 2 of the board when + and - are coming. Now the next question is to fix the grammar so that it becomes SLR(1). How can I do that ?? I am searching the net and I cannot find the answer. Sorry for my bad english.

Edit:

The grammar was at first like

E->E+E+E|E-E-E|E+E-E|E-E+E
E->E+E|E-E|E*E|E/E
E->(E)|x

and the exercise tells you that * and / has equally priority but higher than + and - where + and - has equal priority. Given those you have to change the given grammar into another one which is SLR(1). All I could come up with was the very first one. The production comes from left to right and we cannot remove any rules.

Kostas Ayfantop
  • 53
  • 1
  • 1
  • 10
  • What are you trying to accomplish with the rules in the first line? Just getting rid of them would solve the problem. – rici Nov 23 '17 at 13:55
  • So you say removing the first 4 rules with the 3 terms ( E-> E+A+A etc) can actually solve the problem AND the new one will be equal to the first one (grammars )? – Kostas Ayfantop Nov 23 '17 at 14:45
  • That depends on how you define grammar equality. It will recognize the same sentences, but not necessarily with the same parse tree. It's clear that your grammar is ambiguous because you can produce `E -> E + A + A -> T + A + A` and `E -> T -> T + A -> T + A + A`. So if you cannot delete the productions (because the first parse is required) then you need to figure out how to avoid producing the second one. – rici Nov 23 '17 at 15:02
  • Yeah I agree its surely ambiguous , but as I just saw it won't create the same productions , because the new one won't create the x+x+x string as the first will do. When 2 grammars are equal mean they can produce the same amount of strings or recognize the same ones. – Kostas Ayfantop Nov 23 '17 at 15:08
  • Sure it will produce `x+x+x`: `E -> T -> T + A -> T + A + A -> A + A + A -> x + A + A -> x + x + A -> x + x + x`. The ternary productions do not change the strings generated; all they do is allow a different interpretation of ternary operations. – rici Nov 23 '17 at 15:56
  • I see that I left out the `A -> B` unit derivations above but the obviously don't make a substantial difference. – rici Nov 23 '17 at 16:08
  • You are right , my bad , it surely produce that. – Kostas Ayfantop Nov 23 '17 at 16:12
  • Iirc, there is a discussion of a similar (but simpler) grammar n the Dragon book. Look for ambiguities from special case productions; the specific example is a layout language with subscripts and superscripts, in which `A sub B sup N` produces an A with both a subscript and a superscript; that is not the same as either possible combination of binary operations. – rici Nov 23 '17 at 19:13

0 Answers0