简体   繁体   English

如何使用W3C EBNF-Notation并生成解析器生成器?

[英]How to consume W3C EBNF-Notation and produce a parser generator?

Throughout the RDF specs an EBNF-NOTATION XML specification is used to specify the grammar of a document. 在整个RDF规范中,EBNF-NOTATION XML规范用于指定文档的语法。 So I am wondering how to use Antlr/bison/yacc (maybe with some flag within these tools I don't know how to search for) — or other tools I don't know about yet — to consume these specifications and generate a parser for me to use to see if my RDF is well-formed before trying to load. 所以我想知道如何使用Antlr / bison / yacc(可能在这些工具中有一些标志,我不知道如何搜索) - 或其他我还不知道的工具 - 使用这些规范并生成解析器在尝试加载之前,我可以使用它来查看我的RDF是否格式正确。

An example grammar for my specific use case is: https://www.w3.org/TR/n-quads/#sec-grammar 我的具体用例的示例语法是: https//www.w3.org/TR/n-quads/#sec-grammar

I have already converted this grammar into Antlr4 grammar and created a parser using that tool and attempted to just write my own recursive descent parser but it was time-consuming and I'd rather not repeat the exercise if I have to do this again. 我已经将这个语法转换成了Antlr4语法并使用该工具创建了一个解析器,并试图编写我自己的递归下降解析器,但这很耗时,如果我不得不再次执行此操作,我宁愿不重复练习。

Don't really have any code, this is just a request for information. 真的没有任何代码,这只是一个信息请求。

What I want to do is basically copy/paste the grammars specified in this XML EBNF-NOTATION and produce a parser generator similar to what Antlr provides. 我想要做的是基本上复制/粘贴此XML EBNF-NOTATION中指定的语法,并生成类似于Antlr提供的解析器生成器。

REx Parser Generator works from grammars in W3C-style EBNF, and Railroad Diagram Generator can extract grammars directly from W3C documents. REx Parser Generator可以在W3C风格的EBNF中使用语法,而Railroad Diagram Generator可以直接从W3C文档中提取语法。

Here is how to create a working parser from the example grammar (in Java - some other target languages are supported, too): 以下是如何从示例语法创建一个有效的解析器(在Java中 - 也支持其他一些目标语言):

  • browse to Railroad Diagram Generator 浏览铁路图生成器
  • on the Get Grammar tab, enter the example URL https://www.w3.org/TR/n-quads 在“ Get Grammar选项卡上,输入示例URL https://www.w3.org/TR/n-quads
  • proceed to Edit Grammar 继续Edit Grammar
  • add a whitespace rule to the end of the grammar: WHITESPACE ::= [ #x9]+ /* ws: definition */ 在语法的末尾添加一个空格规则: WHITESPACE ::= [ #x9]+ /* ws: definition */
  • save grammar to local file n-quads.ebnf 将语法保存到本地文件n-quads.ebnf
  • browse to REx Parser Generator 浏览到REx Parser Generator
  • use input file n-quads.ebnf and command line -java -tree -main 使用输入文件n-quads.ebnf和命令行-java -tree -main
  • save the resulting parser n_quads.java and compile it 保存生成的解析器n_quads.java并编译它
  • run the parser on a sample file: java n_quads -i a-sample-file 在示例文件上运行解析器: java n_quads -i a-sample-file

Full disclosure: I'm the creator and maintainer of REx Parser Generator. 完全披露:我是REx Parser Generator的创建者和维护者。

It might be easier to use a tool to transform the EBNF into a parser generator spec for your parser generator of choice. 这可能是更容易使用的工具的EBNF 转变成一个解析器生成规格为您所选择的解析器生成。

To do that, you need a tool that can be taught to read the EBNF; 要做到这一点,你需要一个可以教授阅读EBNF的工具; actually, you can probably teach most parser generators that by writing down the syntax of the EBNF. 实际上,您可以通过写下EBNF的语法来教授大多数解析器生成器。

That tool has to also build some kind of syntax tree representing the EBNF, that you can walk over/transform to the target EBNF. 该工具还必须构建表示EBNF的某种语法树,您可以将其转换/转换为目标EBNF。 This is classic code generation... along with the usual issue that you have to specify the shape of the tree, build it, and then write all the ad hoc tree traversals needed to generate the target BNF. 这是典型的代码生成...以及您必须指定树的形状,构建它,然后编写生成目标BNF所需的所有ad hoc树遍历的常见问题。

You can get all this machinery packaged into a bundle as a program transformation system (PTS). 您可以将所有这些机器打包成一个程序包转换系统(PTS)。 PTS usually include parser generation, tree building, and pattern-directed code transformation. PTS通常包括解析器生成,树构建和模式定向代码转换。 Then you can focus to writing the EBNF grammar, and writing source-to-source translation rules. 然后,您可以专注于编写EBNF语法,并编写源到源的转换规则。

Our DMS Software Reengineering Toolkit can be used for this. 我们的DMS软件再造工具包可用于此目的。 We've done similar with DMS: namely, read XML DTD descriptions and synthesized high performance XML readers in Java. 我们已经完成了与DMS类似的工作:即读取XML DTD描述并使用Java合成高性能XML读取器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM