<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Flex Bison C++ Example: src/scanner.ll Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.9 -->
<div class="navigation" id="top">
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li class="current"><a href="files.html"><span>Files</span></a></li>
</ul>
</div>
<div class="tabs">
<ul>
<li><a href="files.html"><span>File List</span></a></li>
<li><a href="globals.html"><span>File Members</span></a></li>
</ul>
</div>
<h1>src/scanner.ll</h1><a href="scanner_8ll.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* $Id: scanner.ll 44 2008-10-23 09:03:19Z tb $ -*- mode: c++ -*- */</span>
<a name="l00004"></a>00004 %{ <span class="comment">/*** C/C++ Declarations ***/</span>
<a name="l00005"></a>00005
<a name="l00006"></a>00006 <span class="preprocessor">#include <string></span>
<a name="l00007"></a>00007
<a name="l00008"></a>00008 <span class="preprocessor">#include "<a class="code" href="scanner_8h.html">scanner.h</a>"</span>
<a name="l00009"></a>00009
<a name="l00010"></a>00010 <span class="comment">/* import the parser's token type into a local typedef */</span>
<a name="l00011"></a><a class="code" href="scanner_8ll.html#8e62bc8da207971bf44c4466027e9556">00011</a> <span class="keyword">typedef</span> <a class="code" href="structexample_1_1Parser_1_1token.html" title="Tokens.">example::Parser::token</a> <a class="code" href="structexample_1_1Parser_1_1token.html" title="Tokens.">token</a>;
<a name="l00012"></a><a class="code" href="scanner_8ll.html#0053da8d6c5c76f61ae5b50741cb9bd2">00012</a> <span class="keyword">typedef</span> <a class="code" href="structexample_1_1Parser_1_1token.html#be6390b38a559513455b7cdb0be57938">example::Parser::token_type</a> <a class="code" href="structexample_1_1Parser_1_1token.html#be6390b38a559513455b7cdb0be57938">token_type</a>;
<a name="l00013"></a>00013
<a name="l00014"></a>00014 <span class="comment">/* By default yylex returns int, we use token_type. Unfortunately yyterminate</span>
<a name="l00015"></a>00015 <span class="comment"> * by default returns 0, which is not of token_type. */</span>
<a name="l00016"></a><a class="code" href="scanner_8ll.html#c3286b18a2e91b4571b97df96a118e84">00016</a> <span class="preprocessor">#define yyterminate() return token::END</span>
<a name="l00017"></a>00017 <span class="preprocessor"></span>
<a name="l00018"></a>00018 <span class="comment">/* This disables inclusion of unistd.h, which is not available under Visual C++</span>
<a name="l00019"></a>00019 <span class="comment"> * on Win32. The C++ scanner uses STL streams instead. */</span>
<a name="l00020"></a><a class="code" href="scanner_8ll.html#e78ac56cd1f29572e967ed7636952d15">00020</a> <span class="preprocessor">#define YY_NO_UNISTD_H</span>
<a name="l00021"></a>00021 <span class="preprocessor"></span>
<a name="l00022"></a>00022 %}
<a name="l00023"></a>00023
<a name="l00024"></a>00024 <span class="comment">/*** Flex Declarations and Options ***/</span>
<a name="l00025"></a>00025
<a name="l00026"></a>00026 <span class="comment">/* enable c++ scanner class generation */</span>
<a name="l00027"></a>00027 %option c++
<a name="l00028"></a>00028
<a name="l00029"></a>00029 <span class="comment">/* change the name of the scanner class. results in "ExampleFlexLexer" */</span>
<a name="l00030"></a>00030 %option <a class="code" href="parser_8yy.html#00025fc5594612204625c5d8ab7703e7">prefix</a>=<span class="stringliteral">"Example"</span>
<a name="l00031"></a>00031
<a name="l00032"></a>00032 <span class="comment">/* the manual says "somewhat more optimized" */</span>
<a name="l00033"></a>00033 %option batch
<a name="l00034"></a>00034
<a name="l00035"></a>00035 <span class="comment">/* enable scanner to generate debug output. disable this for release</span>
<a name="l00036"></a>00036 <span class="comment"> * versions. */</span>
<a name="l00037"></a>00037 %option debug
<a name="l00038"></a>00038
<a name="l00039"></a>00039 <span class="comment">/* no support for include files is planned */</span>
<a name="l00040"></a>00040 %option yywrap nounput
<a name="l00041"></a>00041
<a name="l00042"></a>00042 <span class="comment">/* enables the use of start condition stacks */</span>
<a name="l00043"></a>00043 %option stack
<a name="l00044"></a>00044
<a name="l00045"></a>00045 <span class="comment">/* The following paragraph suffices to track locations accurately. Each time</span>
<a name="l00046"></a>00046 <span class="comment"> * yylex is invoked, the begin position is moved onto the end position. */</span>
<a name="l00047"></a>00047 %{
<a name="l00048"></a>00048 <span class="preprocessor">#define YY_USER_ACTION yylloc->columns(yyleng);</span>
<a name="l00049"></a>00049 <span class="preprocessor"></span>%}
<a name="l00050"></a>00050
<a name="l00051"></a>00051 %% <span class="comment">/*** Regular Expressions Part ***/</span>
<a name="l00052"></a>00052
<a name="l00053"></a>00053 <span class="comment">/* code to place at the beginning of yylex() */</span>
<a name="l00054"></a>00054 %{
<a name="l00055"></a>00055 <span class="comment">// reset location</span>
<a name="l00056"></a>00056 yylloc->step();
<a name="l00057"></a>00057 %}
<a name="l00058"></a>00058
<a name="l00059"></a>00059 <span class="comment">/*** BEGIN EXAMPLE - Change the example lexer rules below ***/</span>
<a name="l00060"></a>00060
<a name="l00061"></a>00061 [0-9]+ {
<a name="l00062"></a>00062 yylval->integerVal = atoi(yytext);
<a name="l00063"></a>00063 <span class="keywordflow">return</span> <a class="code" href="structexample_1_1Parser_1_1token.html#be6390b38a559513455b7cdb0be579381438341b7fe2872e0eba6f1864097dd2">token::INTEGER</a>;
<a name="l00064"></a>00064 }
<a name="l00065"></a>00065
<a name="l00066"></a>00066 [0-9]+<span class="stringliteral">"."</span>[0-9]* {
<a name="l00067"></a>00067 yylval->doubleVal = atof(yytext);
<a name="l00068"></a>00068 <span class="keywordflow">return</span> <a class="code" href="structexample_1_1Parser_1_1token.html#be6390b38a559513455b7cdb0be579383c4fedf60439d272dd07777c41242b63">token::DOUBLE</a>;
<a name="l00069"></a>00069 }
<a name="l00070"></a>00070
<a name="l00071"></a>00071 [A-Za-z][A-Za-z0-9_,.-]* {
<a name="l00072"></a>00072 yylval->stringVal = <span class="keyword">new</span> std::string(yytext, yyleng);
<a name="l00073"></a>00073 <span class="keywordflow">return</span> <a class="code" href="structexample_1_1Parser_1_1token.html#be6390b38a559513455b7cdb0be57938d8ee446b1f044f6a68ba3d5b4f0052f2">token::STRING</a>;
<a name="l00074"></a>00074 }
<a name="l00075"></a>00075
<a name="l00076"></a>00076 <span class="comment">/* gobble up white-spaces */</span>
<a name="l00077"></a>00077 [ \t\r]+ {
<a name="l00078"></a>00078 yylloc->step();
<a name="l00079"></a>00079 }
<a name="l00080"></a>00080
<a name="l00081"></a>00081 <span class="comment">/* gobble up end-of-lines */</span>
<a name="l00082"></a>00082 \n {
<a name="l00083"></a>00083 yylloc->lines(yyleng); yylloc->step();
<a name="l00084"></a>00084 <span class="keywordflow">return</span> <a class="code" href="structexample_1_1Parser_1_1token.html#be6390b38a559513455b7cdb0be57938caa26f7a1198fc0597c403b3aac443b4">token::EOL</a>;
<a name="l00085"></a>00085 }
<a name="l00086"></a>00086
<a name="l00087"></a>00087 <span class="comment">/* pass all other characters up to bison */</span>
<a name="l00088"></a>00088 . {
<a name="l00089"></a>00089 <span class="keywordflow">return</span> <span class="keyword">static_cast<</span><a class="code" href="structexample_1_1Parser_1_1token.html#be6390b38a559513455b7cdb0be57938">token_type</a><span class="keyword">></span>(*yytext);
<a name="l00090"></a>00090 }
<a name="l00091"></a>00091
<a name="l00092"></a>00092 <span class="comment">/*** END EXAMPLE - Change the example lexer rules above ***/</span>
<a name="l00093"></a>00093
<a name="l00094"></a>00094 %% <span class="comment">/*** Additional Code ***/</span>
<a name="l00095"></a>00095
<a name="l00096"></a>00096 <span class="keyword">namespace </span>example {
<a name="l00097"></a>00097
<a name="l00098"></a>00098 <a class="code" href="classexample_1_1Scanner.html#155a119623eb650753829e3357d59d53" title="Create a new scanner object.">Scanner::Scanner</a>(std::istream* in,
<a name="l00099"></a>00099 std::ostream* out)
<a name="l00100"></a>00100 : <a class="code" href="classExampleFlexLexer.html">ExampleFlexLexer</a>(in, out)
<a name="l00101"></a>00101 {
<a name="l00102"></a>00102 }
<a name="l00103"></a>00103
<a name="l00104"></a>00104 Scanner::~Scanner()
<a name="l00105"></a>00105 {
<a name="l00106"></a>00106 }
<a name="l00107"></a>00107
<a name="l00108"></a>00108 <span class="keywordtype">void</span> Scanner::set_debug(<span class="keywordtype">bool</span> b)
<a name="l00109"></a>00109 {
<a name="l00110"></a>00110 yy_flex_debug = b;
<a name="l00111"></a>00111 }
<a name="l00112"></a>00112
<a name="l00113"></a>00113 }
<a name="l00114"></a>00114
<a name="l00115"></a>00115 <span class="comment">/* This implementation of ExampleFlexLexer::yylex() is required to fill the</span>
<a name="l00116"></a>00116 <span class="comment"> * vtable of the class ExampleFlexLexer. We define the scanner's main yylex</span>
<a name="l00117"></a>00117 <span class="comment"> * function via YY_DECL to reside in the Scanner class instead. */</span>
<a name="l00118"></a>00118
<a name="l00119"></a>00119 <span class="preprocessor">#ifdef yylex</span>
<a name="l00120"></a>00120 <span class="preprocessor"></span><span class="preprocessor">#undef yylex</span>
<a name="l00121"></a>00121 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00122"></a>00122 <span class="preprocessor"></span>
<a name="l00123"></a>00123 <span class="keywordtype">int</span> <a class="code" href="classExampleFlexLexer.html#52dace47f333320dfb4ffb21f8c6e474" title="The main scanner function which does all the work.">ExampleFlexLexer::yylex</a>()
<a name="l00124"></a>00124 {
<a name="l00125"></a>00125 std::cerr << <span class="stringliteral">"in ExampleFlexLexer::yylex() !"</span> << std::endl;
<a name="l00126"></a>00126 <span class="keywordflow">return</span> 0;
<a name="l00127"></a>00127 }
<a name="l00128"></a>00128
<a name="l00129"></a>00129 <span class="comment">/* When the scanner receives an end-of-file indication from YY_INPUT, it then</span>
<a name="l00130"></a>00130 <span class="comment"> * checks the yywrap() function. If yywrap() returns false (zero), then it is</span>
<a name="l00131"></a>00131 <span class="comment"> * assumed that the function has gone ahead and set up `yyin' to point to</span>
<a name="l00132"></a>00132 <span class="comment"> * another input file, and scanning continues. If it returns true (non-zero),</span>
<a name="l00133"></a>00133 <span class="comment"> * then the scanner terminates, returning 0 to its caller. */</span>
<a name="l00134"></a>00134
<a name="l00135"></a>00135 <span class="keywordtype">int</span> <a class="code" href="classExampleFlexLexer.html#c9b84056b4fb8057604f7d9e34f0ff34">ExampleFlexLexer::yywrap</a>()
<a name="l00136"></a>00136 {
<a name="l00137"></a>00137 <span class="keywordflow">return</span> 1;
<a name="l00138"></a>00138 }
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Generated on Sat Sep 5 10:26:25 2009 for Flex Bison C++ Example by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.9 </small></address>
</body>
</html>