Advertisement
Guest User

Untitled

a guest
Oct 13th, 2018
640
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. parse :: String -> AST
  4. parse str = fst (parseRe str)
  5.  
  6. parseRe :: String -> (AST, String)
  7. parseRe str  
  8.    | null rest = (ast, [])  
  9.    | head rest == '+' = (Plus ast ast2, rest2)
  10.    | otherwise (ast, rest)
  11.   where  (ast, rest) = parseSq str , (ast2, rest2) = parseRe (tail rest)
  12.  
  13. parseBa :: String -> (AST, String)
  14. parseBa str = if null rest then (ast, [])
  15.               else if head rest == '*' then (Star ast, tail rest)
  16.               else (ast, rest)
  17.         where
  18.             (ast, rest) = parseEl str
  19.  
  20. parseSq :: String -> (AST, String)
  21. parseSq str = if null rest then (ast, [])
  22.               else if not (elem (head rest) "*+") then (Seq ast ast2, rest2)
  23.               else (ast, rest)
  24.         where
  25.             (ast, rest) = parseBa str
  26.             (ast2, rest2) = parseSq rest
  27.  
  28. parseEl :: String -> (AST, String)
  29. parseEl ('(':str) = (ast, rest)
  30.         where
  31.             (ast, rest) = parsePar str
  32. parseEl (x:str) = if isDigit x then ((C [x]), str)
  33.                  else if isLetter x then ((C [x]), str)
  34.                  else error $ "Feil: " ++ [x]
  35.  
  36. parsePar :: String -> (AST, String)
  37. parsePar (x:str)
  38.     | not (null rest) = (parse token, tail rest)
  39.     | otherwise        = error "Incorrect parentheses"
  40.             where
  41.                 token = takeWhile (\e -> (not (elem e ")"))) (x:str)
  42.                 rest = dropWhile (\e -> (not (elem e ")"))) (x:str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement