7

我正在寻找一些用 BFN 规则描述的广泛扩展的方言(比如https://github.com/vmeurisse/wildmatch + globstar **)。

任何格式或语言。OMeta 或 PEG 会很棒。

4

1 回答 1

2

我不确定是否理解您的问题,因为文件路径通配符的语法可以简化为简单的正则表达式。该语法由 Unix Shell 定义。

您可以在此处找到 Bash 的 BNF:http: //my.safaribooksonline.com/book/operating-systems-and-server-administration/unix/1565923472/syntax/lbs.appd.div.3

在 Python 编程语言中,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)'

这可以帮助您编写 de 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 。

于 2014-09-19T20:25:02.933 回答