我正在开始使用 Haskell,我正在尝试使用 Alex 工具来创建正则表达式,我我有点失落;我的第一个不便是编译部分。我必须如何使用 Alex 编译文件?然后,我认为我必须将 alex 生成的模块导入到我的代码中,但不确定。如果有人可以帮助我,我将非常感激!
I'm getting started with Haskell and I'm trying to use the Alex tool to create regular expressions and I'm a little bit lost; my first inconvenience was the compile part. How I have to do to compile a file with Alex?. Then, I think that I have to import into my code the modules that alex generates, but not sure. If someone can help me, I would be very greatful!
发布评论
评论(3)
您可以在 Alex 中指定正则表达式函数。
例如,Alex 中的正则表达式用于匹配浮点数:
当我们匹配浮点数时,我们分派一个解析函数来对捕获的字符串进行操作,然后我们可以将其包装并作为解析函数公开给用户:
使用 Alex 进行正则表达式匹配的一个很好的结果是性能良好,因为正则表达式引擎是静态编译的。它也可以作为用 cabal 构建的常规 Haskell 库公开。有关完整实现,请参阅 bytestring-lexing。
关于何时使用词法分析器而不是正则表达式匹配器的一般建议是,如果您有要匹配的词素的语法,就像我对浮点所做的那样,请使用 Alex。如果不这样做,并且结构更加临时,请使用正则表达式引擎。
You can specify regular expression functions in Alex.
Here for example, a regex in Alex to match floating point numbers:
When we match the floating point number, we dispatch to a parsing function to operate on that captured string, which we can then wrap and expose to the user as a parsing function:
A nice consequence of using Alex for this regex matching is that the performance is good, as the regex engine is compiled statically. It can also be exposed as a regular Haskell library built with cabal. For the full implementation, see bytestring-lexing.
The general advice on when to use a lexer instead of a regex matcher would be that, if you have a grammar for the lexemes you're trying to match, as I did for floating point, use Alex. If you don't, and the structure is more ad hoc, use a regex engine.
为什么要使用 alex 创建正则表达式?
如果您只想进行一些正则表达式匹配等,您应该查看 regex-base 包。
Why do you want to use alex to create regular expressions?
If all you want is to do some regex matching etc, you should look at the regex-base package.
如果您想要的是纯正则表达式,则 API 在 text.regex.base。然后是实现 text.regex.Posix , text.regex.pcre 和其他几个。 Haddoc 文档有点薄弱,但是基本知识在 现实世界 Haskell,第 8 章。 在此 所以问题。
If it is plain Regex you want, the API is specified in text.regex.base. Then there are the implementations text.regex.Posix , text.regex.pcre and several others. The Haddoc documentation is a bit slim, however the basics are described in Real World Haskell, chapter 8. Some more indepth stuff is descriped in this SO question.