Parsing, infixes and on-the-fly precedence

I imagine a parser that operates on (String,[String]) pairs, for which the second element allows the user to define precedence on the fly. For instance, it would read the pair (x,y) = ("a , b : c", [":" , ","]) as "(a , (b : c))" because y informs the parser that in x the colon binds first, then the comma. I know it is possible, because Haskell does it already! In Haskell we can define novel infix operators on the fly, including their precedence relative to each other. I don't know how, though, so that's my question. -- Jeffrey Benjamin Brown

Hi Jeffrey,
On Mon, Sep 28, 2015 at 8:28 PM, Jeffrey Brown
I imagine a parser that operates on (String,[String]) pairs, for which the second element allows the user to define precedence on the fly.
For instance, it would read the pair (x,y) = ("a , b : c", [":" , ","]) as "(a , (b : c))" because y informs the parser that in x the colon binds first, then the comma.
I know it is possible, because Haskell does it already! In Haskell we can define novel infix operators on the fly, including their precedence relative to each other.
I don't know how, though, so that's my question.
You might like to look at Text.Parsec.Token[1], which does exactly what you describe. [1] https://hackage.haskell.org/package/parsec-3.1.9/docs/Text-Parsec-Expr.html
-- Jeffrey Benjamin Brown
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-- Chris Wong (https://lambda.xyz) "I fear that Haskell is doomed to succeed." -- Tony Hoare

On Mon, Sep 28, 2015 at 8:58 PM, Chris Wong
Hi Jeffrey,
[snip]
You might like to look at Text.Parsec.Token[1], which does exactly what you describe.
[1] https://hackage.haskell.org/package/parsec-3.1.9/docs/Text-Parsec-Expr.html
Whoops, I meant "Text.Parsec.Expr". But you can probably figure that out from context :) -- Chris Wong (https://lambda.xyz) "I fear that Haskell is doomed to succeed." -- Tony Hoare

I know it is possible, because Haskell does it already! In Haskell we can define novel infix operators on the fly, including their precedence relative to each other. I don't know how, though, so that's my question.
You might like to look at https://en.wikipedia.org/wiki/Operator-precedence_grammar Stefan

On Mon, Sep 28, 2015 at 2:28 PM, Jeffrey Brown
I imagine a parser that operates on (String,[String]) pairs, for which the second element allows the user to define precedence on the fly.
Let's use the Parser newtype newtype Parser a = Parser { parse :: String -> [(a,String)] } from http://dev.stephendiehl.com/fun/002_parsers.html as a basis for discussion. Now you want to "operate on (String,[String]) pairs", so the Parser newtype must now look like this: type MyInput = (String,[String]) newtype MyParser a = MyParser { parse :: MyInput -> [(a,MyInput)] } Is this what you have in mind? -- Kim-Ee

Chris I thanked in private. Perhaps I should have done that publicly; it
looks like the Parsec.Expr library is exactly what I need.
Kim-Ee, yes, I believe that's exactly what I'm going for.
On Mon, Sep 28, 2015 at 8:07 PM, Kim-Ee Yeoh
On Mon, Sep 28, 2015 at 2:28 PM, Jeffrey Brown
wrote: I imagine a parser that operates on (String,[String]) pairs, for which the second element allows the user to define precedence on the fly.
Let's use the Parser newtype
newtype Parser a = Parser { parse :: String -> [(a,String)] }
from
http://dev.stephendiehl.com/fun/002_parsers.html
as a basis for discussion.
Now you want to "operate on (String,[String]) pairs", so the Parser newtype must now look like this:
type MyInput = (String,[String])
newtype MyParser a = MyParser { parse :: MyInput -> [(a,MyInput)] }
Is this what you have in mind?
-- Kim-Ee
-- Jeffrey Benjamin Brown
participants (4)
-
Chris Wong
-
Jeffrey Brown
-
Kim-Ee Yeoh
-
Stefan Monnier