如何在Scala解析器组合器中组合Regexp和关键字 [英] How to combine Regexp and keywords in Scala parser combinators

查看:104
本文介绍了如何在Scala解析器组合器中组合Regexp和关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了两种在Scala中构建解析器的方法.

I've seen two approaches to building parsers in Scala.

第一个是从RegexParsers扩展并定义您所获的词汇模式.我看到的问题是,我不太了解它如何处理关键字歧义性.例如,如果我的关键字与ident匹配相同的模式,则它将关键字作为idents处理.

The first is to extends from RegexParsers and define your won lexical patterns. The issue I see with this is that I don't really understand how it deals with keyword ambiguities. For example, if my keyword match the same pattern as idents, then it processes the keywords as idents.

为了解决这个问题,我看过类似这条信息的帖子展示了如何使用StandardTokenParsers指定关键字.但是,我不明白如何指定正则表达式模式!是的,StandardTokenParsers附带了"ident",但它不附带我需要的其他标识符(复杂的浮点数表示形式,特定的字符串文字模式和转义规则等).

To counter that, I've seen posts like this one that show how to use the StandardTokenParsers to specify keywords. But then, I don't understand how to specify the regexp patterns! Yes, StandardTokenParsers comes with "ident" but it doesn't come with the other ones I need (complex floating point number representations, specific string literal patterns and rules for escaping, etc).

如何同时具有指定关键字的能力和使用正则表达式指定标记模式的能力?

How do you get both the ability to specify keywords and the ability to specify token patterns with regular expressions?

推荐答案

我只写了RegexParsers派生的解析器,但是我所做的却是这样的:

I've written only RegexParsers-derived parsers, but what I do is something like this:

val name: Parser[String] = "[A-Z_a-z][A-Z_a-z0-9]*".r

val kwIf: Parser[String]    = "if\\b".r
val kwFor: Parser[String]   = "for\\b".r
val kwWhile: Parser[String] = "while\\b".r

val reserved: Parser[String] = ( kwIf | kwFor | kwWhile )

val identifier: Parser[String] = not(reserved) ~> name

这篇关于如何在Scala解析器组合器中组合Regexp和关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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