加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

解析 – 在scala解析器组合中回溯?

发布时间:2020-12-16 09:56:20 所属栏目:安全 来源:网络整理
导读:似乎 scala的解析器组合器不会回溯.我有一个语法(见底部),无法正确解析以下“stmt”: copy in to out . 这应该很容易用回溯解析: stmt: (to out(copy in)) 还是我错过了什么? 分析器: type ExprP = Parser[Expr]type ValueP = Parser[ValExpr]type CallP
似乎 scala的解析器组合器不会回溯.我有一个语法(见底部),无法正确解析以下“stmt”:

copy in to out .

这应该很容易用回溯解析:

stmt: (to out(copy in))

还是我错过了什么?

分析器:

type ExprP = Parser[Expr]
type ValueP = Parser[ValExpr]
type CallP = Parser[Call]
type ArgsP = Parser[Seq[Expr]]

val ident     = "[a-zA-Z+-*/%><\=]+".r
val sqstart   = "["                          .r
val sqend     = "]"                          .r
val del       = ","                            .r
val end       = "."                          .r

def stmt: ExprP      = expr <~ end
def expr: ExprP      = ucall | call | value
def value: ValueP    = ident ^^ {str => IdentExpr(str)}
def call: CallP      = (args ~ ident ~ expr) ^^ {case args ~ method ~ upon => Call(args,method,upon)}
def ucall: CallP     = (ident ~ expr) ^^ {case method ~ upon => Call(Seq(),upon)}
def args: ArgsP      = advargs | smplargs
def smplargs: ArgsP  = expr ^^ {e => Seq(e)}
def advargs: ArgsP   = (sqstart ~> repsep(expr,del) <~ sqend) ^^ {seq => seq}

解决方法

你想在2.8中使用PackratParsers.我认为packrat解析器是唯一的回溯解析器.

编辑:截至2015年中期,您应该使用fastparse.它不仅更快,而且更易于使用(特别是在从解析构建数据结构时).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读