import Text.Megaparsec import Text.Megaparsec.Char import Control.Monad.Combinators.Expr import Data.Void (Void) type Parser = Parsec Void String data Expr = Var Char | Add Expr Expr | Mul Expr Expr deriving Show var :: Parser Expr var = Var <$> letterChar parens :: Parser a -> Parser a parens = between (char '(') (char ')') term :: Parser Expr term = choice [ var, parens expr ] expr :: Parser Expr expr = makeExprParser term [ [ InfixN (Mul <$ string "") , InfixN (Add <$ string "+") ] ] main :: IO () main = parseTest (expr <* eof) "x+y(xz+z)"