Click here to Skip to main content
13,293,369 members (48,566 online)
Click here to Skip to main content


42 bookmarked
Posted 10 Oct 2004

Implementing Semantic Actions in the Boost Spirit Parser Framework

, 10 Oct 2004
Using composites to implement a modular arithmetic calculator with the Boost Spirit parser framework.
#include "stdafx.h"
#include "parser.h"
#include "syntax.h"
#include "expressiontree.h"
#include "visitors.h"
#include <boost/spirit.hpp>
#include <iostream>

#ifdef _DEBUG
#define new DEBUG_NEW

using namespace std;
using namespace boost::spirit;
using namespace Calculator;

CParser::CParser() :
m_parser( NULL ),
m_root( new CTreeRoot() ),
m_modular( 10 ),
m_processingFinished( false )
	m_parser = new Syntax( *this );

	if ( m_parser )
		delete m_parser;
	if ( m_root )
		delete m_root;

CParser::ParseString( const string &line )
	parse_info<> info;
		info = parse( line.c_str(), *m_parser, space_p );
	catch( exception &e )
		// TODO: Handle the error properly
		TRACE( e.what() );
		TRACE( "\n" );
	if ( !info.full )
		return false;

	cout << "Expression Tree: ";
	CPrintTreeVisitor printTree;
	printTree( GetRoot() );
	cout << endl;

	return true;

	// Output some instructions
	cout << "Modular Arithmetic Calculator" << endl;
	cout << "Type \"exit\" to quit" << endl;
	cout << "Declare a variable using eg: int i, j, k" << endl;
	cout << "Assign a variable using eg: i = 1 + 2" << endl;
	cout << "Evaluate an expression using eg: ? i + j + 3" << endl;
	cout << "Operators implemented include: +, -, *, / and unary -" << endl;
	cout << "Set the modular arithmetic value using eg: mod 13" << endl;
	// Infinite loop
	for ( ;; )
		// Accept input from the command line
		cout << "> ";
		string commandLine;
		getline( cin, commandLine, '\n' );
		// Parse input
		if ( ParseString( commandLine ) )
			TRACE( "Successfully parsed\n" );
			cout << "Syntax error" << endl;
		// Exit if required
		if ( m_processingFinished )

	m_processingFinished = true;

CTreeRoot *
	return m_root;

Syntax *
	return m_parser;

CParser::SetModular( unsigned int modular )
	m_modular = modular;

unsigned int
CParser::GetModular() const
	return m_modular;

	if ( m_root )
		delete m_root;
	m_root = new CTreeRoot;

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

Dave Handley
Web Developer
United Kingdom United Kingdom
I started programming on 8 bit machines as a teenager, writing my first compiled programming language before I was 16. I went on to study Engineering and Computer Science at Oxford University, getting a first and the University Prize for the best results in Computer Science. Since then I have worked in a variety of roles, involving systems management and development management on a wide variety of platforms. Now I manage a software development company producing CAD software for Windows using C++.

My 3 favourite reference books are: Design Patterns, Gamma et al; The C++ Standard Library, Josuttis; and Computer Graphics, Foley et al.

Outside computers, I am also the drummer in a band, The Unbelievers and we have just released our first album. I am a pretty good juggler and close up magician, and in my more insane past, I have cycled from Spain to Eastern Turkey, and cycled across the Namib desert.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 10 Oct 2004
Article Copyright 2004 by Dave Handley
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid