Skip to content

Instantly share code, notes, and snippets.

@Dierk
Created November 16, 2014 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dierk/89b34a4b06cf4199288d to your computer and use it in GitHub Desktop.
Save Dierk/89b34a4b06cf4199288d to your computer and use it in GitHub Desktop.
Frege Template for the monadic parser exercises of the FP101x course
-- Functional parsing library from chapter 8 of Programming in Haskell,
-- Graham Hutton, Cambridge University Press, 2007.
-- Template file for the homework in the FP101x EdX course.
-- Read also: http://www.cs.nott.ac.uk/~gmh/monparsing.pdf
-- adapted to Frege by Dierk Koenig
-- uncomment code as needed
module Parsing where
import Data.Char
infixr 5 `+++`
-- The monad of parsers
-- --------------------
data Parser a = P ([Char] -> [(a,[Char])])
--instance Monad Parser where
-- return v = P (\inp -> [(v,inp)])
-- p >>= f = <your code comes here>, remember the type: Parser a -> (a -> Parser b) -> Parser b
-- Basic parsers
-- -------------
failure :: Parser a
failure = P (\inp -> [])
item :: Parser Char
item = P (\inp -> case inp of
[] -> []
(x:xs) -> [(x,xs)]
)
parse :: Parser a -> [Char] -> [(a,[Char])]
parse (P p) inp = p inp
return v = P (\inp -> [(v,inp)]) -- needed for the initial questions about "return"
-- Choice
-- ------
(+++) :: Parser a -> Parser a -> Parser a
(+++) p q = P (\inp -> case parse p inp of
[] -> parse q inp
[(v,out)] -> [(v,out)]
)
-- Derived primitives
-- ------------------
--sat :: (Char -> Bool) -> Parser Char
--sat p = do x <- item
-- if p x then return x else failure
--
--digit :: Parser Char
--digit = sat isDigit
--
--lower :: Parser Char
--lower = sat isLower
--
--upper :: Parser Char
--upper = sat isUpper
--
--letter :: Parser Char
--letter = sat Char.isLetter
--
--alphanum :: Parser Char
--alphanum = sat (\c -> Char.isLetter c || isDigit c)
--
--char :: Char -> Parser Char
--char x = sat (== x)
--string :: [Char] -> Parser [Char]
--string [] = return []
--string (x:xs) = do char x
-- string xs
-- return (x:xs)
--comment :: Parser [Char]
--comment = <your code comes here>
--many :: Parser a -> Parser [a]
--many p = many1 p +++ return []
--
--many1 :: Parser a -> Parser [a]
--many1 p = do v <- p
-- vs <- many p
-- return (v:vs)
--
--ident :: Parser [Char]
--ident = do x <- lower
-- xs <- many alphanum
-- return (x:xs)
--
--read :: [Char] -> Int
--read cs = sum [ (ord (fst be) - ord ('0')) * 10 ^ snd be | be <- zip (reverse cs) [0..] ]
--
--nat :: Parser Int
--nat = do xs <- many1 digit
-- return (read xs)
--int :: Parser Int
--int = <your code comes here>
--space :: Parser ()
--space = do many (sat isSpace)
-- return ()
--expr = <your code comes here>
-- Ignoring spacing
-- ----------------
--token :: Parser a -> Parser a
--token p = do space
-- v <- p
-- space
-- return v
--
--identifier :: Parser [Char]
--identifier = token ident
--
--natural :: Parser Int
--natural = token nat
--
--integer :: Parser Int
--integer = token int
--
--symbol :: [Char] -> Parser [Char]
--symbol xs = token (string xs)
-- successively uncomment as you solve the homework
--main _ = do
-- println (read $ unpacked "1234")
-- println (parse item (unpacked "hello"))
-- println (parse (string (unpacked "hello")) (unpacked "hello"))
-- println (parse (return 1 +++ return 2) [])
-- println (parse (return 1) (unpacked "hello"))
-- println (parse (item +++ return 'a') (unpacked "hello"))
--
--
-- println (parse int (unpacked "007"))
-- println (parse int (unpacked "-007"))
--
-- println (parse comment (unpacked "-- tralala --\n"))
-- println (parse expr (unpacked "1 - 2 - 3"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment