Deriving from Lexical Analyser¶
The class generated by quex implements an engine for lexical analysis with some
basic functionality as was discussed in the previous sections. The body
section allows to add new members to the analyzer class which helps to
customize functionality. It is, however, conceivable that the generated lexical
analyzer needs to remain as it is to ensure the analysis being stable. At the
same time, derivates of such a ‘frozen’ analyzer might be needed for testing or
research purposes. Adding new features to frozen code is best done using C++
inheritance, i.e. one derives his own class from the generated lexer class.
The name and definition of the derived class needs to be known for the creation
of the lexical analyser. Thus, one has to specify the command line option
--derived-class
followed by the name of the class and
--derived-class-file
followed by the name of the file that contains the
definition of the derived class. The derivation itself happens in the standard
C++ way, i.e. one derives publicly from the lexical analyser class:
class DerivedLexer
: public quex::GeneratedLexer {
small_lexer(const std::string& filename,
std::ostream* output_stream = 0);
// ...
};
The derived class has to call a base class’ constructor from inside its constructor initialization list in order to properly initialize the lexical analyser object.
DerivedLexer(const std::string& filename,
std::ostream* output_stream = 0)
: GeneratedLexer(filename, output_stream),
// ...
{
// ...
}
The destructor must be virtual in order to ensure that the base class’ destruction happens properly.