This page is no longer maintained — Please continue to the home page at www.scala-lang.org

<~ and ~> in parser combinators

3 replies
Dave Ray
Joined: 2009-01-07,
User offline. Last seen 42 years 45 weeks ago.

Hi there. I was wondering if someone could point me to a good
explanation of the semantics of the <~ and ~> operators when using
parser combinators. For example, in this piece of code where I'm
trying to omit the braces from the parse tree:

object Main {

case class P extends JavaTokenParsers {

def test : Parser[Any] = "{" ~> ident ~ ident <~ "}" ~> ident
}

def main(args: Array[String]) {

val p = P()
println(p.parseAll(p.test, "{ A B } C"))
}
}

I would expect the output to be ( (A ~ B), C) (I think), but instead I
just get (A ~ B). Thoughts? Is there a better way to do this kind
of thing?

Thanks!

Dave

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: <~ and ~> in parser combinators
Start throwing parenthesis around and you'll see the problem.  It's a precedence issue, and you're throwing away too much.

("{" ~> (ident ~ ident) <~ "}")  ~ ident

That's most likely what you want.  As I don't remember the precedence rules off hand, I can't remember which <~ or ~> you need to remove.

On Tue, Jan 27, 2009 at 4:56 PM, Dave Ray <daveray [at] gmail [dot] com> wrote:
Hi there. I was wondering if someone could point me to a good
explanation of the semantics of the <~ and ~> operators when using
parser combinators. For example, in this piece of code where I'm
trying to omit the braces from the parse tree:

object Main {

 case class P extends JavaTokenParsers {

   def test : Parser[Any] = "{" ~> ident ~ ident <~ "}" ~> ident
 }

 def main(args: Array[String]) {

   val p = P()
   println(p.parseAll(p.test, "{ A B } C"))
 }
}

I would expect the output to be ( (A ~ B), C) (I think), but instead I
just get  (A ~ B).  Thoughts?  Is there a better way to do this kind
of thing?

Thanks!

Dave

David Biesack
Joined: 2008-11-18,
User offline. Last seen 2 years 38 weeks ago.
Re: <~ and ~> in parser combinators

Dave;

Take a look at http://scala.sygneca.com/libs/parsing
which explains this a bit.

djb

> Date: Tue, 27 Jan 2009 16:56:23 -0500
> From: Dave Ray
>
> Hi there. I was wondering if someone could point me to a good
> explanation of the semantics of the <~ and ~> operators when using
> parser combinators...

Tony Sloane
Joined: 2009-01-07,
User offline. Last seen 2 years 32 weeks ago.
Re: <~ and ~> in parser combinators
According to section 6.12.3 of the Scala spec, ~ and ~> have the same precedence and one that is higher than <~.  All of the operators are left associative. Hence the original expression is equivalent to
(("{" ~> ident) ~ ident) <~ ("}" ~> ident)
which clarifies why the C was being thrown away.
On 28/01/2009, at 9:06 AM, Josh Suereth wrote:
Start throwing parenthesis around and you'll see the problem.  It's a precedence issue, and you're throwing away too much.

("{" ~> (ident ~ ident) <~ "}")  ~ ident

That's most likely what you want.  As I don't remember the precedence rules off hand, I can't remember which <~ or ~> you need to remove.

On Tue, Jan 27, 2009 at 4:56 PM, Dave Ray <daveray [at] gmail [dot] com> wrote:
Hi there. I was wondering if someone could point me to a good
explanation of the semantics of the <~ and ~> operators when using
parser combinators. For example, in this piece of code where I'm
trying to omit the braces from the parse tree:

object Main {

 case class P extends JavaTokenParsers {

   def test : Parser[Any] = "{" ~> ident ~ ident <~ "}" ~> ident
 }

 def main(args: Array[String]) {

   val p = P()
   println(p.parseAll(p.test, "{ A B } C"))
 }
}

I would expect the output to be ( (A ~ B), C) (I think), but instead I
just get  (A ~ B).  Thoughts?  Is there a better way to do this kind
of thing?

Thanks!

Dave


Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland