2
votes

I'm using Parsec and the example version of boolExpr http://hpaste.org/86299 at the moment. I'm compiling on Windows via GHC.

The code above will match a boolean expression like 3 < 4 or a not 3 however it will not match an expression like 3, true or (((3 < 1))). Can anyone give me advice on how to match such expressions like 3 and (((3 < 1))) the same as 3 > 0 and (((3 < 1))) > 0, where the >0 is assumed/added automatically on RHS-less expressions?

1
To clarify: you want to parse the string "3" into the same structure as "3 > 0"? - Tikhon Jelvis
ie: 3 > 0 results in an expression that validates if (expr) expr; however (3 > 0) results in (expr), hence, for that to be valid I must be able to parse any single term inside brackets against >0 if there is no RHS. (((3))) -> (((3 > 0) > 0) > 0) > 0 unless you know a better way to do it so that expressions like while (1), while ((1)) and while ((x>2)) are valid. - kvanbere

1 Answers

2
votes

This looks like you're trying to push the semantics of your language into a syntax parser. The "correct" thing to do from a programming-languages perspective is to accept both number and boolean-valued expressions in your syntax tree. Then, at a later stage -- type reconstruction not parsing -- decide that number-valued expressions get a single "> 0" added to them while boolean valued expressions do not.