Archived

This topic is now archived and is closed to further replies.

Structural

Text parsing - How?

Recommended Posts

How do you make a text parser in C/C++? For example something that parses XML to read menu data into the application. I''ve tried to work my way through an XML file with ssprintf, but I can''t even extract the contents between "<" and ">" tags. Perhaps these things are easier with something that supports regular expressions. Does anyone have tips on how I can do this? Preferably with standard C/C++ functionality.

Share this post


Link to post
Share on other sites
There are several XML parsers for C++ already, why not use one of them?
I don''t know any links, but I''m sure you''ll google them up.

Share this post


Link to post
Share on other sites
Go through each character, one at a time. Keep track of what "mode" your in. For example, if you read in a < but haven''t read a space or > yet, then you''re reading in a tag. Read the whole tag. Then match it up with your database of tags. If you reach a >, then end tag mode and enter regular text mode (which ends when it finds a < or one of the special characters like &. If you read a space, enter attribute mode (which ends you find a space, at which point you can enter attribute value mode, when you find an = and a "). In short, its quite complicated, unless you use lex and yacc like someone suggested (which are, in fact, generic parsers, not just used for XML and many programs use like lilypond, a nice program that generates sheet music in postscript format from code that describes the music).

Share this post


Link to post
Share on other sites