<div style="color:#000; font-size: 14px;font-family: arial;"><div>Hi Francesco,<br><br>Thanks for your reply. But after I change the aOperators like the following, it still produce non-correct parse result.<br><br>aOperators = [ [Prefix (reservedOp "-" >> return (Neg ))]<br>             , [ Infix (reservedOp "*" >> return (ABinary Divide )) AssocLeft,<br>                 Infix (reservedOp "/" >> return (ABinary Multiply )) AssocLeft]<br>             , [Infix (reservedOp "+" >> return (ABinary Add )) AssocLeft,<br>                Infix (reservedOp "-" >> return (ABinary Subtract )) AssocLeft]<br>            ]<br><br><br>parse result:<br><br>*Main> parseString "res := a / b * c"<br>Assign "res" (ABinary Divide (ABinary Multiply (Var "a") (Var "b")) (Var "c"))<br>*Main> <br></div><div><br></div><div><br></div><div><br></div></div><div id="spnEditorSign" name="100"><div><div>--m00nlight<br></div></div></div><div><br></div><!-- jy5ContentSuffix --><div>在2015年04月13 15时21分, "Francesco Ariis"<fa-ml@ariis.it>写道:</div><blockquote id="isReplyContent" style="padding-left: 1ex; margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204);"><br>On Mon, Apr 13, 2015 at 02:56:47PM +0800, m00nlight wrote:<br>>  Dear Haskellers,<br>> <br>> I have tried the "while language parser" tutorial on haskellwiki, but I<br>> encounter an problem I can not understand here,<br>> The following is the code:<br>> <br>> http://pastebin.com/saC9vwCn<br>> <br>> <br>> So it seems that the order of the operator will affect the parse result.<br>> What's the problem with my code?  How can I make my program to produce<br>> consistent parse result.<br><br>Checking the documentation for OperatorTable [1] (the first argument to<br>buildExpressionParser), it seems it is working as expected:<br><br>    An OperatorTable s u m a is a list of Operator s u m a lists. The list<br>    is ordered in descending precedence. All operators in one list have<br>    the same precedence (but may have a different associativity).<br><br>I think you can take advantage of the fact that OperatorTable is /a list<br>of lists/, hence<br><br>    aOperators = [ [Prefix (reservedOp "-" >> return (Neg ))]<br>                 , [Infix (reservedOp "/" >> return (ABinary Divide )) AssocLeft,<br>                    Infix (reservedOp "*" >> return (ABinary Multiply )) AssocLeft]<br>                 , [Infix (reservedOp "+" >> return (ABinary Add )) AssocLeft]<br>                 , [Infix (reservedOp "-" >> return (ABinary Subtract )) AssocLeft]<br>                 ]<br><br>should do the trick (try swapping the elements in the second list.<br>Will this do?<br><br><br>[1] http://hackage.haskell.org/package/parsec-3.0.0/docs/Text-Parsec-Expr.html#t:OperatorTable<br>_______________________________________________<br>Beginners mailing list<br><a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners<br></blockquote><br><br><span title="neteasefooter"><span id="netease_mail_footer"><span title="neteasefooter"><span id="netease_mail_footer"><a href="#" target="_blank"></a></span></span>
</span></span>