Click here to Skip to main content
12,693,263 members (25,961 online)
Click here to Skip to main content

Stats

69.9K views
1.1K downloads
39 bookmarked
Posted

Parsing XML in C++ using the YARD Parser

, 21 Dec 2004
Provides a set of tools for building XML parsers in C++ using the YARD recursive descent parser.
// released into the public domain 
// by Christopher Diggins 2004 
// http://www.cdiggins.com 

#ifndef TOKENIZER_HPP_INCLUDED
#define TOKENIZER_HPP_INCLUDED 

#include <list>
#include <utility>

#include "parser_input_stream.hpp"

namespace yard
{
  typedef std::pair<char const*, char const*> Token;
  typedef std::list<Token> TokenList;
  typedef TokenList::const_iterator TokenIter;
  
  template<typename Rules_T>
  struct Tokenizer 
  {  
    void Tokenize(char const* pBegin, char const* pEnd) 
    {      
      ParserInputStream<char> input(pBegin, pEnd); 
      while (!input.AtEnd()) {
        char const* pos = input.GetPos();
        if (Rules_T::Accept(input)) {
          mTkns.push_back(Token(pos, input.GetPos()));
        }
        input.GotoNext();
      }              
    }
    TokenIter begin() { return mTkns.begin(); }
    TokenIter end() { return mTkns.end(); }
  private:
    TokenList mTkns;
  };

  void OutputTokens(TokenIter iter, TokenIter end) 
  {
    // outputs first 10 tokens as strings
    for (int i=0; (i < 10) && (iter != end); i++, iter++) {
      int n = static_cast<int>(iter->second - iter->first);
      std::string s(iter->first, 0, n);     
      std::cout << s.c_str() << std::endl;
    }
  }
}

#endif // #ifndef TOKENIZER_HPP_INCLUDED

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Christopher Diggins
Software Developer Autodesk
Canada Canada
This article was written by Christopher Diggins, a computer science nerd who currently works at Autodesk as an SDK specialist.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 21 Dec 2004
Article Copyright 2004 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid