Reputation: 1550
I am writing a scala parser to parse the following input(x is number): /hdfs://xxx.xx.xx.x:xxxx/path1/file1.jpg+1
trait pathIdentifier extends RegexParsers{
def pathIdent: Parser[String] ="""^/hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+)$)""".r
}
class ParseExp extends JavaTokenParsers with pathIdentifier {
def expr: Parser[Any] = term~rep("+"~term | "-"~term)
def term: Parser[Any] = factor~rep("*"~factor | "/"~factor)
def factor: Parser[Any] = pathIdent | floatingPointNumber | "("~expr~")"
}
I am getting following error:
[1.1] failure: `(' expected but `/' found
Can't figure out the problem!
Upvotes: 1
Views: 90
Reputation: 93842
There are two problems here. First you are trying to match a string that starts with /hfds
but your input starts with /hdfs
. Secondly the regular expression you have will try to match the all input with the anchors you put (^
and $
). It means that when the parser pathIdent
will be used, it will try to match all the input until the Reader
has no more values to return.
In your input there is a +1
after .jpg
and \w
does not match +
, that's why you get a parse failure. So you should remove them.
Running your expression with the expr
parser, I get:
[1.43] parsed: ((/hdfs://111.22.33.4:5555/path1/file1.jpg~List())~List((+~(1~List()))))
which is indeed a factor followed by an empty repetition of factors (/hdfs://111.22.33.4:5555/path1/file1.jpg~List())
) followed by a repetition of a term with a +
that is a factor (1
) followed by an empty repetition of factors (List((+~(1~List())))
).
Upvotes: 2