0

I'm having a minor problem in trying to figure out how to resolve a conflict in my CUP parser project. I understand why the error is occurring, VariableDeclStar's first terminal can be ID, as well as Type, which brings up the conflict, however I cannot figure out how to resolve the conflict in a way that would preserve Type and Variable as separate states. Any help or tips would be appreciated.

VariableDecl    ::= Variable SEMICOLON                  {::};
Variable        ::= Type ID                             {::};
Type            ::= INT                                 {::}
                    | DOUBLE                            {::}
                    | BOOLEAN                           {::}
                    | STRING                            {::}
                    | Type LEFTBRACKET RIGHTBRACKET     {::}
                    | ID                                {::};
VariableDeclStar::= VariableDecl VariableDeclStar       {::}
                    |                                   {::};

https://i.gyazo.com/0ac3fbf4ebc2d3968f1c2a78c292bc0d.png

Hacker Zen
  • 11
  • 3
  • That grammar works just fine as presented (using VariableDeclStar as the start symbol.) But it only has 15 states, and you image (which should have been pasted text) says the problem is in state 63. So I'm assuming that you are not presenting your entire grammar and that the S/R conflict is induced by some other grammar rule(s). Please provide a [mcve] with an emphasis on **complete** (and to the extent possible *minimal*). In the process of producing that, you may well solve your problem yourself, but otherwise we'll take another look. – rici Mar 08 '18 at 01:15
  • Thank you for the reply, also I apologize for the link, my initial thought was to include it as an embedded image, but as I do not have sufficient status I automatically resorted to linking it. As for the problem, I do not know the boundaries of the conflict, would my entire grammar be acceptable? It is 6 lines short of 100. I do not expect a complete solution to the grammar, I just want to understand the methodology to solving these more thoroughly. – Hacker Zen Mar 08 '18 at 03:48
  • Usually this particular S/R conflict results from something like `Block ::= VariableDeclStar StatementStar`, where a statement can also start with an ID. Bottom-up parsers can deal with two productions starting with the same token; that is a prediction conflict, and it only affects top-down (LL) parsers. There are a number of SO answers about this problem. You should try to find a minimal subset of your grammar which can be processed as a complete grammar, and which exhibits the conflict. In the process of adding rules to your current extract, you will likely see the problem yourself. – rici Mar 08 '18 at 05:40
  • Here's a possibly relevant answer: https://stackoverflow.com/questions/28665795/shift-reduce-conflict-on-c-variation-grammar/28666129#28666129. Or https://stackoverflow.com/questions/43579367/shift-reduce-conflict/43581713#43581713. Or perhaps https://stackoverflow.com/questions/25185601/shift-reduce-error-with-cup/25188236#25188236 – rici Mar 08 '18 at 05:49

0 Answers0