Order-dependent Happy Grammar

Seth Fogarty 2018-03-05 21:59:27.520222 UTC

1{
2data Token = Int | Neg | Both deriving Show
3}
4
5%monad { Maybe }
6%name parse
7%tokentype {Token}
8%error { parseError}
9
10%token
11 int { Int }
12 '-' { Neg }
13 both { Both }
14
15%nonassoc both
16%left '-'
17%nonassoc NEG
18
19%%
20
21E :: {Expr}
22E : int {IntE}
23 | both E E {BothE $2 $3}
24 | '-' E %prec NEG {NegE $2}
25 | E '-' E {SubE $1 $3}
26{
27
28data Expr = BothE Expr Expr | IntE | SubE Expr Expr | NegE Expr deriving Show
29
30parseError :: [Token] -> Maybe a
31parseError _ = Nothing
32
33}