我正在搜索用BFN规则描述的广泛扩展的方言(例如https://github.com/vmeurisse/wildmatch + globstar **)。

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

最佳答案

由于文件路径通配符的语法可以简化为简单的正则表达式,因此我不确定您的问题。该语法由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

10-04 16:08