Simple XML parser in bison/flex

Posted by user360872 on Stack Overflow See other posts from Stack Overflow or by user360872
Published on 2010-06-25T22:47:36Z Indexed on 2012/10/06 9:38 UTC
Read the original article Hit count: 224

Filed under:
|
|
|

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>.

So for example:

<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:

  • num: 1 (only value)
  • str: 1 (only value)
  • add | sub | mul | div: 2 (num | str | tag, num | str | tag)

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

© Stack Overflow or respective owner

Related posts about bison

Related posts about gnu-flex