[英]Parser Combinators - Simple grammar
I am trying to use parser combinators in Scala on a simple grammar that I have copied from a book. 我试图在Scala中使用我从书中复制的简单语法中的解析器组合器。 When I run the following code it stops immediately after the first token has been parsed with the error
当我运行以下代码时,它会在解析第一个令牌并出现错误后立即停止
[1.3] failure: string matching regex '\\z' expected but '+' found
I can see why things go wrong. 我明白了为什么出问题了。 The first token is an expression and therefor it is the only thing that needs to be parsed according to the grammar.
第一个标记是一个表达式,因此它是唯一需要根据语法进行解析的东西。 However I do not know what is a good way to fix it.
但是,我不知道什么是修复它的好方法。
object SimpleParser extends RegexParsers
{
def Name = """[a-zA-Z]+""".r
def Int = """[0-9]+""".r
def Main:Parser[Any] = Expr
def Expr:Parser[Any] =
(
Term
| Term <~ "+" ~> Expr
| Term <~ "-" ~> Expr
)
def Term:Parser[Any] =
(
Factor
| Factor <~ "*" ~> Term
)
def Factor:Parser[Any] =
(
Name
| Int
| "-" ~> Int
| "(" ~> Expr <~ ")"
| "let" ~> Name <~ "=" ~> Expr <~ "in" ~> Expr <~ "end"
)
def main(args: Array[String])
{
var input = "2 + 2"
println(input)
println(parseAll(Main, input))
}
}
Factor <~ "*" ~> Term
means Factor.<~("*" ~> Term)
, so the whole right part is dropped. Factor <~ "*" ~> Term
表示Factor.<~("*" ~> Term)
,因此删除了整个右侧部分。 Use Factor ~ "*" ~ Term ^^ { case f ~ _ ~ t => ??? }
使用
Factor ~ "*" ~ Term ^^ { case f ~ _ ~ t => ??? }
Factor ~ "*" ~ Term ^^ { case f ~ _ ~ t => ??? }
or rep1sep
: Factor ~ "*" ~ Term ^^ { case f ~ _ ~ t => ??? }
或rep1sep
:
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.util.parsing.combinator.RegexParsers
object SimpleParser extends RegexParsers
{
def Name = """[a-zA-Z]+""".r
def Int = """[0-9]+""".r
def Main:Parser[Any] = Expr
def Expr:Parser[Any] = rep1sep(Term, "+" | "-")
def Term:Parser[Any] = rep1sep(Factor, "*")
def Factor:Parser[Any] =
(
"let" ~> Name ~ "=" ~ Expr ~ "in" ~ Expr <~ "end" ^^ { case n ~ _ ~ e1 ~ _ ~ e2 => (n, e1, e2)
| Int
| "-" ~> Int
| "(" ~> Expr <~ ")"
| Name }
)
}
SimpleParser.parseAll(SimpleParser.Main, "2 + 2")
// Exiting paste mode, now interpreting.
import scala.util.parsing.combinator.RegexParsers
defined module SimpleParser
res1: SimpleParser.ParseResult[Any] = [1.6] parsed: List(List(2), List(2))
The second part of parser def Term:Parser[Any] = Factor | Factor <~ "*" ~> Term
解析器定义的第二部分
def Term:Parser[Any] = Factor | Factor <~ "*" ~> Term
def Term:Parser[Any] = Factor | Factor <~ "*" ~> Term
is useless. def Term:Parser[Any] = Factor | Factor <~ "*" ~> Term
无用。 The first part, Factor
, can parse (with non-empty next
) any Input
that the second part, Factor <~ "*" ~> Term
, is able to parse. 第一部分,
Factor
,可以解析( next
非空)第二部分, Factor <~ "*" ~> Term
可以解析的任何Input
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.