我對Haskell相當新,我試圖做一個簡單的解析器,並使用Parsec模塊。我的解析器的語法是:如何在解析Haskell時正確區分詞幹字符和文字字符
data Frag
= Lt String
| St
deriving (Eq, Show)
type Template = [Frag]
type FileT = Template
type CommandT = Template
data Rule
= Rule [FileT] [FileT] [CommandT]
deriving (Eq, Show)
type Makefile = [Rule]
我已經實現一路下跌至片段(這將是字符集)。下面是我如何試圖處理文本字符的文檔片斷,我不知道如何處理,雖然乾的字符:
template :: Parser [Frag]
template =
do result <- many frag
return result
frag :: Parser Frag
frag = do Lt x <- (many (noneOf ['\n','\t','\r',':','.']))
return x
但我發現了這個錯誤,我不知道爲什麼:
Parser\Impl.hs:72:11: error:
• Couldn't match expected type ‘[Char]’ with actual type ‘Frag’
• In the pattern: Lt x
In a stmt of a 'do' block:
Lt x <- (many (noneOf ['\n', '\t', '\r', ':', ....]))
In the expression:
do { Lt x <- (many (noneOf ['\n', '\t', ....]));
return x }
Parser\Impl.hs:73:11: error:
• Couldn't match type ‘[Char]’ with ‘Frag’
Expected type: Text.Parsec.Prim.ParsecT
String() Data.Functor.Identity.Identity Frag
Actual type: Text.Parsec.Prim.ParsecT
String() Data.Functor.Identity.Identity String
• In a stmt of a 'do' block: return x
In the expression:
do { Lt x <- (many (noneOf ['\n', '\t', ....]));
return x }
In an equation for ‘frag’:
frag
= do { Lt x <- (many (noneOf ['\n', ....]));
return x }
輸入:
"aaa : bbb ccc"
"\:aaa : \%bbb \\ccc
輸出:
[["aaa"] , ["bbb"] , ["ccc"]]
[[":aaa"] , ["%bbb"] , ["\ccc"]]
什麼是幹字符?你想要解析的語法是什麼?如果你提供了一個示例輸入和你試圖獲得的輸出,這可能會非常有幫助。 –
完成。字符如%:\ n \ t \ r被排除,但是如果字符串包含'\',它會使其成爲字面值(例如「\%」 - >「%」,而「%」 - >「」)。 –