module MyFirstParsec where -- Original grammar: (E is start symbol) -- E ::= T | "-" T | E "+" T | E "-" T . -- T ::= num | "(" E ")" . -- Lexical specifications: -- num is one or more decimal digits (0-9) -- all tokens may be separated by arbtrary whitespace (spaces, tabs, newlines). -- Rewritten grammar, without left-recursion: -- E ::= T E' | "-" T E' -- E' ::= "+" T E' | "-" T E' | epsilon -- T ::= num | "(" E ")" -- Find details about Parsec on hackage. Look as the difference parsers and combinators -- [1] https://hackage.haskell.org/package/parsec import Text.ParserCombinators.Parsec import Data.Char (isDigit, isSpace) -- Our AST data Exp = Num Int | Negate Exp | Add Exp Exp deriving (Eq, Show) -- lexeme is a lexer that removes trailling white spaces -- It takes any parser (Parser a) and extends it. Thus returning the same type of parser. lexeme :: Parser a -> Parser a lexeme p = do a <- p -- Run the parser skipMany space -- Remove trailling spaces; return a -- skipMany: `skipMany p` applies the parser p zero or more times, skipping its result. [1, Prim module] -- space: Parses a white space character (any character which satisfies isSpace) Returns the parsed character. [1, Char module] -- pNum is a parser that gives an integer pNum :: Parser Int pNum = lexeme $ do ds <- many1 (satisfy isDigit) -- Parses one or more digits return $ read ds -- Return string as number -- many1: `many1 p` applies the parser p one or more times. Returns a list of the returned values of p. [1, Combinators module] -- satisfy: The parser satisfy f succeeds for any character for which the supplied function f returns True. Returns the character that is actually parsed. [1, Char module] -- symbol gives a parse the parses a specific symbol symbol :: String -> Parser () symbol s = lexeme $ do _ <- string s; -- Parse s, but throw it away return () -- Return nothing -- string: string s parses a sequence of characters given by s. Returns the parsed string (i.e. s). [1, Char module] -- parens takes any parser and gives a parser that runs this parser within parenthesis parens :: Parser a -> Parser a parens a = do symbol "(" -- Parse left-parenthesis e <- a -- Run the given parser symbol ")" -- Parse a right-parenthesis return e -- Return the result of the parser parseE :: Parser Exp parseE = undefined parseE' :: Exp -> Parser Exp parseE' = undefined parseT :: Parser Exp parseT = undefined -- parseString runs the parser on the string and returns either an error or an AST parseString :: String -> Either ParseError Exp parseString s = case parse expParser "" s of -- Run my parser; See [1, Prim module] for details Left err -> Left err -- If error, then return this Right e -> return e -- If correct AST, then return this where expParser = do lexeme (return ()) -- Remove initial white spaces e <- parseE -- Run the top parser eof -- parse end-of-file; nothing more to parse return e -- return the result of my parser -- parse: `parse p filePath input` runs a parser p over Identity without user state. [1, Prim module]