我有一个EBNF语法,并希望将它转换为PEG (https://github.com/anatoo/PHPPEG):
query = { word | wildcard }
word = ( [apostrophe] ( letter { alpha } ) ) | ” , ”
letter = ” a ” | ... | ” z ” | ” A ” | ... | ” Z ”
alpha = letter | ” 0 ” | ... | ” 9 ”
apostrophe = ” ’ ”
wildcard = ” ? ” | ” * ” | synonyms | multiset | optionset
synonyms = ” ~ ” word
multiset = ” { ” word { word } ” } ”
optionset = ” [ ” word { word } ” ] ”
有人能解释如何从一个转换到另一个,或者如果有什么地方我可以读到它吗?
谢谢!
• the question mark (?), which matches exactly one word;
• the asterisk (*), which matches any sequence of words;
• the tilde sign in front of a word (∼<word>), which matches any of the word’s synonyms;
• the multiset operator ({<words>}), which matches any ordering of the enumerated words; and,
• the optionset operator ([<words>]), which matches any one word from a list of options.
发布于 2018-11-05 05:27:21
有几个Peg实现,然后所有这些都为Peg选择的一般约定添加了一些内容,它们是:
在EBNF中,重复用"{ }“表示,在Peg中用"*”操作符表示,意思是主题的零或多次重复。例如,您的第一个语法规则可以在假设的Peg实现中表示如下:
query = (word / wildcard)*
EBNF“”操作符与Peg的"?“有相同的含义。运算符,意味着主题是可选的。这是您的第二个规则,因为它可以转换为Peg:
word = (apostrophe? letter alpha*) / ","
最后,几个Peg实现允许在语法中直接使用正则表达式。看看您的第三条规则是如何在这样的Peg中表示的:
letter = [a-zA-Z]
对于您正在使用的语言和特定的Peg实现,可能会有一些变化,但我希望这些指导方针能为您指明正确的方向。
https://stackoverflow.com/questions/53053899
复制相似问题