<div dir="ltr"><div><div><div><div>Dear guys,<br><br></div>Thanks! chainr and chainl are exactly what I was looking for. I did something along the lines of<br><br></div>andParser = ExpAnd <$> ((stringParser <|> andParser) <* "and") <*> (stringParser <|> andParser)<br><br></div>I can see now, why that wouldn't work!<br><br></div><div>Regards,<br></div><div>Hon<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 6 October 2016 at 01:05, Leonard Wörteler <span dir="ltr"><<a href="mailto:leo@woerteler.de" target="_blank">leo@woerteler.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
<br>
Am 05.10.2016 um 14:32 schrieb Lian Hung Hon:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Given<br>
<br>
data Expression = ExpToken String | ExpAnd Expression Expression<br>
<br>
How to write an attoparsec parser that parses this example:<br>
<br>
"a" and "b" and "c"<br>
<br>
into<br>
<br>
ExpAnd (ExpToken "a") (ExpAnd (ExpToken "b") (ExpToken "c"))?<br>
</blockquote>
<br></span>
You can re-implement `chainr1` from Parsec as follows:<br>
<br>
    chainr1 :: Parser a -> Parser (a -> a -> a) -> Parser a<br>
    chainr1 p op = scan<br>
      where<br>
        scan   = p >>= rest<br>
        rest x = op <*> pure x <*> scan <|> return x<br>
<br>
Then you just plug in your parsers for variables and the `and` operator. Working example attached.<br>
<br>
-- Leo<br>
</blockquote></div><br></div>