EBNF_variant.ebnf 2.03 KB
Newer Older
1
2
3
4
5
6
7
# EBNF-Grammar in EBNF

@ comment    =  /#.*(?:\n|$)/                    # comments start with '#' and eat all chars up to and including '\n'
@ whitespace =  /\s*/                            # whitespace includes linefeed
@ literalws  =  right                            # trailing whitespace of literals will be ignored tacitly

syntax     =  [~//] { definition | directive } §EOF
8
9
definition =  symbol §"=" §expression
directive  =  "@" §symbol §"=" §( regexp | literal | list_ )
10
11
12
13
14
15
16
17
18
19
20

expression =  term { "|" term }
term       =  { factor }+
factor     =  [flowmarker] [retrieveop] symbol !"="   # negative lookahead to be sure it's not a definition
            | [flowmarker] literal
            | [flowmarker] regexp
            | [flowmarker] group
            | [flowmarker] oneormore
            | repetition
            | option

21
22
flowmarker =  "!"  | "&"  | "§"                  # '!' negative lookahead, '&' positive lookahead, '§' required
            | "-!" | "-&"                        # '-' negative lookbehind, '-&' positive lookbehind
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
retrieveop =  "::" | ":"                         # '::' pop, ':' retrieve

group      =  "(" expression §")"
oneormore  =  "{" expression "}+"
repetition =  "{" expression §"}"
option     =  "[" expression §"]"

symbol     =  /(?!\d)\w+/~                       # e.g. expression, factor, parameter_list
literal    =  /"(?:[^"]|\\")*?"/~                # e.g. "(", '+', 'while'
            | /'(?:[^']|\\')*?'/~                # whitespace following literals will be ignored tacitly.
regexp     =  /~?\/(?:\\\/|[^\/])*?\/~?/~        # e.g. /\w+/, ~/#.*(?:\n|$)/~
                                                 # '~' is a whitespace-marker, if present leading or trailing
                                                 # whitespace of a regular expression will be ignored tacitly.
list_      =  /\w+/~ { "," /\w+/~ }              # comma separated list of symbols, e.g. BEGIN_LIST, END_LIST,
                                                 # BEGIN_QUOTE, END_QUOTE ; see CommonMark/markdown.py for an exmaple
EOF =  !/./