src/scanner.ll

Go to the documentation of this file.
00001 /* $Id: scanner.ll 28 2007-08-20 10:27:39Z tb $ -*- mode: c++ -*- */
00004 %{ /*** C/C++ Declarations ***/
00005 
00006 #include <string>
00007 
00008 #include "parser.h"
00009 #include "scanner.h"
00010 
00011 /* import the parser's token type into a local typedef */
00012 typedef example::Parser::token token;
00013 typedef example::Parser::token_type token_type;
00014 
00015 /* Work around an incompatibility in flex (at least versions 2.5.31 through
00016  * 2.5.33): it generates code that does not conform to C89.  See Debian bug
00017  * 333231 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>.  */
00018 #undef yywrap
00019 #define yywrap()        1
00020 
00021 /* By default yylex returns int, we use token_type. Unfortunately yyterminate
00022  * by default returns 0, which is not of token_type. */
00023 #define yyterminate() return token::END
00024 
00025 /* This disables inclusion of unistd.h, which is not available under Visual C++
00026  * on Win32. The C++ scanner uses STL streams instead. */
00027 #define YY_NO_UNISTD_H
00028 
00029 %}
00030 
00031 /*** Flex Declarations and Options ***/
00032 
00033 /* enable c++ scanner class generation */
00034 %option c++
00035 
00036 /* change the name of the scanner class. results in "ExampleFlexLexer" */
00037 %option prefix="Example"
00038 
00039 /* the manual says "somewhat more optimized" */
00040 %option batch
00041 
00042 /* enable scanner to generate debug output. disable this for release
00043  * versions. */
00044 %option debug
00045 
00046 /* no support for include files is planned */
00047 %option noyywrap nounput 
00048 
00049 /* enables the use of start condition stacks */
00050 %option stack
00051 
00052 /* The following paragraph suffices to track locations accurately. Each time
00053  * yylex is invoked, the begin position is moved onto the end position. */
00054 %{
00055 #define YY_USER_ACTION  yylloc->columns(yyleng);
00056 %}
00057 
00058 %% /*** Regular Expressions Part ***/
00059 
00060  /* code to place at the beginning of yylex() */
00061 %{
00062     // reset location
00063     yylloc->step();
00064 
00065     // variable for quoted strings
00066     std::string quotedstring;
00067 %}
00068 
00069  /*** BEGIN EXAMPLE - Change the example lexer rules below ***/
00070 
00071 [0-9]+ {
00072     yylval->integerVal = atoi(yytext);
00073     return token::INTEGER;
00074 }
00075 
00076 [0-9]+"."[0-9]* {
00077     yylval->doubleVal = atof(yytext);
00078     return token::DOUBLE;
00079 }
00080 
00081 [A-Za-z][A-Za-z0-9_,.-]* {
00082     yylval->stringVal = new std::string(yytext, yyleng);
00083     return token::STRING;
00084 }
00085 
00086  /* gobble up white-spaces */
00087 [ \t\r]+ {
00088     yylloc->step();
00089 }
00090 
00091  /* gobble up end-of-lines */
00092 \n {
00093     yylloc->lines(yyleng); yylloc->step();
00094     return token::EOL;
00095 }
00096 
00097  /* pass all other characters up to bison */
00098 . {
00099     return static_cast<token_type>(*yytext);
00100 }
00101 
00102  /*** END EXAMPLE - Change the example lexer rules above ***/
00103 
00104 %% /*** Additional Code ***/
00105 
00106 namespace example {
00107 
00108 Scanner::Scanner(std::istream* in,
00109                  std::ostream* out)
00110     : ExampleFlexLexer(in, out)
00111 {
00112 }
00113 
00114 Scanner::~Scanner()
00115 {
00116 }
00117 
00118 void Scanner::set_debug(bool b)
00119 {
00120     yy_flex_debug = b;
00121 }
00122 
00123 }
00124 
00125 /* This implementation of ExampleFlexLexer::yylex() is required to fill the
00126  * vtable of the class ExampleFlexLexer. We define the scanner's main yylex
00127  * function via YY_DECL to reside in the Scanner class instead. */
00128 
00129 #ifdef yylex
00130 #undef yylex
00131 #endif
00132 
00133 int ExampleFlexLexer::yylex()
00134 {
00135     std::cerr << "in ExampleFlexLexer::yylex() !" << std::endl;
00136     return 0;
00137 }

Generated on Mon Aug 20 13:34:21 2007 for Flex Bison C++ Example by  doxygen 1.5.2