使用Scala解析器组合器解析基于缩进的语言 [英] Parsing an indentation based language using scala parser combinators

查看:149
本文介绍了使用Scala解析器组合器解析基于缩进的语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方便的方法来使用Scala的解析器组合器来解析缩进很重要的语言? (例如Python)

Is there a convenient way to use Scala's parser combinators to parse languages where indentation is significant? (e.g. Python)

推荐答案

假设我们使用一种非常简单的语言来表示有效程序

Let's assume we have a very simple language where this is a valid program

block
  inside
  the
  block

,我们想将其解析为List[String],其中块内的每一行都为一个String.

and we want to parse this into a List[String] with each line inside the block as one String.

我们首先定义一个方法,该方法采用最小缩进级别,并为具有该缩进级别的行返回解析器.

We first define a method that takes a minimum indentation level and returns a parser for a line with that indentation level.

def line(minIndent:Int):Parser[String] = 
  repN(minIndent + 1,"\\s".r) ~ ".*".r ^^ {case s ~ r => s.mkString + r}

然后,通过在行之间使用合适的分隔符重复行解析器,来定义最小缩进级别的块.

Then we define a block with a minimum indentation level by repeating the line parser with a suitable separator between lines.

def lines(minIndent:Int):Parser[List[String]] =
  rep1sep(line(minIndent), "[\n\r]|(\n\r)".r)

现在,我们可以为我们的小语言定义一个解析器,如下所示:

Now we can define a parser for our little language like this:

val block:Parser[List[String]] =
  (("\\s*".r <~ "block\\n".r) ^^ { _.size }) >> lines

它首先确定当前的缩进级别,然后将其作为最小值传递给行解析器.让我们对其进行测试:

It first determines the current indentation level and then passes that as the minimum to the lines parser. Let's test it:

val s =
"""block
    inside
    the
    block
outside
the
block"""

println(block(new CharSequenceReader(s)))

然后我们得到

[4.10] parsed: List(    inside,     the,     block)

要进行所有编译,需要导入这些文件

For all of this to compile, you need these imports

import scala.util.parsing.combinator.RegexParsers
import scala.util.parsing.input.CharSequenceReader

您需要将所有内容放入扩展RegexParsers的对象中,

And you need to put everything into an object that extends RegexParsers like so

object MyParsers extends RegexParsers {
  override def skipWhitespace = false
  ....

这篇关于使用Scala解析器组合器解析基于缩进的语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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