bir` optional` combinator içindeki many` combinator: İsteğe bağlı kabul eden bir ayrıştırıcı kural (yani potansiyel olarak boşHaskell parsekten: `Ben Haskell'ın parsekten kütüphanesini kullanarak bu dilbilgisi kuralı uygulamak istiyorum
((a | b | c)* (a | b))?
) dize. o acccepts dize boş değilse, o zaman a
b
veya c
ayrıştırıcıları sıfır veya daha fazla oluşumları geçerek tüketilebilir, ancak en dış ?
opsiyonel ayrıştırıcı tarafından kabul dize ayrıştırıcı a
ile yahut tüketilmelidir b
, ancak c
. İşte bir örnek:
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
şöyle görünebilir bir ilk girişim:
myParser = option "" $ do
str <- many (a <|> b <|> c)
ch <- a <|> b
return (str ++ [ch])
Ama many
sadece a <|> b
bırakarak her test durumda tüm 'a' 'b' ve 'c' karakteri tüketir Tüketecek karakter yok.
soru:
parsekten combinators kullanarak, myParser
tanımlamak ((a | b | c)* (a | b))?
doğru uygulanması nedir?
Belki ayrıştırmak (a | b | c) + ve c ile biterse daha sonra reddetmek? –