昼休みの時間を使って中置表記のパーサを考えてみる。
まだいろいろわかってません。結構必死です(苦笑)。
expression :: Parser [Elem] expression = do { many space; t <- term; ts <- many $ try terms; many space; return $ t ++ (concat ts) } where terms = do { many space; op <- (plus <|> minus); many space; t <- term; return (t ++ [op]) } term = do { f <- factor; fs <- many $ try factors; return $ f ++ (concat fs) } factors = do { many space; op <- times <|> divide; many space; f <- factor; return (f ++ [op]) } factor = braced_exp <|> value' braced_exp = do { char '('; e <- expression; char ')'; return e } value' = do { v <- value; return [v] } value = do { v <- many digit; return $ Value $ read v }
追記:ミスがあったのでちょっと修正。