tokenize "" = ["epsilon"]
tokenize (':':x:xs) =
case x of
')' -> "smiley" : (tokenize xs)
'(' -> "frowney" : (tokenize xs)
_ -> "ignored" : (tokenize xs)
tokenize ('(':xs) = "left_paren" : (tokenize xs)
tokenize (')':xs) = "right_paren" : (tokenize xs)
tokenize (x:xs) = "ignored" : (tokenize xs)
parseParens stack (tok:tokens) =
case tok of
"left_paren" -> parseParens ("left_paren":stack) tokens
"right_paren" ->
case stack of
(s:[]) -> parseSentence True tokens
(s:st) -> parseParens st tokens
"epsilon" -> parseSentence False []
"smiley" -> case stack of
("left_paren":[]) -> parseSentence True tokens
_ -> parseParens stack tokens
"frowney" -> parseParens stack tokens
_ -> parseParens stack tokens
parseSentence False [] = False
parseSentence True [] = True
parseSentence intable (tok:tokens) =
case tok of
"ignored" -> parseSentence True tokens
"smiley" -> parseSentence True tokens
"frowney" -> parseSentence True tokens
"epsilon" -> parseSentence True tokens
"left_paren" -> parseParens ["left_paren"] tokens
"right_paren" -> False
isBalanced = parseSentence True . tokenize
yesno True = "YES"
yesno False = "NO"
out n xs
= "Case #"++(show n
)++": "++(show $ yesno
$ isBalanced xs
)
parseInput xs =
main = do
-- input <- readFile "./emotinput2"
-- writeFile "./emotoutput2" $ parseInput input
print $ isBalanced
"((:)))"