Haskell 标识符识别
我使用 Haskell 进行文件解析,并且同时使用 Data.Attoparsec.Char8
和 Data.ByteString.Char8
。我想解析一个可以包含以下符号的表达式:- / [ ] _ .(减号、斜杠、括号和下划线)。
我已经编写了以下解析器
import qualified Data.ByteString.Char8 as B
import qualified Data.Attoparsec.Char8 as A
identifier' :: Parser B.ByteString
identifier' = A.takeWhile $ A.inClass "A-Za-z0-9_//- /[/]"
...但它没有按预期工作。
ghc> A.parse identifier' (B.pack "EMBXSHM-PortClo")
Done "-PortClo" "EMBXSHM"
ghc> A.parse identifier' (B.pack "AU_D[1].PCMPTask")
Done ".PCMPTask" "AU_D[1]"
有人可以帮助我吗?
感谢您抽出时间。
my working in file parsing using Haskell, and I'm using both Data.Attoparsec.Char8
and Data.ByteString.Char8
. I want to parse an expression which can contains symbols like : - / [ ] _ . (minus, slashes, braquets and underscore).
I've write the following parser
import qualified Data.ByteString.Char8 as B
import qualified Data.Attoparsec.Char8 as A
identifier' :: Parser B.ByteString
identifier' = A.takeWhile $ A.inClass "A-Za-z0-9_//- /[/]"
... but it's not works like expected.
ghc> A.parse identifier' (B.pack "EMBXSHM-PortClo")
Done "-PortClo" "EMBXSHM"
ghc> A.parse identifier' (B.pack "AU_D[1].PCMPTask")
Done ".PCMPTask" "AU_D[1]"
can someone help me.
Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看文档: http://hackage.haskell.org/packages/archive/attoparsec/0.10.1.0/doc/html/Data-Attoparsec-ByteString-Char8.html#g:9
添加“-”到一个集合,将其放置在字符串的开头或结尾。
后者不会解析,因为您的类列表中没有点。
Take a look at the documentation: http://hackage.haskell.org/packages/archive/attoparsec/0.10.1.0/doc/html/Data-Attoparsec-ByteString-Char8.html#g:9
To add a "-" to a set, place it a the beginning or end of a string.
The latter doesn't parse because you don't have dots in your class listing.
您希望在标识符中允许使用
'-'
字符,但A.inClass
使用'-'
作为范围。您必须将其放在范围字符串的开头或结尾:You want to allow
'-'
characters in identifiers, butA.inClass
uses'-'
for ranges. You have to put it at the start or end of the range string: