文件路径通配符(glob)的BNF语法定义

Ev_*_*nus 7 glob bnf peg ometa

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

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

Lau*_*RTE 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)'
Run Code Online (Sandbox Code Playgroud)

这可以帮助你编写 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
Run Code Online (Sandbox Code Playgroud)

著名的 ANTLR 工具解析的流行语法列表可以在这里找到:http ://www.antlr3.org/grammar/list.html 。