如何解决与从正则表达式创建解析器相关的错误? [英] How to solve an error related to creating parser from regex?

查看:90
本文介绍了如何解决与从正则表达式创建解析器相关的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Scala中的StandardTokenParsers编写解析器.需要创建一个正则表达式解析器来解析路径.我已经测试过正则表达式可以正常工作,但是将其发送给函数进行解析,程序给出了一个错误,我无法弄清楚!与该解析器相关的部分代码如下:

I am writing a parser using StandardTokenParsers in Scala. Need to create a regex parser to parse a path. I have tested the regex works fine but sending it to a function to parse it, the program gives an error that I am not able to figure it out! a part of code that is related to this parser is as follow:

 class InfixToPostfix extends StandardTokenParsers {
 import scala.util.matching.Regex
 import lexical.StringLit
//parsing the path
 def regexStringLit(r: Regex): Parser[String] =
 acceptMatch( "string literal matching regex " + r,{ case  StringLit(s) if r.unapplySeq(s).isDefined => s })
// Regex for path
 val pathIdent ="""/hdfs://[\d.]+:\d+/[\w/]+/\w+([.+]\w+)+""".r   
 def pathIdente: Parser[String] =regexStringLit(pathIdent)

 lexical.delimiters ++= List("+","-","*","/", "^","(",")",",")
 def value :Parser[Expr] = numericLit ^^ { s => Number(s) }
 def variable:Parser[Expr] =  pathIdente ^^ { s => Variable(s) }
 def parens:Parser[Expr] = "(" ~> expr <~ ")"

 def argument:Parser[Expr] = expr <~ (","?)
 def func:Parser[Expr] = ( pathIdente ~ "(" ~ (argument+) ~ ")" ^^ { case f ~ _ ~ e ~ _ => Function(f, e) })
//and the rest of the code ....

此解析器将解析算术运算.我使用args(0)将输入发送到以下程序: "/hdfs://111.33.55.2:8888/folder1/p.a3d+1"

This parser is going to parse arithmetic operations. I use args(0) to send my input to the program which is : "/hdfs://111.33.55.2:8888/folder1/p.a3d+1"

,我收到以下错误消息:

and I get the following error:

[1.1] failure: string literal matching regex /hdfs://([\d\.]+):(\d+)/([\w/]+/(\w+\.\w+)) expected

 /hdfs://111.33.55.2:8888/folder1/p.a3d
 ^

不知道如何解决!

仅供参考:"+ 1"部分将由代码中的解析器处理,因此"pathIdent"部分仅用于路径,而这是造成麻烦的部分.这也很好:

FYI: The part for "+1" is going to handle by the parser in the code so the part "pathIdent" is only for the path and that is the part causing the trouble. This is also good :

  """/hdfs://\d+(\.\d+){3}:\d+(/(\w+([.+]\w+)*))+""".r 

在regexpal.com中对其进行检查的代码之外均能正常工作 但在程序内部使用它仍然会出现相同的错误.

it works fine outside of the code checking it in : regexpal.com but still same error using it inside the program.

我想知道StringLit是否不包含某些字符并导致错误.除了StringLit之外,还有其他我可以在这里使用的东西吗?

I am wondering if StringLit is the one that doesn't contain some of the characters and causing the error. Is there anything else other than StringLit that I can use here?

推荐答案

匹配失败是因为匹配者贪婪.这是几种语言中的正则表达式匹配(以及词法分析)的常见问题.

The failure to match will be because the matcher is greedy. This is a common problem with regular expression matching (and hence lexical analysis) in several languages.

贪婪的匹配会在表达式的结尾抓住您.

The greedy matching catches you at the end of the expression.

您有([\w/]+/(\w+\.\w+)),但这将无法匹配,因为与 word p与输入文本folder1/p表示的\w匹配的词被([\w/]+吞没了.它在周期.处停止.因此,在点之前没有任何单词允许(\w+\.\w+)匹配.

You have ([\w/]+/(\w+\.\w+)) but this will fail to match because the word p matched by the \w represented by the input text folder1/p is swallowed up by the piece ([\w/]+. It stops at the period .. There is therefore no word before the dot to permit (\w+\.\w+) to ever match.

您必须重新考虑正则表达式,并使每个路径片段以固相线/终止,而不是使其成为集合的一部分.

You'll have to rethink your regular expression and make each path fragment terminate at a solidus / rather than make it part of a set.

看到了吗?

要进行这项工作,您需要通过以下方式表达:

To make this work you need to express in the following way:

"""/hdfs://[\d.]+:\d+/(\w/)+\w+([.+]\w+)+""".r 

我将[\w/]+/替换为(\w/)+的位置.现在,这指定了单词和斜杠的顺序,并留下了一个不匹配的单词,以使以下模式成功.

Where I replaced [\w/]+/ by (\w/)+. This now specifies the ordering of the words and slashes and leaves a word unmatched for the following pattern to succeed.

这篇关于如何解决与从正则表达式创建解析器相关的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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