从 XML DTD 生成 lex 匹配规则和 yacc 语法规则

发布于 2024-12-23 03:54:25 字数 1186 浏览 4 评论 0原文

概述

虽然这个问题涉及用 C 编写的 lex/yacc,但它基本上以 python 编程为中心。

我有几个非常相似的 DTD,用于解析文档。程序的该部分是用 C 编写的,并且不需要调用完整的 SAX 处理程序(libxml2)用于此目的。由于 DTD(以及 XML 文件)具有静态格式,我认为这个问题最好用 lex 和 yacc 来解决。

虽然为任何 XML 文档编写完整的词法解析器都过于复杂,但为 XML 文档的特定子集编写完整的词法解析器是完全可以管理的。 DTD 可用于生成词法分析器(对输入进行标记)以及 YACC 中的解析器生成器。

我愿意做两个假设:

  1. XML 文档相对于 REC-xml-19980210
  2. XML 文档相对于其 DTD 是有效的。

因此,如果 XML 文档未能满足上述任何条件,则词法分析器/解析器对于该特定文件应该会失败。

问题

我的最终目标是编写一个能够成功执行以下操作的 python 脚本:(1) 解析 DTD;和(2) 生成 lex/yacc 文件。在开始之前,我有几个问题:

  1. 这个问题已经解决了吗?
    • 如果是这样,我应该考虑查看哪些库吗?
    • 如果没有,是否是因为使用我提到的工具没有解决方案?
  2. 是否有比使用静态解析器更好的(按性能衡量)方法从 XML 文件中提取非标记“内容”?

我意识到我可以使用 PLY 来解析 DTD,但是因为我对生成 lex 感兴趣/yacc 文件包含在 C 程序中,该选项将不起作用。因此,我想我可能会使用 xml.parsers.expat 来解析DTD。这允许我注册跟踪元素名称、它们在树中的位置、是否需要它们等的回调。这应该为我提供足够的信息来生成 lex/yacc 文件,但我想看看你们有什么建议。

Overview

Although this question implicates lex/yacc, which are written in C, it's fundamentally centered around programming in python.

I have several very similar DTDs that I'm using to parse a document. That section of the program is written in C, and there's just no need to invoke a full SAX handler (viz., libxml2) for this purpose. Since the DTDs (and therefore the XML files) have a static format, I think that this problem can best be solved with lex and yacc.

While writing a full lexical parser for any XML document is far too complex, writing one for a specific subset of XML documents is entirely manageable. The DTD could be used to generate the lexical analyzer (which tokenizes the input) as well as the parser generator in YACC.

There are two assumptions I am willing to make:

  1. The XML document is well-formed vis-à-vis REC-xml-19980210
  2. The XML document is valid vis-à-vis its DTD

Therefore, if an XML document fails to satisfy any of the above, the lexical analyzer/parser should simply fail for that particular file.

Questions

My ultimate goal is to write a python script that successfully: (1) parses the DTD; and (2)
generates the lex/yacc files. Before I begin, I have several questions:

  1. Has this problem already been solved?
    • If so, are there any libraries that I should consider looking at?
    • If not, is it because there is no solution using the tools I've mentioned?
  2. Are there better (as measured by performance) ways to extract the non-markup 'content' from XML files than using a static parser?

I realize that I can use PLY to parse the DTD, but because I'm interested in generating the lex/yacc files for inclusion in a C program, that option will not work. As such, I'm thinking that I might use xml.parsers.expat to parse the DTD. This allows me to register callbacks that track the element names, their position within the tree, whether they're required, etc. This should offer me enough information to generate lex/yacc files, but I would like to see what advice you guys have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伏妖词 2024-12-30 03:54:25

结合使用 XML Lexeryacc语法,和 YAXX 扩展名来生成相应的文件。

Use a combination of the XML Lexer, the yacc grammar, and the YAXX extension to generate the respective files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文