I’m currently writing a parser for a simple programming language. It’s getting there however I’m unable to parse a boolean logic statement such as “i == 0 AND j == 0”. All I get back is “non exhaustive patterns in case”
When I parse a boolean expression on its own it works fine e.g. “i == 0”. Note “i == 0 a” will also return a boolean statement but “i == 0 AND” does not return anything.
Can anyone help please?
Whilst the above works correctly for input such as run parseBoolean “i == 0”
As @hammar points out, you should use
Text.Parsec.Exprfor this kind of thing. However, since this is homework, maybe you have to do it the hard way!The problem is in
parseArithmetic, you allowanyCharto be an operator, but then in the case statement, you only allow for+,-,*,/,%, and^. WhenparseArithmetictries to parsei == 0, it uses the first=as the operator, but can’t parse anintExp2from the second=, and fails in the monad, and backtracks, before getting to the case statement. However, when you try to parsei == 0 AND j == 0, it gets thei ==part, but then it thinks that there’s an arithmetic expression of0 A ND, whereAis an operator, andNDis the name of some variable, so it gets to the case, and boom.Incidentally, instead of using the parser to match a string, and then using a case statement to match it a second time, you can have your parser return a function instead of a string, and then apply the function directly: