user2150839
user2150839

Reputation: 483

parsing parentheses from a string

So I have a logical statement such as "((A o B) a B) o (B a C)" and I want to parse each set of statements within the parenthesis to each part in a list... here is what I have so far but since im new to haskell I have no idea if im on the right track edit: I now have this again. So the problem now is two things!! (1) I have trouble returning a list of strings (2) when I run it now I get unexpected '('

statement :: Parser String
statement =  many (letter <|> space ) >> parenStatement

parenStatement :: Parser [String]
parenStatement = do
    char '('
    answer <- statement
    char ')'
    return answer

Upvotes: 0

Views: 664

Answers (1)

dave4420
dave4420

Reputation: 47072

Presumably you have some data type that represents a logical statement.

data Statement = Conjunction Statement Statement
               | Disjunction Statement Statement
               -- etc

Presumably you ultimately want to be able to parse logical statements.

statement :: Parser Statement
statement = conjunction <|> disjunction <|> parenthesisisedStatement -- etc

Parsing a statement surrounded by parentheses is then just

parenthesisisedStatement :: Parser Statement
parenthesisisedStatement = do
    char '('
    answer <- statement
    char ')'
    return parenthesisisedStatement

Edit: (because this won't fit into a comment)

You appear to be trying to tokenise your input string into a list of strings (that you might later feed into a parser). You do not seem to be trying to parse your input string into a data structure that properly constrains what it may contain.

If you really want to tokenise your input, I advise asking again in a new top level question.

But you are probably better off parsing your input into a proper data structure, so think about how best to represent statements and don't just use strings everywhere.

Upvotes: 3

Related Questions