我正在搜索一些用BFN规则描述的广泛扩展的方言(比如这个https://github.com/vmeurisse/wildmatch + globstar **)。
以任何形式或语言。OMeta或聚乙二醇会很好。
发布于 2014-09-19 20:25:02
我不确定是否理解您的问题,因为文件路径通配符的语法可以简化为一个简单的正则表达式。这个语法是由Unix定义的。
您可以在这里找到Bash的BNF:http://my.safaribooksonline.com/book/operating-systems-and-server-administration/unix/1565923472/syntax/lbs.appd.div.3
在programming语言中,glob.glob()函数的定义可以在文档中找到。该函数使用fnmatch.fnmatch()函数执行模式匹配。文档可以在这里获得:https://docs.python.org/2/library/fnmatch.html#fnmatch.fnmatch。
fnmatch.fnmatch函数将文件路径通配符模式转换为典型的正则表达式,如下所示:
def translate(pat):
"""Translate a shell PATTERN to a regular expression.
There is no way to quote meta-characters.
"""
i, n = 0, len(pat)
res = ''
while i < n:
c = pat[i]
i = i+1
if c == '*':
res = res + '.*'
elif c == '?':
res = res + '.'
elif c == '[':
j = i
if j < n and pat[j] == '!':
j = j+1
if j < n and pat[j] == ']':
j = j+1
while j < n and pat[j] != ']':
j = j+1
if j >= n:
res = res + '\\['
else:
stuff = pat[i:j].replace('\\','\\\\')
i = j+1
if stuff[0] == '!':
stuff = '^' + stuff[1:]
elif stuff[0] == '^':
stuff = '\\' + stuff
res = '%s[%s]' % (res, stuff)
else:
res = res + re.escape(c)
return res + '\Z(?ms)'能帮你写德BNF语法..。
编辑
这里有一个非常简单的语法:
wildcard : expr
| expr wildcard
expr : WORD
| ASTERIX
| QUESTION
| neg_bracket_expr
| pos_bracket_expr
pos_bracket_expr : LBRACKET WORD RBRACKET
neg_bracket_expr : LBRACKET EXCLAMATION WORD RBRACKET由著名的ANTLR工具解析的流行语法列表可在这里获得:http://www.antlr3.org/grammar/list.html。
https://stackoverflow.com/questions/25711465
复制相似问题