Happy shift/reduce - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Pedro Minicz

When encountering a shift/reduce conflict, Happy automatically chooses shift. This is the behavior I want. Is it possible to add an annotation on the rules that cause the conflict telling Happy "I know there is a conflict and that you will choose shift, that is fine, it is what I want". I don't want to disable shift/reduce warnings globally because that could hide actually mistakes in the future.

Here is the grammar in question:

stmt :: { Stmt }
  : compound_stmt               { $1 }
  | expr ';'                    { Expression $1 }
  | 'if' '(' expr ')' stmt      { If $3 $5 }
  | 'if' '(' expr ')' stmt 'else' stmt { IfElse $3 $5 $7 }
  | 'while' '(' expr ')' stmt   { While $3 $5 }
  | 'return' expr ';'           { Return $2 }

compound_stmt :: { Stmt }
  : '{' stmt_list '}'           { Compound [] $2 }
  | '{' decl_list stmt_list '}' { Compound $2 $3 }

decl :: { ByteString }
  : 'int' identifier ';'        { $2 }

decl_list :: { [ByteString] }
  : decl                        { [$1] }
  | decl_list decl              { $2 : $1 }

stmt_list :: { [Stmt] }
  : stmt                        { [$1] }
  | stmt_list stmt              { $2 : $1 }

(Also, if anyone knows a grammar with the same result but without the conflict, that would be nice.)

TheMatten

Are you me? I'm literally fixing shift conflicts in Happy right now :joy:
Anyway, there's %shift annotation you can put after offending rule - I would recommend you to read tutorial in GHC's parser, because it covers steps to locate offending rules