bison/flex中的简单XML解析器 [英] Simple XML parser in bison/flex

查看:130
本文介绍了bison/flex中的简单XML解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用bison/flex创建简单xml解析器.我不需要验证,注释,参数,只需<tag>value</tag>,其中 value 可以是数字,字符串或其他<tag>value</tag>.

I would like to create simple xml parser using bison/flex. I don't need validation, comments, arguments, only <tag>value</tag>, where value can be number, string or other <tag>value</tag>.

例如:

<div>
  <mul>
    <num>20</num>
    <add>
      <num>1</num>
      <num>5</num>
    </add>
  </mul>
  <id>test</id>
</div>

如果有帮助,我知道可能会出现的所有标签的名称.我知道给定标签可以容纳多少个子标签.是否有可能创建像这样的野牛解析器:

If it helps, I know the names of all tags that may occur. I know how many sub-tag can be hold by given tag. Is it possible to create bison parser that would do something like that:

- new Tag("num", 1)           // tag1
- new Tag("num", 5)           // tag2
- new Tag("add", tag1, tag2)  // tag3
- new Tag("num", 20)          // tag4
- new Tag("mul", tag4, tag3)
...
- root = top_tag

标记&子标签数量:

Tag & number of sub-tags:

  • 数字:1(仅值)
  • str:1(仅值)
  • 添加|子| mul | div:2(num | str |标签,num | str |标签)

您能否帮助我进行语法设计,以便像上面给出的那样创建AST?

Could you help me with grammar to be able to create AST like given above?

推荐答案

对于您的要求,我认为 yax系统会很好. 在自述文件中:

For your requirements, I think the yax system would work well. From the README:

yax项目的目标是允许使用YACC(实际上是Gnu Bison)来解析/处理XML文档.

The goal of the yax project is to allow the use of YACC (Gnu Bison actually) to parse/process XML documents.

实现上述目标的关键软件是提供一个可以从XML文档生成XML词法标记流的库.

The key piece of software for achieving the above goal is to provide a library that can produce an XML lexical token stream from an XML document.

可以包装此流以创建yylex()实例,以将令牌提供给Bison语法以解析和处理XML文档.

This stream can be wrapped to create an instance of yylex() to feed tokens to a Bison grammar to parse and process the XML document.

使用流和Bison语法,可以至少进行以下类型的活动.

Using the stream plus a Bison grammar, it is possible to carry at least the following kinds of activities.

  1. 验证XML文档,
  2. 直接解析XML文档以创建内部数据结构,
  3. 构造DOM树.

这篇关于bison/flex中的简单XML解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆