Parsec或开心(使用alex)或uu-parsinglib [英] Parsec or happy (with alex) or uu-parsinglib

查看:149
本文介绍了Parsec或开心(使用alex)或uu-parsinglib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将编写verilog(或vhdl)语言的解析器,并对解析的数据进行大量操作(排序转换)。我打算解析真正的大文件(完整的Verilog设计,大到10K行),我最终将支持大部分的Verilog。我不介意打字,但是我不想重写代码的任何部分,只要我添加对其他规则的支持。

在bash中,推荐?我知道Haskell并且在之前使用过Happy(玩)。我觉得有可能使用Parsec来转换代码中的分析字符串(这非常有帮助)。我没有使用uu-paringlib的经验。



那么为了解析一个完整的verilog / VHDL语法,哪一个是推荐的?我主要关注的是我可以随心所欲地操作分析数据的简便性和正确性。速度不是主要问题。

解决方案

我个人更喜欢在Alex的帮助下Parsec。
$ b

我比较喜欢Parsec,因为1)Parsec是一个库,而Happy是一个程序,如果你使用Happy,然后用Happy编译,你会用另一种语言编写。 2)由于它的monadic接口,Parsec为你提供了上下文敏感的解析能力。您可以使用额外的状态进行上下文相关的解析,然后根据该状态进行检查和决定。或者只是先查看一些解析值,然后决定下一个解析器等(如 a< - parseSomething; if test a then ... do ... )你不需要任何上下文敏感的信息,你可以简单地使用应用的风格,并得到一个像在YACC或类似的工具中实现的实现。



作为Parsec的一个缺点,你永远不会知道你的Parsec解析器是否包含一个左递归,并且解析器会在运行时停滞(因为Parsec基本上是一个自顶向下的递归下降解析器)。你必须找到左边的递归并消除它们。 YACC风格的解析器可以为您提供Parsec无法获得的一些静态保证和信息(如移位/减少冲突,未使用的终端等)。



Alex是强烈推荐在这两种情况下进行练习(我认为如果你决定继续Happy ),你必须使用Alex。因为即使你使用了Parsec,它也真的简化了你的解析器实现,并且也捕获了很多错误(例如:将关键字解析为标识符是我在使用Parsec时没有Alex的一个常见错误,这只是一个例子)。



你可以看看我的在Alex + Parsec中实现的Lua解析器这里是在Parsec中使用Alex生成的令牌



编辑:谢谢 John L 更正。显然你也可以用Happy来做上下文敏感的解析。此外,亚历克斯的兴奋不是必需的,虽然它是建议。


I am going to write a parser of verilog (or vhdl) language and will do a lot of manipulations (sort of transformations) of the parsed data. I intend to parse really big files (full Verilog designs, as big as 10K lines) and I will ultimately support most of the Verilog. I don't mind typing but I don't want to rewrite any part of the code whenever I add support for some other rule.

In Haskell, which library would you recommend? I know Haskell and have used Happy before (to play). I feel that there are possibilities in using Parsec for transforming the parsed string in the code (which is a great plus). I have no experience with uu-paringlib.

So to parse a full-grammar of verilog/VHDL which one of them is recommended? My main concern is the ease and 'correctness' with which I can manipulate the parsed data at my whim. Speed is not a primary concern.

解决方案

I personally prefer Parsec with the help of Alex for lexing.

I prefer Parsec over Happy because 1) Parsec is a library, while Happy is a program and you'll write in a different language if you use Happy and then compile with Happy. 2) Parsec gives you context-sensitive parsing abilities thanks to its monadic interface. You can use extra state for context-sensitive parsing, and then inspect and decide depending on that state. Or just look at some parsed value before and decide on next parsers etc. (like a <- parseSomething; if test a then ... do ...) And when you don't need any context-sensitive information, you can simply use applicative style and get an implementation like implemented in YACC or a similar tool.

As a downside of Parsec, you'll never know if your Parsec parser contains a left recursion, and your parser will get stuck in runtime (because Parsec is basically a top-down recursive-descent parser). You have to find left recursions and eliminate them. YACC-style parsers can give you some static guarantees and information (like shift/reduce conflicts, unused terminals etc.) that you can't get with Parsec.

Alex is highly recommended for lexing in both situations (I think you have to use Alex if you decide to go on with Happy). Because even if you use Parsec, it really simplifies your parser implementation, and catches a great deal of bugs too (for example: parsing a keyword as an identifier was a common bug I did while I was using Parsec without Alex. It's just one example).

You can have a look at my Lua parser implemented in Alex+Parsec And here's the code to use Alex-generated tokens in Parsec.

EDIT: Thanks John L for corrections. Apparently you can do context-sensitive parsing with Happy too. Also, Alex for lexing is not required in Happy, though it's recommended.

这篇关于Parsec或开心(使用alex)或uu-parsinglib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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