Defining a BNF grammar for a file path template (glob)

I am looking for a widespread dialect (e.g. https://github.com/vmeurisse/wildmatch + globstar **) described by the BFN rules.

In any format or language. OMeta or PEG would be great.

+7
ometa glob bnf peg
source share
1 answer

I am not sure to understand your question, since the grammar for the file path template can be reduced to a simple regular expression. This grammar is defined by the Unix shell.

You can find the BNF for Bash here: http://my.safaribooksonline.com/book/operating-systems-and-server-administration/unix/1565923472/syntax/lbs.appd.div.3

In the Python programming language, the documentation has a definition of the glob.glob() function. This function uses the fnmatch.fnmatch() function to perform pattern matching. Documentation is available here: https://docs.python.org/2/library/fnmatch.html#fnmatch.fnmatch .

The fnmatch.fnmatch function translates the pattern template of the path to the classic regular expression, for example:

 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)' 

This will help you write a BNF grammar ...

EDIT

Here is a very simple grammar:

 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 

A list of popular grammars analyzed by the famous ANTLR tool is available here: http://www.antlr3.org/grammar/list.html .

+2
source share

All Articles