【问题标题】:What BNF Variant is This?这是什么 BNF 变体?
【发布时间】:2014-11-17 01:25:49
【问题描述】:

我正在考虑使用我在 Github 上找到的一些 BNF 文件来制作基于 Bison 的解析器。然而,我的问题是我似乎无法确定文件使用的 BNF 变体。

Here is a link 到包含 BNF 变体样本的文件夹。

bnf.rkt 中,作者引用了BNF Wikipedia page,但他的文件看起来一点也不像:

## 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

以下是如何使用此语法表示基本 JSON 的示例 (from the repo):

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

有人知道他在用什么吗?如果可能的话,我想把它转换成 Bison 可读的格式。

提前致谢。

【问题讨论】:

    标签: bison bnf


    【解决方案1】:

    它是一种扩展的BNF,类似于各种解析器生成器中使用的BNF,大致基于bison语法并添加了括号和正则表达式运算符。文档为here,摘要为:

    • 产生式的左侧后跟 :,如野牛。
    • 产生式末尾没有 ;(在野牛中,; 是可选的)。
    • *+ 是大多数正则表达式库中常见的 Kleene 星号和加号运算符。 | 是常用的交替运算符。 () 是分组括号,也类似于大多数正则表达式实现。
    • 但是,可选序列被 [] 包围,类似于 EBNF。

    除了最后一点,这使得它与例如 ANTLR 使用的基本格式基本相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      相关资源
      最近更新 更多