Scalas/Haskells 解析器组合器足够了吗?

发布于 2024-07-13 01:33:17 字数 324 浏览 7 评论 0原文

我想知道 Scala/Haskells 解析器组合器是否足以解析编程语言。 更具体地说是MiniJava 语言。 我目前正在阅读编译器构造,jflex 和 java cup 工作起来非常痛苦,所以我想知道我是否可以/应该使用解析器组合器。 MiniJava 语法非常小。 MiniJavas BNF: http://www.cambridge.org/us/features/052182060X /grammar.html

I'm wondering if Scalas/Haskells parser combinators are sufficient for parsing a programming language. More specifically the language MiniJava. I'm currently reading compiller construction and jflex and java cup is quite painful to work with so I'm wondering if I could/should use parser combinators instead.
The MiniJava syntax is very small.
MiniJavas BNF: http://www.cambridge.org/us/features/052182060X/grammar.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

つ可否回来 2024-07-20 01:33:17

我从未使用过 Scala,但明确的 BNF 的存在使这变得很容易。

简单地翻译成Haskell的Text.ParserCombinators.Parsec

goal = do c <- mainClass
          cs <- many classDeclaration
          eof
          return $ c:cs
mainClass = do token "class"
               name <- identifier
               ...

等。 PArrows 翻译也非常简单。 您可能会发现在解析器之前有一个不同的词法分析阶段更容易,但您也可以不这样做。

I've never used Scala, but the existence of a definitive BNF makes this easy.

Trivially translated into Haskell's Text.ParserCombinators.Parsec:

goal = do c <- mainClass
          cs <- many classDeclaration
          eof
          return $ c:cs
mainClass = do token "class"
               name <- identifier
               ...

etc. The PArrows translation is pretty trivial too. You'll probably find it easier to have a distinct lexing phase before the parser, but you can do without too.

溺深海 2024-07-20 01:33:17

我正在使用 Scala 的解析器组合器来解析 PL/SQL 代码,它的工作方式就像一个魅力。

I'm using Scala's parser combinators to parse PL/SQL code, it works like a charm.

计㈡愣 2024-07-20 01:33:17

至少 Parsec 具有用于类 Java 语言的内置词法分析器:

lexer = makeTokenParser javaStyle

您必须自己定义保留字。

At least Parsec has built-in lexer for Java-like languages:

lexer = makeTokenParser javaStyle

You have to define the reserved words yourself.

望喜 2024-07-20 01:33:17

Scala 的解析器是一个回溯解析器,因此它几乎可以处理任何 BNF 或 EBNF。 但这也意味着,在某些极端情况下,输入的读取速度可能会非常慢。

如果语法可以更改为 LL(1) 语法,则可以使用 ~! 操作员将回溯保持在最低限度。

该语法可能可以转换为 LL(1),但正如所写,它不是。 例如,请参阅表达式和语句存在第一个/第一个冲突(请在链接文章的末尾查找此内容)。

无论如何,对于一个学术项目来说,这已经足够了。 对于现实生活中的编译器,您需要更快的解析器。

Scala's parser is a backtracking parser, so it can deal with pretty much any BNF or EBNF. It also means, though, that there are edge cases where input can be painfully slow to be read.

If the grammar can be changed into an LL(1) grammar, you can use the ~! operator to keep backtracking to a minimum.

The grammar probably CAN be turned into LL(1), but, as written, it is not. See, for instance, that Expression and Statement have First/First conflicts (look this up at the end of the linked article).

Anyway, for an academic project, it is enough. For real life compiler stuff, you'll need faster parsers.

挽手叙旧 2024-07-20 01:33:17

Scala 编程(第 647 页)说:

它 [Scala 的解析器组合器框架] 比解析器生成器更容易理解和适应,并且速度的差异在实践中通常并不重要,除非您想解析非常大的输入。

由于我不会将源代码归类为非常大的输入(理想情况下),因此它应该足够了。

Programming in Scala (p. 647) says:

It [Scala's parser combinator framework] is much easier to understand and to adapt than a parser generator, and the difference in speed would often not matter in practice, unless you want to parse very large inputs.

As I would not classify source code as very large input (ideally), it should be sufficient.

染墨丶若流云 2024-07-20 01:33:17

我还没有处理过 Scala 或 Haskell 解析器组合器库,但看起来语法应该没问题。

I haven't dealt with the Scala or Haskell parser combinator libraries, but it looks like the grammar should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文