Ternary infix expression parser

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Ternary infix expression parser

Jeffrey Brown
I want to write a parser that lets someone write "(a #uses b) #because (c #needs d)" and read it into a tree:

  data Tree = Tree String Tree Tree | Leaf String
  magic "(a #uses b) #because (c #needs d)"
    = Tree "because" (Tree "uses" (Leaf "a") (Leaf "b"))
                     (Tree "needs" (Leaf "c") (Leaf "d"))

I thought I could use Text.Megaparsec.Expr for it, but now I realize it hadnles binary operations, and this is a ternary one.

Suggestions?


--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Ternary infix expression parser

Jon Fairbairn
Jeffrey Brown <[hidden email]> writes:

> I want to write a parser that lets someone write "(a #uses b) #because (c
> #needs d)" and read it into a tree:
>
>   data Tree = Tree String Tree Tree | Leaf String
>   magic "(a #uses b) #because (c #needs d)"
>     = Tree "because" (Tree "uses" (Leaf "a") (Leaf "b"))
>                      (Tree "needs" (Leaf "c") (Leaf "d"))
>
> I thought I could use Text.Megaparsec.Expr for it, but now I realize it
> hadnles binary operations, and this is a ternary one.
>
> Suggestions?

Parse it as three binary ones and check the tree afterwards?

--
Jón Fairbairn                                 [hidden email]


_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Ternary infix expression parser

S D Swierstra-2
In reply to this post by Jeffrey Brown
Make the #uses expr #because expr #needs part act like an operator?

Doaitse

Op 19 aug. 2017 om 05:56 heeft Jeffrey Brown <[hidden email]> het volgende geschreven:

I want to write a parser that lets someone write "(a #uses b) #because (c #needs d)" and read it into a tree:

  data Tree = Tree String Tree Tree | Leaf String
  magic "(a #uses b) #because (c #needs d)"
    = Tree "because" (Tree "uses" (Leaf "a") (Leaf "b"))
                     (Tree "needs" (Leaf "c") (Leaf "d"))

I thought I could use Text.Megaparsec.Expr for it, but now I realize it hadnles binary operations, and this is a ternary one.

Suggestions?


--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
Reply | Threaded
Open this post in threaded view
|

Re: Ternary infix expression parser

Jeffrey Brown
I appear to be able to read Text.Megaparsec.Expr! This is a breakthrough for me. I think I refactor it. Thanks, and sorry for the noise.

On Sat, Aug 19, 2017 at 3:13 AM, S D Swierstra <[hidden email]> wrote:
Make the #uses expr #because expr #needs part act like an operator?

Doaitse

Op 19 aug. 2017 om 05:56 heeft Jeffrey Brown <[hidden email]> het volgende geschreven:

I want to write a parser that lets someone write "(a #uses b) #because (c #needs d)" and read it into a tree:

  data Tree = Tree String Tree Tree | Leaf String
  magic "(a #uses b) #because (c #needs d)"
    = Tree "because" (Tree "uses" (Leaf "a") (Leaf "b"))
                     (Tree "needs" (Leaf "c") (Leaf "d"))

I thought I could use Text.Megaparsec.Expr for it, but now I realize it hadnles binary operations, and this is a ternary one.

Suggestions?


--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.



--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.