haskell - Parsec:预测解析

标签 haskell parsec ll

我只有很少的 haskell 技能,我需要有关如何使用 parsec 实现预测性解析 (LL*) 的帮助。

我有上下文无关语法:

<a> ::= identifier | identifier '(' <args> ')'

基于 http://research.microsoft.com/en-us/um/people/daan/download/parsec/parsec.pdf (章节预测解析器)我写了这段代码:

term =  do{ x <- m_identifier
    ; try( char '(' )
    ; b <- argsparser
    ; char ')'
    ; return (FncCall x b)
    }
<|> do { x <- m_identifier
    ; return (VarId x)
    }

我预计此代码会尝试匹配“(”,如果不匹配,解析器将继续并仅匹配标识符。此代码仅适用于匹配标识符“('args')'。

仅在标识符“a”上调用它会抛出:

parse error at (line 1, column 2):
unexpected end of input
expecting letter or digit or "("

最佳答案

所有替代部分都应该在try下,我认为:

term =  try( do{ x <- m_identifier
    ; char '('
    ; b <- argsparser
    ; char ')'
    ; return (FncCall x b)
    } )
<|> do { x <- m_identifier
    ; return (VarId x)
    }

关于haskell - Parsec:预测解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9615493/

相关文章:

haskell - 使用 Typeable 对不同类型进行有效的大小写匹配

parsing - Haskell Parsec,将 oneOf 改编为 [String]

parsing - LL 解析器比 LR 解析器有什么优势?

haskell - 它在从文件加载时有效,但在输入 ghci 时无效。为什么?

haskell - 有条件地派生 Show 以获取在类型构造函数上参数化的存在类型

algorithm - 来自 "Programming Pearls"的方程式 - 有人可以解释一下吗?

haskell - 使用 parsec 获取已解析源中的当前位置

parsing - 在 Haskell 中正确解析 uu-parsinglib 中的行缩进

parsing - 制作语法 LL

c# - 用于删除选择的 ANTLR4 语法集成复杂性