Parsec:解析具有相同定界符的列表列表 [英] Parsec: Parsing a list of lists, both with the same delimiter

查看:66
本文介绍了Parsec:解析具有相同定界符的列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一种简单的语言,该语言是用空格分隔的命令的列表.每个命令均以单个字母作为命令名称,并使用一系列以空格分隔的数字作为其参数.例如 a 1 2 3 b 4 5 6 d 7 e f 表示以下命令:

Consider a simple language that's a list of space-delimited commands. Each command takes a single letter as the command name, and a series of space-delimited numbers as its arguments; e.g. a 1 2 3 b 4 5 6 d 7 e f would represent the following commands:

  • a 1 2 3
  • b 4 5 6
  • d 7
  • e
  • f
  • a 1 2 3
  • b 4 5 6
  • d 7
  • e
  • f

问题在于它不断通过 sepBy 抓取物品,并到达另一个数字,但到达"b"时失败.

The problem is that it keeps grabbing items through sepBy, and reaches for another number but fails when it gets to "b".

但是,通过下面的代码传递时会产生以下错误:

However, this yields the following error when passed through the code below:

Left "error" (line 1, column 5):
unexpected "b"
expecting space

我现在意识到这很可能是由于我查看问题的方式而引起的,并且希望有人指出如何解析这样的内容.

I realize now that this is likely coming from an error in the way I'm looking at the problem, and would appreciate someone pointing out how to parse something like this.

module Main where

import Control.Applicative hiding (many, optional, (<|>))
import Text.ParserCombinators.Parsec
import Numeric (readSigned, readFloat)

commands = command `sepBy` spaces

command = do
  l <- letter
  ns <- number `sepBy` spaces
  return (l, ns)

main :: IO ()
main = print $ show $ parse commands "error" "a 1 2 3 b 4 5 6 d 7 e f"

number :: CharParser () Double
number = do s <- getInput
            case readSigned readFloat s of
              [(n, s')] -> n <$ setInput s'
              _         -> empty

推荐答案

尝试在每个非空格标记之后使用空格.例如

Try consuming the spaces after each non-space token. E.g.

commands = many (command <* spaces)

command = do
  l <- letter
  spaces
  ns <- many (number <* spaces)
  return (l, ns)

command = (,) <$> (letter <* spaces) <*> many (number <* spaces)

主要思想是:解析某些内容后,下一个字符应为非空格,开始下一个要解析的项.

The main idea is: after you parse something, the next character should be a non-space starting the next item to be parsed.

这篇关于Parsec:解析具有相同定界符的列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆