2

I am writing a parser using Happy/Alex, and because the grammar I am parsing isn't entirely context free, I have need to get a hold of the lookahead token. The Happy documentation suggests that when using a threaded lexer, this can be done with rules of the form

n  :  t_1 ... t_n          {%^ <expr> }

So I have written a rule

gdecl   : type ident paramList block     { FDefn $1 $2 $3 $4 }
        | type ident paramList ';'       { FDecl $1 $2 $3 }
        | typedef type ident ';'         {%^ mkTypDef $2 $3 }
        | struct ident ';'               { StructDecl $2 }
        | struct ident '{' fieldList '}' { StructDefn $2 $4 }

This produces a .hs file that ghc cannot parse, however. GHC gives the following error:

Parser.hs:301:47: parse error on input ‘tk’

This is the haskell function which produces the error:

happyReduce_6 = happyMonadReduce 4# 2# happyReduction_6
happyReduction_6 (happy_x_4 `HappyStk`
        happy_x_3 `HappyStk`
        happy_x_2 `HappyStk`
        happy_x_1 `HappyStk`
        happyRest) tk 
         = happyThen (case happyOut21 happy_x_2 of { happy_var_2 ->
        case happyOutTok happy_x_3 of { (LocTok _ (TokIdent happy_var_3)) ->
        ( mkTypDef happy_var_2 happy_var_3)}} tk        <-- Error Here
        ) (\r -> happyReturn (happyIn6 r))

I have left mkTypDef = undefined, just to make sure I wasn't making any type errors. Normal monadic productions (with {% <expn> }) work fine. I am using Happy 1.19.4 and GHC 7.8.3.

Is this likely a bug, or is there something that I am doing wrong?

John G
  • 269
  • 2
  • 9
  • 4
    The parse error is actually on the next-to-last line, which ends an expression `case ... of { ... } tk` which isn't valid. I'd say it is definitely a bug in happy, though I don't know whether your grammar syntax is correct. – Reid Barton Oct 19 '14 at 02:00
  • Oh good call, my mistake. I guess I will open a bug report then. – John G Oct 19 '14 at 16:52
  • @ReidBarton since your comment resolved the question, you could turn it into an answer? Otherwise this will just languish as an unanswered question... – sclv Feb 21 '15 at 16:44

1 Answers1

0

As pointed out by Ried Barton above, this is indeed a bug, and it has been reported here: https://github.com/simonmar/happy/issues/35.

John G
  • 269
  • 2
  • 9