Listing 1: A straightforward interpreter implementation for the grammar rule
from the regular-expression language
class RegularExpression
{
public:
virtual ~RegularExpression() {}
virtual bool Interpret( const char *& ) const = 0;
};
class RepetitionExpression : public RegularExpression
{
RegularExpression * repeat_;
public:
RepititionExpression( RegularExpression * repeat )
: repeat_( repeat ) {}
virtual bool Interpret( const char *& sz ) const
{
while( repeat_->Interpret( sz ) )
;
return true;
}
};
class LiteralExpression : public RegularExpression
{
char ch_;
public:
LiteralExpression( char ch )
: ch_( ch ) {}
virtual bool Interpret( const char *& sz ) const
{
return ( *sz!==\o&&*sz==ch_ ) ? ( ++sz,true ) : false;
}
};
End of Listing