0

I am continuously getting shift/reduce conflicts when by parser tries to sort out whether something is a unary or binary operator.

%token <intconst> tHEX tOCT tDEC tRUNE
%token <stringconst> tBOOL INTERPRETEDSTRING RAWSTRING tIDENTIFIER
%token <floatconst> tFLOAT
%token <charconst> tRUNES
%token TRUE FALSE BREAK CASE CHAN CONST CONTINUE DEFAULT DEFER ELSE FALLTHROUGH FOR FUNC GO GOTO IF IMPORT INTERFACE MAP PACKAGE RANGE RETURN SELECT STRUCT SWITCH TYPE VAR INT PRINT FLOAT PRINTLN BOOL APPEND RUNE STRING SEMICOLON NEWLINE PLUS MINUS TIMES DIV MOD AMP PIPE CARAT COUT CIN AMPCARAT SELFPLUS SELFMINUS SELFTIMES SELFDIV SELFMOD AMPEQUALS PIPEEQUALS CARATEQUALS COUTEQUALS CINEQUALS WTF AND OR REDIRECT INCREMENT DECREMENT DOESEQUALS LT GT EQUALS NOT NEQ LE GE COMPAT ELLIPSIS LEFTPAREN RIGHTPAREN LEFTSQUARE RIGHTSQUARE LEFTBRACE RIGHTBRACE COMMA PERIOD FULLCOLON ESCAPEA ESCAPEB ESCAPEF ESCAPEV ESCAPESLASH ESCAPEAPOSTROPHE INVALID
/*%token unary*/
/*%token binary*/
%left OR
%left AND
%left DOESEQUALS NEQ GT GE LT LE
%left PLUS MINUS PIPE CARAT
%left TIMES DIV MOD COUT CIN AMP AMPCARAT
/*%left binary*/
%left UPLUS UMINUS UNOT UCARAT UTIMES UAMP UPAREN
%start expList

%%

expList: exp expList {}
       | /*empty*/
;
exp: exp OR addOp {}
   | exp AND addOp {}
   | exp NEQ addOp {}
   | exp GT addOp {}
   | exp GE addOp {}
   | exp LT addOp {}
   | exp LE addOp {}
   | addOp {}
;
addOp: addOp PLUS mulOp {}
     | addOp MINUS mulOp {}
     | addOp PIPE mulOp {}
     | addOp CARAT mulOp {}
     | mulOp {}
;  
mulOp: mulOp TIMES factor {}
     | mulOp DIV factor {}
     | mulOp MOD factor {}
     | mulOp COUT factor {}
     | mulOp CIN factor {}
     | mulOp AMP factor {}
     | mulOp AMPCARAT factor {}
     | factor {}
;
factor: LEFTPAREN exp RIGHTPAREN %prec UPAREN {}
      | PLUS factor %prec UPLUS {}
      | MINUS factor %prec UMINUS {}
      | NOT factor %prec UNOT {}
      | CARAT factor %prec UCARAT {}
      | TIMES factor %prec UTIMES {}
      | AMP factor %prec UAMP {}
      | tIDENTIFIER {}
      | tDEC {}
      | tFLOAT {}
      | tOCT {}
      | tHEX {}
      | tRUNES {}
      | INTERPRETEDSTRING {}
      | RAWSTRING {}
;
%%

I know I have a lot of tokens and I will eventually use them. I'm just trying to get the grammar for the expressions working. Here are the shift/reduce errors I'm getting

State 18

   10 exp: addOp .
   11 addOp: addOp . PLUS mulOp
   12      | addOp . MINUS mulOp
   13      | addOp . PIPE mulOp
   14      | addOp . CARAT mulOp

    PLUS   shift, and go to state 37
    MINUS  shift, and go to state 38
    PIPE   shift, and go to state 39
    CARAT  shift, and go to state 40

    PLUS      [reduce using rule 10 (exp)]
    MINUS     [reduce using rule 10 (exp)]
    CARAT     [reduce using rule 10 (exp)]
    $default  reduce using rule 10 (exp)

State 19

   15 addOp: mulOp .
   16 mulOp: mulOp . TIMES factor
   17      | mulOp . DIV factor
   18      | mulOp . MOD factor
   19      | mulOp . COUT factor
   20      | mulOp . CIN factor
   21      | mulOp . AMP factor
   22      | mulOp . AMPCARAT factor

    TIMES     shift, and go to state 41
    DIV       shift, and go to state 42
    MOD       shift, and go to state 43
    AMP       shift, and go to state 44
    COUT      shift, and go to state 45
    CIN       shift, and go to state 46
    AMPCARAT  shift, and go to state 47

    TIMES     [reduce using rule 15 (addOp)]
    AMP       [reduce using rule 15 (addOp)]
    $default  reduce using rule 15 (addOp)

I've run out of ideas and would appreciate any help I can get.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Do you have separator for `expList`? It's kind of difficult to tell for expression 5 - 5 if that is a single expression or two of them... – Valeri Atamaniouk Feb 13 '15 at 20:26
  • I'm not sure what you mean by separator. The expression list is used so that a series of expressions read in is valid. – user2829366 Feb 13 '15 at 20:44
  • Let me rephrase the question: how the parser can determine the expression end? – Valeri Atamaniouk Feb 13 '15 at 20:46
  • An expression list can be empty, so it will read the last expression, check the list, see that nothing is there and match with empty – user2829366 Feb 13 '15 at 20:48
  • 1
    So is 5 - 5 sequence one expression (substraction) or two: 5 and - 5? – Valeri Atamaniouk Feb 13 '15 at 20:49
  • Oh wow I totally see what you mean! That completely fixed it! Thanks!!!! – user2829366 Feb 13 '15 at 20:50
  • 1
    By the way, all those precedence declarations are doing precisely nothing. So you might as well get rid of them all; they are only making your grammar unnecessarily complicated and harder to read. If you were going to use precedence declarations, you would have only one expression non-terminal (instead of four). In that case, you would still only need a single pseudo-token for unary expressions, since all unary productions have the same precedence, and you wouldn't need to declare a precedence for parenthesized expressions because that production cannot participate in a shift-reduce conflict. – rici Feb 14 '15 at 00:45

1 Answers1

-1

With precedence and associativity directives, you should start by simplify your grammar, to let them work, and check what conflicts still appear...

exp: exp OR exp {}
   | exp AND exp {}
   | exp NEQ exp {}
   | ...
   | exp PLUS exp {}
   | ... 
   | exp '*' exp {}
   | '(' exp ')' {}
   | MINUS exp %prec UMINUS {}
   | ...
   | tDEC {}
   | ...
   ;
JJoao
  • 4,891
  • 1
  • 18
  • 20