// Note: there is a dependency here on MFC, since the collection
// class CPtrList is used as a stack structure.
#include "fabuild.h"
#include "commdefs.h"
#include <afx.h>
#include <afxcoll.h>
#include <iostream.h>
// uses the envelope/letter idiom to hide implementation -- the
// letter never changes. methods overridden from base class are
// implemented by delegating them to a contained letter class.
class RegularExpression : protected CFABuild
{
public:
RegularExpression(CFABuild& faBuild);
~RegularExpression();
void SetFABuild(CFABuild* pFaBuild);
CFABuild* GetFABuild( ) const;
// converts the given regular expression to a finite automaton.
// The finite automaton data is stored in the CFABuild instance
// owned by this object.
void ConvertToFiniteAutomaton(const char* regexp);
// Debug method
void Dump(ostream &out);
protected:
//methods overridden from CFABuild
virtual int NewState();
virtual void AddTransition(
int sourceState, char event, int targetState);
virtual void AddEmptyTransition(
int sourceState, int targetState);
virtual void SetAcceptState(int a_acceptState);
private:
// Converts a character from an escape code to corresponding
// ASCII value
char TranslateCh(char inChar);
// Determines whether the passed character is in any way
// 'special', that is whether or not it is a meta-character
// controlling the meaning of the regular expression string
bool SpecialChar(char ch);
// Connects subexpression automaton to main automaton
void ConnectSubExpr(
int &oFs, int iFs, int iTs, int &oTs, bool &bPrevOR);
// Sub-expression stack members -- the structure used to hold
// the parent sub-expression information
struct subExpState
{
int m_s1;
int m_s2;
int m_s3;
bool m_f1;
subExpState(int i, int j, int k, bool f)
: m_s1(i), m_s2(j), m_s3(k), m_f1(f)
{ }
};
// - save the parent sub-expression
void Push(int i_s1, int i_s2, int i_s3, bool i_f1);
// - restore the parent sub-expression
void Pop(int &o_s1, int &o_s2, int &o_s3, bool &o_f1);
// - the stack storage
CPtrList subExpStack;
private:
//the contained letter class instance.
CFABuild* pFABuild;
};
//End of File