如何在Haskell

时间:2016-05-19 15:37:06

标签: parsing haskell

我是Haskell的初学者,我需要解析一系列由某些东西组成的价值。

使用tokenparser生成以下解析器: m_semiSep1 p解析并返回一个以分号分隔的一个或多个p的序列。

但我不太明白它是如何创造的。我需要一个返回逗号分隔的p`s序列。你能给我一个暗示我该怎么办?我还需要解析一系列“= |”分隔值,但我想它将与逗号相同。这是我正在处理的代码:

def = emptyDef{ commentStart = "{-"
          , commentEnd = "-}"
          , identStart = letter
          , identLetter = alphaNum
          , opStart = oneOf "^~&=:-|,"
          , opLetter = oneOf "^~&=:-|,"
          , reservedOpNames = ["~", "&", "^", ":=", "|-", ","]
          , reservedNames = ["true", "false", "nop",
                             "if", "then", "else", "fi",
                             "while", "do", "od"]
          }
TokenParser{ parens = m_parens
       , identifier = m_identifier
       , reservedOp = m_reservedOp
       , reserved = m_reserved
       , semiSep1 = m_semiSep1
       , whiteSpace = m_whiteSpace } = makeTokenParser def

1 个答案:

答案 0 :(得分:0)

您可以在parsec中使用sepBysepBy cell deli解析cell deli cell deli...

之类的内容

例如:

Prelude> :m Text.ParserCombinators.Parsec
Prelude Text.ParserCombinators.Parsec> let csv = (many letter) `sepBy` (char ',') :: Parser [String]
Prelude Text.ParserCombinators.Parsec> parse csv "" "xx,yy,zz"
Right ["xx","yy","zz"]
  1. https://hackage.haskell.org/package/parsec-3.1.9/docs/Text-Parsec-Combinator.html#v:sepBy