将 EBNF 语法转换为 PEG

use*_*349 5 php parsing bnf ebnf

我有一个 EBNF 语法,想将其转换为 PEG ( https://github.com/anatoo/PHPPEG ):

\n\n
query = { word | wildcard }\nword = ( [apostrophe] ( letter { alpha } ) ) | \xe2\x80\x9d , \xe2\x80\x9d\nletter = \xe2\x80\x9d a \xe2\x80\x9d | ... | \xe2\x80\x9d z \xe2\x80\x9d | \xe2\x80\x9d A \xe2\x80\x9d | ... | \xe2\x80\x9d Z \xe2\x80\x9d\nalpha = letter | \xe2\x80\x9d 0 \xe2\x80\x9d | ... | \xe2\x80\x9d 9 \xe2\x80\x9d\napostrophe = \xe2\x80\x9d \xe2\x80\x99 \xe2\x80\x9d\nwildcard = \xe2\x80\x9d ? \xe2\x80\x9d | \xe2\x80\x9d * \xe2\x80\x9d | synonyms | multiset | optionset\nsynonyms = \xe2\x80\x9d ~ \xe2\x80\x9d word\nmultiset = \xe2\x80\x9d { \xe2\x80\x9d word { word } \xe2\x80\x9d } \xe2\x80\x9d\noptionset = \xe2\x80\x9d [ \xe2\x80\x9d word { word } \xe2\x80\x9d ] \xe2\x80\x9d\n
Run Code Online (Sandbox Code Playgroud)\n\n

任何人都可以解释如何从一种转换为另一种,或者是否有地方我可以阅读它?

\n\n

谢谢!

\n\n
\xe2\x80\xa2 the question mark (?), which matches exactly one word;\n\xe2\x80\xa2 the asterisk (*), which matches any sequence of words;\n\xe2\x80\xa2 the tilde sign in front of a word (\xe2\x88\xbc<word>), which matches any of the word\xe2\x80\x99s synonyms;\n\xe2\x80\xa2 the multiset operator ({<words>}), which matches any ordering of the enumerated words; and,\n\xe2\x80\xa2 the optionset operator ([<words>]), which matches any one word from a list of options.\n
Run Code Online (Sandbox Code Playgroud)\n

Bra*_*ros 3

有几种 Peg 实现,所有这些实现都在 Peg 采用的通用约定中添加了一些内容,它们是:

  • 运算符“*”、“+”和“?” 与正则表达式中的含义相同;
  • 替代方案实际上是优先选择,它们使用“/”运算符来表示差异;
  • 运算符“&”和“!” 指定正和负零长度前瞻(即它们不推进“当前”指针);

在 EBNF 中,重复由“{ }”表示,在 Peg 中由“*”运算符表示,表示主题的零次或多次重复。例如,您的第一个语法规则可以在假设的 Peg 实现中表示如下:

query = (word / wildcard)* 
Run Code Online (Sandbox Code Playgroud)

EBNF“[]”运算符与 Peg 的“?”含义相同 运算符,表示主语是可选的。这是您的第二条规则,因为它可以转换为 Peg:

word = (apostrophe?  letter alpha*) / ","
Run Code Online (Sandbox Code Playgroud)

最后,一些 Peg 实现允许直接在其语法中使用正则表达式。看看你的第三条规则如何在这样的挂钩中表示:

letter = [a-zA-Z]
Run Code Online (Sandbox Code Playgroud)

根据您使用的语言和具体的 Peg 实现,一些事情可能会发生变化,但我希望这些指南能为您指明正确的方向。