Click here to Skip to main content
11,417,390 members (53,466 online)
Click here to Skip to main content
Add your own
alternative version

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 "expressiontree.h"

#include <sstream>
#include <assert.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;
using namespace Calculator;
using namespace Types;

////////////////////////////////////////////////////////////////////////////////
///	Definitions for root of expression tree
///
////////////////////////////////////////////////////////////////////////////////
CExpressionTree::CExpressionTree()
{
}

CExpressionTree::~CExpressionTree()
{
}

CExpressionTree::CExpressionTree( const CExpressionTree &src ) :
CGenericTree<CExpressionTree>( src )
{
}

CExpressionTree &
CExpressionTree::operator=( const CExpressionTree &rhs )
{
	if ( this != &rhs )
	{
		CGenericTree<CExpressionTree>::operator=( rhs );
	}
	return *this;
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for root node
///
////////////////////////////////////////////////////////////////////////////////
CTreeRoot::CTreeRoot()
{
}

CTreeRoot::~CTreeRoot()
{
}

CTreeRoot::CTreeRoot( const CTreeRoot &src ) :
CExpressionTree( src )
{
}

CTreeRoot &
CTreeRoot::operator=( const CTreeRoot &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
	}
	return *this;
}

CTreeRoot *
CTreeRoot::Clone()
{
	return new CTreeRoot( *this );
}

CExpressionTree *
CTreeRoot::Create()
{
	return new CTreeRoot();
}

void
CTreeRoot::AddChild( CExpressionTree *node )
{
	// Simply push the node onto the tree
	push_back( node );
}

void
CTreeRoot::AddParent( CExpressionTree *node )
{
	// Take the last 2 children from the root, add them to the new node
	CExpressionTree *last1 = NULL, *last2 = NULL;
	shallow_iterator iter = begin_shallow();
	for ( ; iter != end_shallow(); ++iter )
	{
		last2 = last1;
		last1 = *iter;
	}
	if ( last1 == NULL || last2 == NULL )
	{
		assert( false );
		delete node;
		return;
	}
	node->push_back( remove( last2 ) );
	node->push_back( remove( last1 ) );
	// Now add the new node to the tree
	push_back( node );
}

string
CTreeRoot::GetValue() const
{
	return string( "" );
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for plus operator
///
////////////////////////////////////////////////////////////////////////////////
COperatorPlus::COperatorPlus()
{
}

COperatorPlus::~COperatorPlus()
{
}

COperatorPlus::COperatorPlus( const COperatorPlus &src ) :
CExpressionTree( src )
{
}

COperatorPlus &
COperatorPlus::operator=( const COperatorPlus &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
	}
	return *this;
}

COperatorPlus *
COperatorPlus::Clone()
{
	return new COperatorPlus( *this );
}

CExpressionTree *
COperatorPlus::Create()
{
	return new COperatorPlus();
}

string
COperatorPlus::GetValue() const
{
	return string( "+" );
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for minus operator
///
////////////////////////////////////////////////////////////////////////////////
COperatorMinus::COperatorMinus()
{
}

COperatorMinus::~COperatorMinus()
{
}

COperatorMinus::COperatorMinus( const COperatorMinus &src ) :
CExpressionTree( src )
{
}

COperatorMinus &
COperatorMinus::operator=( const COperatorMinus &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
	}
	return *this;
}

COperatorMinus *
COperatorMinus::Clone()
{
	return new COperatorMinus( *this );
}

CExpressionTree *
COperatorMinus::Create()
{
	return new COperatorMinus();
}

string
COperatorMinus::GetValue() const
{
	return string( "-" );
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for multiply operator
///
////////////////////////////////////////////////////////////////////////////////
COperatorMultiply::COperatorMultiply()
{
}

COperatorMultiply::~COperatorMultiply()
{
}

COperatorMultiply::COperatorMultiply( const COperatorMultiply &src ) :
CExpressionTree( src )
{
}

COperatorMultiply &
COperatorMultiply::operator=( const COperatorMultiply &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
	}
	return *this;
}

COperatorMultiply *
COperatorMultiply::Clone()
{
	return new COperatorMultiply( *this );
}

CExpressionTree *
COperatorMultiply::Create()
{
	return new COperatorMultiply();
}

string
COperatorMultiply::GetValue() const
{
	return string( "*" );
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for divide operator
///
////////////////////////////////////////////////////////////////////////////////
COperatorDivide::COperatorDivide()
{
}

COperatorDivide::~COperatorDivide()
{
}

COperatorDivide::COperatorDivide( const COperatorDivide &src ) :
CExpressionTree( src )
{
}

COperatorDivide &
COperatorDivide::operator=( const COperatorDivide &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
	}
	return *this;
}

COperatorDivide *
COperatorDivide::Clone()
{
	return new COperatorDivide( *this );
}

CExpressionTree *
COperatorDivide::Create()
{
	return new COperatorDivide();
}

string
COperatorDivide::GetValue() const
{
	return string( "/" );
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for unary minus operator
///
////////////////////////////////////////////////////////////////////////////////
COperatorUnaryMinus::COperatorUnaryMinus()
{
}

COperatorUnaryMinus::~COperatorUnaryMinus()
{
}

COperatorUnaryMinus::COperatorUnaryMinus( const COperatorUnaryMinus &src ) :
CExpressionTree( src )
{
}

COperatorUnaryMinus &
COperatorUnaryMinus::operator=( const COperatorUnaryMinus &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
	}
	return *this;
}

COperatorUnaryMinus *
COperatorUnaryMinus::Clone()
{
	return new COperatorUnaryMinus( *this );
}

CExpressionTree *
COperatorUnaryMinus::Create()
{
	return new COperatorUnaryMinus();
}

string
COperatorUnaryMinus::GetValue() const
{
	return string( "-" );
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for value node
///
////////////////////////////////////////////////////////////////////////////////
CValue::CValue() :
m_value( 0 )
{
}

CValue::~CValue()
{
}

CValue::CValue( int value ) :
m_value( value )
{
}

CValue::CValue( const CValue &src ) :
CExpressionTree( src ),
m_value( src.m_value )
{
}

CValue &
CValue::operator=( const CValue &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
		m_value = rhs.m_value;
	}
	return *this;
}

CValue *
CValue::Clone()
{
	return new CValue( *this );
}

CExpressionTree *
CValue::Create()
{
	return new CValue();
}

void
CValue::SetNumber( int value )
{
	m_value = value;
}

int
CValue::GetNumber() const
{
	return m_value;
}

string
CValue::GetValue() const
{
	ostringstream str;
	str << m_value;
	return str.str();
}

////////////////////////////////////////////////////////////////////////////////
///	Definitions for empty node
///
////////////////////////////////////////////////////////////////////////////////
CEmpty::CEmpty()
{
}

CEmpty::~CEmpty()
{
}

CEmpty::CEmpty( const CEmpty &src ) :
CExpressionTree( src )
{
}

CEmpty &
CEmpty::operator=( const CEmpty &rhs )
{
	if ( this != &rhs )
	{
		CExpressionTree::operator=( rhs );
	}
	return *this;
}

CEmpty *
CEmpty::Clone()
{
	return new CEmpty( *this );
}

CExpressionTree *
CEmpty::Create()
{
	return new CEmpty();
}

string
CEmpty::GetValue() const
{
	return string();
}

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

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.

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