Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data AST = C String | Seq AST AST | Star AST | Plus AST AST deriving Show
- parsePar :: String -> (AST, String)
- parsePar (x:str)
- | not (null excess) = (parse token, tail excess)
- | otherwise = error "Incorrect parentheses"
- where
- token = takeWhile (\e -> (not (elem e ")"))) (x:str)
- excess = dropWhile (\e -> (not (elem e ")"))) (x:str)
- parseEl :: String -> (AST, String)
- parseEl ('(':str) = (ast, excess)
- where
- (ast, excess) = parsePar str
- parseEl (x:str)
- | isDigit x = ((C [x]), str)
- | isLetter x = ((C [x]), str)
- | otherwise = error $ "Feil: " ++ [x]
- parseBa :: String -> (AST, String)
- parseBa str
- | null excess = (ast, [])
- | head excess == '*' = (Star ast, tail excess)
- | otherwise = (ast, excess)
- where
- (ast, excess) = parseEl str
- parseSq :: String -> (AST, String)
- parseSq str
- | null excess = (ast, [])
- | not (elem (head excess) "+*") = (Seq ast ast2, excess2)
- | otherwise = (ast, excess)
- where
- (ast, excess) = parseBa str
- (ast2, excess2) = parseSq excess
- parseRe :: String -> (AST, String)
- parseRe str
- | null excess = (ast, [])
- | head excess == '+' = (Plus ast ast2, excess2)
- | otherwise = (ast, excess)
- where
- (ast, excess) = parseSq str
- (ast2, excess2) = parseRe (tail excess)
- parse :: String -> AST
- parse str = fst (parseRe str)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement