Problem with JavaTokenParsers...

39 views
Skip to first unread message

Mattias Wallman

unread,
Nov 8, 2012, 12:28:53 PM11/8/12
to scala...@googlegroups.com
I have some problems with a simple parser that basically is defined as:

object MyParser extends JavaTokenParsers {

  def test: Parser[String] = string | decimalNumber

  def string: Parser[String] = """[\w-]+"""


  def main(args: Array[String]) {

    println(parseAll(test, "3e9ad088-5d08-4e61-9864-a2a56db93db3"))

    println(parseAll(test, "123.33"))

  }

}


This will produce the output 


[1.37] parsed: 3e9ad088-5d08-4e61-9864-a2a56db93db3

[1.4] failure: string matching regex `\z' expected but `.' found


123.33

   ^


Then I tried to modify  the test rule:

object MyParser extends JavaTokenParsers {

  def test: Parser[String] =   decimalNumber | string

  def string: Parser[String] = """[\w-]+""".r


  def main(args: Array[String]) {

    println(parseAll(test, "3e9ad088-5d08-4e61-9864-a2a56db93db3"))

    println(parseAll(test, "123.33"))

  }


}


This will produce the output:

[1.2] failure: string matching regex `\z' expected but `e' found


3e9ad088-5d08-4e61-9864-a2a56db93db3

 ^

[1.7] parsed: 123.33


So, the question is: why does it matter in what order the decimalNumber and string rule is declared in the test rule, and how can I do to fix this little problem, so I can parse my decimal number and string properly?

Regards
Mattias

Daniel Sobral

unread,
Nov 8, 2012, 12:34:22 PM11/8/12
to Mattias Wallman, scala-user
The | operator is not backtracking: if the left side succeeds, the right side will never be tried. If something fails latter on, there's no going back.

You may use ||| to pick the longest match instead, but, generally speaking, you should strive to avoid such ambiguities.
--
Daniel C. Sobral

I travel to the future all the time.

Mattias Wallman

unread,
Nov 8, 2012, 12:45:01 PM11/8/12
to scala...@googlegroups.com, Mattias Wallman
Ok, the ||| seems to work, thank you!

But if that is an undesirable construction, is there a better alternative?

Daniel Sobral

unread,
Nov 8, 2012, 1:01:40 PM11/8/12
to Mattias Wallman, scala-user
On Thu, Nov 8, 2012 at 3:45 PM, Mattias Wallman <mattias...@gmail.com> wrote:
Ok, the ||| seems to work, thank you!

But if that is an undesirable construction, is there a better alternative?

That can only be answered by looking at the whole grammar. 
Reply all
Reply to author
Forward
0 new messages