Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C++

Implementing Semantic Actions in the Boost Spirit Parser Framework

Rate me:
Please Sign up or sign in to vote.
4.95/5 (26 votes)
10 Oct 200410 min read 112.5K   1.3K   42  
Using composites to implement a modular arithmetic calculator with the Boost Spirit parser framework.
#pragma once

#include "GenericVisitor.h"
#include <Visitor.h>
#include <stack>

namespace Calculator
{
	class CExpressionTree;
	class COperatorPlus;
	class COperatorMinus;
	class COperatorMultiply;
	class COperatorDivide;
	class COperatorUnaryMinus;
	class CValue;
	class CEmpty;

	class CVisitorBase :
		public Loki::BaseVisitor,
		public Loki::Visitor<CExpressionTree>,
		public Loki::Visitor<COperatorPlus>,
		public Loki::Visitor<COperatorMinus>,
		public Loki::Visitor<COperatorMultiply>,
		public Loki::Visitor<COperatorDivide>,
		public Loki::Visitor<COperatorUnaryMinus>,
		public Loki::Visitor<CValue>,
		public Loki::Visitor<CEmpty>
	{
	public:
		CVisitorBase() {}
		virtual ~CVisitorBase() {}

		virtual void Visit( CExpressionTree & ) {}
		virtual void Visit( COperatorPlus &node ) { Visit( (CExpressionTree&) node ); }
		virtual void Visit( COperatorMinus &node ) { Visit( (CExpressionTree&) node ); }
		virtual void Visit( COperatorMultiply &node ) { Visit( (CExpressionTree&) node ); }
		virtual void Visit( COperatorDivide &node ) { Visit( (CExpressionTree&) node ); }
		virtual void Visit( COperatorUnaryMinus &node ) { Visit( (CExpressionTree&) node ); }
		virtual void Visit( CValue &node ) { Visit( (CExpressionTree&) node ); }
		virtual void Visit( CEmpty &node ) { Visit( (CExpressionTree&) node ); }
	};

	class CPrintTreeVisitor :
		public CVisitorBase,
		public Types::CVisitorLeftToRight<CPrintTreeVisitor, CExpressionTree>
	{
	public:
		CPrintTreeVisitor() {}
		virtual ~CPrintTreeVisitor() {}
		virtual void operator()( CExpressionTree *tree );

		virtual void Visit( CExpressionTree &node );

	};

	class CCalculateTreeVisitor :
		public CVisitorBase,
		public Types::CVisitorBottomToTop<CCalculateTreeVisitor, CExpressionTree>
	{
	public:
		CCalculateTreeVisitor( unsigned int modular ) : m_modular( modular ) {}
		virtual ~CCalculateTreeVisitor() {}

		virtual void Visit( COperatorPlus &node );
		virtual void Visit( COperatorMinus &node );
		virtual void Visit( COperatorMultiply &node );
		virtual void Visit( COperatorDivide &node );
		virtual void Visit( COperatorUnaryMinus &node );
		virtual void Visit( CValue &node );

		int pop();
		void push( int value );

	protected:
		int ReturnModular( int value ) const;

	private:
		unsigned int m_modular;
		std::stack<int> m_eval;
	};

}	// 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


Written By
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.

Comments and Discussions