我寫在Haskell詞法分析器。這裏是代碼:哈斯克爾詞法問題
lexer :: String -> [Token]
lexer s
| s =~ whitespace :: Bool =
let token = s =~ whitespace :: String in
lex (drop (length token) s)
| s =~ number :: Bool =
let token = s =~ number :: String in
Val (read token) : lex (drop (length token) s)
| s =~ operator :: Bool =
let token = s =~ operator :: String in
Oper token : lex (drop (length token) s)
| otherwise = error "unrecognized character"
where
whitespace = "^[ \t\n]"
number = "^[0-9]*(\.[0-9]+)?"
operator = "^[+-*/()]"
data Token = Val Int | Oper String
我有兩個問題。一,數量正則表達式"^[0-9]*(\.[0-9]+)?"
拋出這個錯誤:
lexical error in string/character literal at character '['
當我註釋掉包含它和使用它的功能的一部分行,我得到這個錯誤:
Couldn't match expected type `Token' against inferred type `(String, String)' Expected type: [Token] Inferred type: [(String, String)] In the expression: lex (drop (length token) s) In the expression: let token = s =~ whitespace :: String in lex (drop (length token) s)
我有不知道爲什麼我得到這些錯誤。有人能幫我嗎?
感謝。 :D在解析器上! – Micah 2009-10-20 01:40:46