2015-06-13 62 views
1

我寫一個階解析器來解析以下的輸入(x是一個數字): /hdfs://xxx.xx.xx.x:xxxx/path1/file1.jpg+1如何修復使用scala解析器解析路徑中的錯誤?

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~")" 
} 

我我得到以下錯誤:

[1.1] failure: `(' expected but `/' found 

找不到問題!

回答

2

這裏有兩個問題。首先,您嘗試匹配以/hfds開頭的字符串,但您的輸入始於/hdfs。其次,您擁有的正則表達式會嘗試將所有輸入與您放置的錨點(^$)相匹配。這意味着當將使用解析器pathIdent時,它將嘗試匹配所有輸入,直到Reader沒有更多值返回。

在您輸入有.jpg\w+1不匹配+,這就是爲什麼你會得到一個解析失敗。所以你應該刪除它們。

expr解析器運行你的表情,我得到:

[1.43] parsed: ((/hdfs://111.22.33.4:5555/path1/file1.jpg~List())~List((+~(1~List())))) 

這確實是後跟一個長期的重複與+的因素,隨後的因素一個空的重複(/hdfs://111.22.33.4:5555/path1/file1.jpg~List()))這是一個因子(1),然後是空的重複因子(List((+~(1~List()))))。

+0

@Rubbic解析'(/hdfs://111.22.33.4:5555/path1/file1.jpg+1)'適合我。 –

+0

我得到這個錯誤:-bash:語法錯誤附近意想不到的標記'/hdfs://111.33.50.2:8020/path1/path2/file1.jpg+1'我沒有得到它沒有括號 – Rubbic

+0

'(/ hdfs://111.22.33.4:5555/path1/file1.jpg + 1)'是圓括號內的'expr'(因子分析器的第三個選項)。什麼是你想要解析的確切輸入? –