Kahn Cse
Kahn Cse

Reputation: 437

Scala --- Error when use "[" "]" in parser

This is my parser

def assstm: Parser[Any] = indexop ~ ":=" ~ expression ~ ";"
def indexop: Parser[Any] = ident ~ "[" ~ expression ~ "]"

There is an error with myArray[5] := 5

``('' expected but `[' found

But with this parser

def assstm: Parser[Any] = indexop ~ ":=" ~ expression ~ ";"
def indexop: Parser[Any] = "[" ~ expression ~ "]"

And I test with [5] := 5, there is no error.

What is happening?

Upvotes: 0

Views: 82

Answers (1)

Julien Richard-Foy
Julien Richard-Foy

Reputation: 9663

We need more pieces of your grammar to fully answer your questions. However, it looks like a backtracking issue: you may have defined a rule which tests several alternatives, each using ident: one where ident must be followed by parens, and your assstm rule.

The rule using ident and parens may raise an error (instead of a failure), aborting the run of your parser.

Upvotes: 1

Related Questions