Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

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.
#pragma once

#include "GenericTree.h"
#include "Visitor.h"
#include <string>

namespace Calculator
{
	class CExpressionTree :
		public Types::CGenericTree<CExpressionTree>,
		public Loki::BaseVisitable<>
	{
	public:
		DEFINE_VISITABLE()
		CExpressionTree();
		CExpressionTree( const CExpressionTree &src );
		virtual ~CExpressionTree();
		CExpressionTree &operator=( const CExpressionTree &rhs );
		virtual CExpressionTree *Clone() = 0;

		virtual std::string GetValue() const = 0;
	};

	class CTreeRoot :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		CTreeRoot();
		CTreeRoot( const CTreeRoot &src );
		virtual ~CTreeRoot();
		CTreeRoot &operator=( const CTreeRoot &rhs );
		virtual CTreeRoot *Clone();
		static CExpressionTree *Create();

		void AddChild( CExpressionTree *node );
		void AddParent( CExpressionTree *node );

		virtual std::string GetValue() const;
	};

	class COperatorPlus :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		COperatorPlus();
		COperatorPlus( const COperatorPlus &src );
		virtual ~COperatorPlus();
		COperatorPlus &operator=( const COperatorPlus &rhs );
		virtual COperatorPlus *Clone();
		static CExpressionTree *Create();

		virtual std::string GetValue() const;
	};

	class COperatorMinus :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		COperatorMinus();
		COperatorMinus( const COperatorMinus &src );
		virtual ~COperatorMinus();
		COperatorMinus &operator=( const COperatorMinus &rhs );
		virtual COperatorMinus *Clone();
		static CExpressionTree *Create();

		virtual std::string GetValue() const;
	};

	class COperatorMultiply :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		COperatorMultiply();
		COperatorMultiply( const COperatorMultiply &src );
		virtual ~COperatorMultiply();
		COperatorMultiply &operator=( const COperatorMultiply &rhs );
		virtual COperatorMultiply *Clone();
		static CExpressionTree *Create();

		virtual std::string GetValue() const;
	};

	class COperatorDivide :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		COperatorDivide();
		COperatorDivide( const COperatorDivide &src );
		virtual ~COperatorDivide();
		COperatorDivide &operator=( const COperatorDivide &rhs );
		virtual COperatorDivide *Clone();
		static CExpressionTree *Create();

		virtual std::string GetValue() const;
	};

	class COperatorUnaryMinus :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		COperatorUnaryMinus();
		COperatorUnaryMinus( const COperatorUnaryMinus &src );
		virtual ~COperatorUnaryMinus();
		COperatorUnaryMinus &operator=( const COperatorUnaryMinus &rhs );
		virtual COperatorUnaryMinus *Clone();
		static CExpressionTree *Create();

		virtual std::string GetValue() const;
	};

	class CValue :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		CValue();
		explicit CValue( int value );
		CValue( const CValue &src );
		virtual ~CValue();
		CValue &operator=( const CValue &rhs );
		virtual CValue *Clone();
		static CExpressionTree *Create();

		void SetNumber( int value );
		int GetNumber() const;

		virtual std::string GetValue() const;
	private:
		int m_value;
	};

	class CEmpty :
		public CExpressionTree
	{
	public:
		DEFINE_VISITABLE()
		CEmpty();
		CEmpty( const CEmpty &src );
		virtual ~CEmpty();
		CEmpty &operator=( const CEmpty &rhs );
		virtual CEmpty *Clone();
		static CExpressionTree *Create();

		virtual std::string GetValue() const;
	};

}	// namespace Calculator

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 | Mobile
Web04 | 2.8.140926.1 | Last Updated 10 Oct 2004
Article Copyright 2004 by Dave Handley
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid