What BNF Variant is This?

444 Views Asked by At

I was looking into making a Bison-based parser using some BNF files I found on Github. My issue, however, is that I cannot seem to determine what variation of BNF the files are using.

Here is a link to a folder containing samples of the BNF variant.

In bnf.rkt, the author references the BNF Wikipedia page, yet his file looks nothing like it:

## The following comes from: http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form
<syntax> : <rule> | <rule> <syntax>
<rule> : <opt-whitespace> "<" <RULE-NAME> ">" <opt-whitespace> "::="
<opt-whitespace> <expression> <line-end>
<opt-whitespace> : " " <opt-whitespace> | "" ## "" is empty string, i.e. no whitespace
<expression> : <list> | <list> "|" <expression>
<line-end> : <opt-whitespace> <EOL> | <line-end> <line-end>
<list> : <term> | <term> <opt-whitespace> <list>
<term> : <literal> | "<" <RULE-NAME> ">"
<literal> : '"' <TEXT> '"' | "'" <TEXT> "'" ## actually, the original BNF did not use quotes

Here is an example (from the repo) of how this syntax is used to represent basic JSON:

;; Simple baby example of JSON structure
json: number | string
| array
| object
number: NUMBER
string: STRING
array: "[" [json ("," json)*] "]"
object: "{" [kvpair ("," kvpair)*] "}"
kvpair: ID ":" json

Does anyone have any idea of what he is using? I would like to convert this into a Bison-readable format, if possible.

Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

It's a kind of extended BNF, similar to that used in various parser generators, roughly based on bison syntax with the addition of brackets and regular expression operators. The documentation is here, and a summary would be:

  • The left-hand-side of a production is followed by :, as in bison.
  • There is no ; at the end of a production (in bison, the ; is optional).
  • * and + are the usual Kleene star and plus operators found in most regex libraries. | is the usual alternation operator. ( and ) are grouping parenthesis, also similar to most regex implementations.
  • However, optional sequences are surrounded with [ and ], similar to EBNF.

Aside from the last point, that makes it essentially the same as the basic format used by ANTLR, for example.