add simple Peggy expression parser

This commit is contained in:
darkf 2013-11-01 18:53:36 -07:00
parent 582895005a
commit bfceb2adda
1 changed files with 28 additions and 0 deletions

28
parser2.hs Normal file
View File

@ -0,0 +1,28 @@
{-# Language TemplateHaskell, QuasiQuotes, FlexibleContexts #-}
import Text.Peggy
import AST
[peggy|
top :: AST = expr !.
expr :: AST
= expr "+" fact { Add $1 $2 }
/ expr "-" fact { Sub $1 $2 }
/ fact
fact :: AST
= fact "*" term { Mul $1 $2 }
/ fact "/" term { Div $1 $2 }
/ term
term :: AST
= "(" expr ")"
/ number
number ::: AST
= [1-9] [0-9]* { IntConst $ read ($1 : $2) }
|]
main :: IO ()
main = print . parseString top "<stdin>" =<< getContents