-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.hs
41 lines (35 loc) · 965 Bytes
/
Example.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-# OPTIONS -XDoRec -XDeriveDataTypeable #-}
module Example where
import Text.Earley
import Data.Dynamic
import Control.Applicative
data E = Nat Int
| Add E E deriving (Show, Typeable)
grammar :: Grammar (Rule Char E)
grammar = do
nat <- rule "NAT"
[ fmap (\_ -> 0) (term '0')
, fmap (\_ -> 1) (term '1')
]
rec expr <- rule "EXPR"
[ fmap Nat $ var nat
, pure (\x _ y -> Add x y)
<*> var expr
<*> term '+'
<*> var expr
]
return expr
-- See figure 2 in
-- http://webhome.cs.uvic.ca/~nigelh/Publications/PracticalEarleyParsing.pdf
badG :: Grammar (Rule Char ())
badG = do
e <- rule "E" [pure ()]
a <- rule "A" [term 'a', var e]
s <- rule "S" [pure (\_ _ _ _ -> ())
<*> var a
<*> var a
<*> var a
<*> var a]
return s
main :: IO ()
main = test grammar "1+0"