` optional`コンビネータ内部many`コンビネータ:オプションを受け入れパーサー規則は(すなわち潜在的に空であるHaskellのパーセク: `私はHaskellのパーセクライブラリ使用して、この文法規則を実装したいと思います
((a | b | c)* (a | b))?
を)文字列。それはacccepts文字列が空でない場合、それはa
b
またはc
パーサの0回以上の繰り返しを通過することによって消費することができますが、最も外側の?
オプションのパーサによって受け入れられた文字列がパーサa
するかのどちらかに消費されなければなりませんb
であるが、c
ではない。ここでは例です:
module Main where
import Text.Parsec
import Text.Parsec.Text
a,b,c :: GenParser() Char
a = char 'a'
b = char 'b'
c = char 'c'
-- ((a | b | c)* (a | b))?
myParser = undefined
shouldParse1,shouldParse2,shouldParse3,
shouldParse4,shouldFail :: Either ParseError String
-- these should succeed
shouldParse1 = runParser myParser() "" "" -- because ? optional
shouldParse2 = runParser myParser() "" "b"
shouldParse3 = runParser myParser() "" "ccccccb"
shouldParse4 = runParser myParser() "" "aabccab"
-- this should fail because it ends with a 'c'
shouldFail = runParser myParser() "" "aabccac"
main = do
print shouldParse1
print shouldParse2
print shouldParse3
print shouldParse4
print shouldFail
次のようになります。最初の試み:
myParser = option "" $ do
str <- many (a <|> b <|> c)
ch <- a <|> b
return (str ++ [ch])
しかしmany
はちょうどa <|> b
を残して、各テストケース内のすべての「」「B」と「C」の文字を消費消費する文字はありません。
質問:パーセクコンビネータを使用して
、myParser
を定義する((a | b | c)* (a | b))?
の正しい実装は何ですか?
はたぶんパース(| B | C) +それがcで終わったら後で拒否しますか? –