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?
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.