module Util0Parser where { import Text.ParserCombinators.Parsec; import Text.ParserCombinators.Parsec.Expr; import Token; import Util0Type; -- パーサー parseExpr :: GenParser Char a Expr; parseExpr = buildExpressionParser table parseFactor "expression"; table :: [[Operator Char a Expr]]; table = [ [ Infix (do { reservedOp "*"; return Mult }) AssocLeft], [ Infix (do { reservedOp "+"; return Add }) AssocLeft] ]; parseFactor :: GenParser Char a Expr; parseFactor = parens parseExpr <|> (do { t <- naturalOrFloat; return (case t of { Left i -> Const (Num (fromInteger i)); Right d -> Const (Num d) }) }) "simple expression" ; myParse :: String -> Expr; myParse str = case (parse (do {whiteSpace; s<- parseExpr; eof; return s }) "" str) of { Left err -> Const (Num (1/0)); Right x -> x } ; }