9

Suppose I have an ambiguous language expressed in combinator parser. Is there a way to make certain expressions locally greedy? Here's an example of what I mean.

import scala.util.parsing.combinator._

object Example extends JavaTokenParsers {
  def obj: Parser[Any] = (shortchain | longchain) ~ anyrep

  def longchain: Parser[Any] = zero~zero~one~one
  def shortchain: Parser[Any] = zero~zero

  def anyrep: Parser[Any] = rep(any)
  def any: Parser[Any] = zero | one
  def zero: Parser[Any] = "0"
  def one: Parser[Any] = "1"
  def main(args: Array[String]) {
    println(parseAll(obj, args(0) ))
  }
}

After compiling, I can run it as follows:

$ scala Example 001111
[1.7] parsed: ((0~0)~List(1, 1, 1, 1))

I would like to somehow instruct the first part of obj to be locally greedy and match with longchain. If I switch the order around, it matches the longchain, but that's not because of the greediness.

def obj: Parser[Any] = (longchain | shortchain) ~ anyrep
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • 1
    The notion of "greediness" only applies to closure (`rep` or the postifx `*` operator) and in your grammar that's only applied to the the part following `(longchain | shortchain)` or `(shortchain | longchain)`. – Randall Schulz Apr 05 '10 at 03:56

1 Answers1

14

Use |||:

object Example extends JavaTokenParsers {
  def obj: Parser[Any] = (shortchain ||| longchain) ~ anyrep

  def longchain: Parser[Any] = zero~zero~one~one
  def shortchain: Parser[Any] = zero~zero

  def anyrep: Parser[Any] = rep(any)
  def any: Parser[Any] = zero | one
  def zero: Parser[Any] = "0"
  def one: Parser[Any] = "1"
  def main(args: Array[String]) {
    println(parseAll(obj, args(0) ))
  }
}

scala> Example.main(Array("001111"))
[1.7] parsed: ((((0~0)~1)~1)~List(1, 1))
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681