Click here to Skip to main content
13,554,130 members
Click here to Skip to main content


69 bookmarked
Posted 23 Mar 2003

Opening a door towards Spirit: a parser framework

, 23 Mar 2003
A quick introduction to Spirit, a parser generator framework based
#pragma once

#include <boost/spirit/core.hpp>
#include <boost/spirit/utility/escape_char.hpp>
#include <boost/spirit/utility/confix.hpp>
#include <iostream>
#include <vector>
#include <map>

namespace myparser
	using namespace std;
	using namespace boost::spirit;

    struct assign_string
        assign_string(std::string& str_)
        : str(str_) {};

		template<typename IteratorT >
        void operator()(IteratorT first, IteratorT last) const
            str.assign(first, last);

        std::string& str;

	struct keyvalue_grammar : public grammar<keyvalue_grammar>
		keyvalue_grammar(std::string& str_key_, std::string& str_val_)
			: str_key(str_key_), str_val(str_val_){};

        template <typename ScannerT>
        struct definition
            definition(keyvalue_grammar const& self)  
				equal = ch_p('=');

				key_value = key >> equal >> value;

				key_tag = ch_p('-') | ch_p('/');

				key = key_tag >> 
					(+alnum_p)[ assign_string(self.str_key) ];

				value = ( 
							(+ c_escape_ch_p )[assign_string(self.str_val)] ,
					| (+ alnum_p )[assign_string(self.str_val)] );

            rule<ScannerT>  key_tag, key, equal, value, key_value;
            rule<ScannerT> const& start() const { return key_value; };

		std::string& str_key;
		std::string& str_val;

	template<typename keyvalue_container> 
    class add_keyvalue_pair
		add_keyvalue_pair( keyvalue_container& kvc_, std::string& key_, std::string& val_)
			: kvc( kvc_ ), key(key_), val(val_)

        template <typename IteratorT>
        void operator()(IteratorT first, IteratorT last) const
			kvc.insert( keyvalue_container::value_type(key, val) );
		std::string& key;
		std::string& val;
		keyvalue_container& kvc;

	template<typename keyvalue_container> 
    struct cmdline_grammar : public grammar< cmdline_grammar >
		cmdline_grammar( keyvalue_container& kvc_, std::string& str_command_)
			: kvc(kvc_), str_command(str_command_)

        template <typename ScannerT>
        struct definition
            definition( cmdline_grammar<keyvalue_container> const& self )
				: key_value( key, value )
				command = (+alnum_p)[assign_string(self.str_command)];
				line = command >> *( key_value[ add_keyvalue_pair<keyvalue_container>( self.kvc, key, value ) ] );

            rule<ScannerT> command, line;
			keyvalue_grammar key_value;
			std::string key;
			std::string value;
            rule<ScannerT> const& start() const { return line; }

		keyvalue_container& kvc;
		std::string& str_command;

class command_line_parser
	typedef std::map< std::string, std::string> keyvalue_container;

	bool parse(const std::string& str)

		myparser::cmdline_grammar<keyvalue_container> parser(kvc, command);
		info = boost::spirit::parse(str.c_str(), parser, boost::spirit::space_p);

		return info.full;

	const char* stop() const	{	return info.stop;};

	const string& get_command() const				{	return command;};
	const keyvalue_container& get_keyvalues()const	{	return kvc;};

	std::string command;
	keyvalue_container kvc;
	parse_info<> info;

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.


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


About the Author

Jonathan de Halleux
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180515.1 | Last Updated 24 Mar 2003
Article Copyright 2003 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid