FlexLexer.hpp

Go to the documentation of this file.
00001 #ifndef __FLEX_LEXER_HPP
00002 #define __FLEX_LEXER_HPP
00003 // FlexLexer.h[pp] -- define interfaces for lexical analyzer classes generated
00004 //                by flex
00005 
00006 // Copyright (c) 1993 The Regents of the University of California.
00007 // All rights reserved.
00008 //
00009 // This code is derived from software contributed to Berkeley by
00010 // Kent Williams and Tom Epperly.
00011 //
00012 // Redistribution and use in source and binary forms with or without
00013 // modification are permitted provided that: (1) source distributions retain
00014 // this entire copyright notice and comment, and (2) distributions including
00015 // binaries display the following acknowledgement:  ``This product includes
00016 // software developed by the University of California, Berkeley and its
00017 // contributors'' in the documentation or other materials provided with the
00018 // distribution and in all advertising materials mentioning features or use
00019 // of this software.  Neither the name of the University nor the names of
00020 // its contributors may be used to endorse or promote products derived from
00021 // this software without specific prior written permission.
00022 
00023 // THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
00024 // WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
00025 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00026 
00027 // This file defines FlexLexer, an abstract class which specifies the
00028 // external interface provided to flex C++ lexer objects, and yyFlexLexer,
00029 // which defines a particular lexer class.
00030 //
00031 // If you want to create multiple lexer classes, you use the -P flag
00032 // to rename each yyFlexLexer to some other xxFlexLexer.  You then
00033 // include <FlexLexer.h> in your other sources once per lexer class:
00034 //
00035 //      #undef yyFlexLexer
00036 //      #define yyFlexLexer xxFlexLexer
00037 //      #include <FlexLexer.h>
00038 //
00039 //      #undef yyFlexLexer
00040 //      #define yyFlexLexer zzFlexLexer
00041 //      #include <FlexLexer.h>
00042 //      ...
00043 
00044 
00045 #include <iostream>
00046 
00047 //////////////////////////////////////////////////////////////////////
00048 // workarounds for the (very outdated) flex output:
00049 using std::istream;
00050 using std::ostream;
00051 using std::cin;
00052 using std::cout;
00053 using std::cerr;
00054 using std::endl;
00055 //////////////////////////////////////////////////////////////////////
00056 
00057 extern "C++" {
00058 
00059 struct yy_buffer_state;
00060 typedef int yy_state_type;
00061 
00062 /**
00063 From:
00064 Frank P.E. Vanris
00065 Subject: Modification of FlexLexer.h
00066 
00067 Date: Thu, 17 Jan 2002 14:35:26 -0600
00068 
00069 I modified FlexLexer.h a bit.
00070 
00071 To prevent a compiler warning (complaining that yylex() was hiding this
00072 function) I added in class yyFlexLexer the following:
00073 
00074 
00075         int yylex( istream* new_in, ostream* new_out = 0 ) {
00076           return FlexLexer::yylex(new_in, new_out);
00077         }
00078 
00079 Also in the class yyFlexLexer I added a protected method:
00080 
00081         // Function that can be used by subclasses during yylex()
00082         virtual int actionHook(void* data = 0) {
00083           return data == 0;
00084         }
00085 
00086 I override this function in a subclass of yyFlexLexer and I use it in
00087 the lex file to call my subclass. Any data I have to save (e.g.
00088 character count) I can now keep in my own subclass as memberfields
00089 instead of global variables in the lex file.
00090 
00091 I attached the FlexLexer.h file.
00092 
00093 I don't know whether Flex is still maintained since the last tarfile is
00094 from July 27th 1997, but I thought I at least pass it on.
00095 
00096 Frank Vanris.
00097 
00098 
00099 
00100 Sept 2003: changes by stephan@s11n.net
00101    - added 'using' statements for istream/ostreams, so gcc 3.x can play, too.
00102    - replace iostream.h with iostream, so gcc 3.x won't bitch about backwards-compatibility headers.
00103 
00104 Oct 2004: changed by stephan@s11n.net
00105    - Removed the macro-based generation of classes (was too
00106    problematic for multi-lexer projects) and replaced it with a
00107    template file, which gets filtered at build-time to create the
00108    subclass.
00109 
00110 */
00111 class FlexLexer {
00112 public:
00113         virtual ~FlexLexer()    { }
00114 
00115         const char* YYText()    { return yytext; }
00116         int YYLeng()            { return yyleng; }
00117 
00118         virtual void
00119                 yy_switch_to_buffer( struct yy_buffer_state* new_buffer ) = 0;
00120         virtual struct yy_buffer_state*
00121                 yy_create_buffer( istream* s, int size ) = 0;
00122         virtual void yy_delete_buffer( struct yy_buffer_state* b ) = 0;
00123         virtual void yyrestart( istream* s ) = 0;
00124 
00125         virtual int yylex() = 0;
00126 
00127         // Call yylex with new input/output sources.
00128         int yylex( istream* new_in, ostream* new_out = 0 )
00129                 {
00130                 switch_streams( new_in, new_out );
00131                 return yylex();
00132                 }
00133 
00134         // Switch to new input/output streams.  A nil stream pointer
00135         // indicates "keep the current one".
00136         virtual void switch_streams( istream* new_in = 0,
00137                                         ostream* new_out = 0 ) = 0;
00138 
00139         int lineno() const              { return yylineno; }
00140 
00141         int debug() const               { return yy_flex_debug; }
00142         void set_debug( int flag )      { yy_flex_debug = flag; }
00143 
00144 protected:
00145         char* yytext;
00146         int yyleng;
00147         int yylineno;           // only maintained if you use %option yylineno
00148         int yy_flex_debug;      // only has effect with -d or "%option debug"
00149 };
00150 
00151 } // extern "C++"
00152 
00153 #endif
00154 // ^^^^ multi-include guard
00155 

Generated on Wed Jun 4 21:41:59 2008 for libs11n-1.2.7 by  doxygen 1.5.3