Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data AST = C String | Seq AST AST | Star AST | Plus AST AST deriving Show
  2.  
  3. parsePar :: String -> (AST, String)
  4. parsePar (x:str)
  5.     | not (null excess) = (parse token, tail excess)
  6.     | otherwise       = error "Incorrect parentheses"
  7.         where
  8.             token = takeWhile (\e -> (not (elem e ")"))) (x:str)
  9.             excess = dropWhile (\e -> (not (elem e ")"))) (x:str)
  10.  
  11. parseEl :: String -> (AST, String)
  12. parseEl ('(':str) = (ast, excess)
  13.         where
  14.             (ast, excess) = parsePar str
  15. parseEl (x:str)
  16.     | isDigit x = ((C [x]), str)
  17.     | isLetter x = ((C [x]), str)
  18.     | otherwise = error $ "Feil: " ++ [x]
  19.  
  20. parseBa :: String -> (AST, String)
  21. parseBa str  
  22.     | null excess = (ast, [])
  23.     | head excess == '*' = (Star ast, tail excess)
  24.     | otherwise = (ast, excess)
  25.         where
  26.             (ast, excess) = parseEl str
  27.  
  28. parseSq :: String -> (AST, String)
  29. parseSq str
  30.     | null excess = (ast, [])
  31.     | not (elem (head excess) "+*") = (Seq ast ast2, excess2)
  32.     | otherwise =  (ast, excess)
  33.         where
  34.             (ast, excess) = parseBa str
  35.             (ast2, excess2) = parseSq excess
  36.  
  37. parseRe :: String -> (AST, String)
  38. parseRe str  
  39.    | null excess = (ast, [])  
  40.    | head excess == '+' = (Plus ast ast2, excess2)
  41.    | otherwise   = (ast, excess)
  42.         where  
  43.          (ast, excess) = parseSq str
  44.          (ast2, excess2) = parseRe (tail excess)
  45.  
  46. parse :: String -> AST
  47. parse str = fst (parseRe str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement