Functional Programming (H) - 4.08 - Parser Puzzles
This quiz is about the Parser parser combinator library. You will be asked to complete some code for each question. First study the code below in detail. It provides the data type for a parser of the JSON format used in JavaScript.
module JSONTypes ( JValue(..), mkJPair, mkJObj ) where import Data.Map hiding ( map ) type JMap = Data.Map.Map String JValue data JValue = JString String | JNumber Integer | JObject JMap | JArray [JValue] | JBool Bool | JNull deriving (Show) mkJPair k v = JObject (Data.Map.singleton k v) mkJObj :: [JValue] -> JValue mkJObj j_vals = let list_of_maps = map (\(JObject pair) -> pair) j_vals combined_map = Data.Map.unions list_of_maps in JObject combined_map
1. What is the type of the function json_parser?
json_parser :: Parser __ json_parser = do whiteSpace j_top <- ( json_array_parser <|> json_obj_parser) return j_top
json_parser :: Parser JValue
2. A JSON array consists of a comma-separated list of JSON values enclosed by braces, e.g.
[ 1, 'two', [ 3, true] ]
To parse this format we use the function json_array_parser below. What is the correct data constructor for the return value?
json_array_parser :: Parser JValue json_array_parser = do j_vals <- brackets $ commaSep json_value_parser return $ __ j_vals
return $ JArray j_vals
3. The JSON format supports boolean values, named true and false.
In the boolean JSON value parser below, what is the missing combinator?
json_bool_parser = do bstr <- ( symbol "true" __ symbol "false" ) let bval = if bstr == "true" then True else False return $ JBool bval
( symbol "true" <|> symbol "false" )
4. A JSON object is a list of key-value pairs, where the key is a string and the value a JSON value, enclosed in braces, e.g.
{ "Street" : "Lilybank Gardens", "Nr" : 18, "Org" : [ "University of Glasgow", { "School" : "Computing Science"} ] }
The most general JValue parser is json_value_parser, which is built of parsers for specific JSON values:
json_value_parser = json_array_parser <|> json_obj_parser <|> json_string_parser <|> json_number_parser <|> json_bool_parser <|> json_null_parser
In the JSON pair parser below, provide the name of the parser for the ‘value’ part of the pair
json_pair_parser = do k <- stringLiteral colon v <- __ return $ mkJPair k v
v <- json_value_parser
5. In the JSON object parser below, complete the return expression.
json_obj_parser :: Parser JValue json_obj_parser = do j_vals <- braces $ commaSep json_pair_parser -- a list of pairs return $ __ j_vals
return $ mkJObj j_vals
6. Given the following parser:
yin_yang :: Parser String yin_yang = do xs <- string "yin" <|> string "yang" return xs
With the definition as above this parser will fail when trying to parse “yangâ€:
*Main> run yin_yang "yang" parse error at (line 1, column 1): unexpected "a" expecting "yin"
How should you modify the parser so that it will work correctly?
yin_yang :: Parser String yin_yang = do xs <- try (string "yin") <|> string "yang" return xs
yin_yang :: Parser String yin_yang = do xs <- try (string "yin" <|> string "yang") return xs
yin_yang :: Parser String yin_yang = do xs <- string "yin" <|> try (string "yang") return xs
Submit Quiz