idlebox / 2007 / flex-bison-cpp-example / flex-bison-cpp-example-0.1.4 / doxygen-html / parser_8yy_source.html.html (Download File)
<!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/parser.yy 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&nbsp;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&nbsp;List</span></a></li>
      <li><a href="globals.html"><span>File&nbsp;Members</span></a></li>
    </ul>
  </div>
<h1>src/parser.yy</h1><a href="parser_8yy.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* $Id: parser.yy 48 2009-09-05 08:07:10Z 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 &lt;stdio.h&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;vector&gt;</span>
<a name="l00009"></a>00009
<a name="l00010"></a>00010 <span class="preprocessor">#include "<a class="code" href="expression_8h.html" title="Implements an example calculator class group.">expression.h</a>"</span>
<a name="l00011"></a>00011
<a name="l00012"></a>00012 %}
<a name="l00013"></a>00013
<a name="l00014"></a>00014 <span class="comment">/*** yacc/bison Declarations ***/</span>
<a name="l00015"></a>00015
<a name="l00016"></a>00016 <span class="comment">/* Require bison 2.3 or later */</span>
<a name="l00017"></a>00017 %require <span class="stringliteral">"2.3"</span>
<a name="l00018"></a>00018
<a name="l00019"></a>00019 <span class="comment">/* add debug output code to generated parser. disable this for release</span>
<a name="l00020"></a>00020 <span class="comment"> * versions. */</span>
<a name="l00021"></a>00021 %debug
<a name="l00022"></a>00022
<a name="l00023"></a>00023 <span class="comment">/* start symbol is named "start" */</span>
<a name="l00024"></a>00024 %start start
<a name="l00025"></a>00025
<a name="l00026"></a>00026 <span class="comment">/* write out a header file containing the token defines */</span>
<a name="l00027"></a>00027 %defines
<a name="l00028"></a>00028
<a name="l00029"></a>00029 <span class="comment">/* use newer C++ skeleton file */</span>
<a name="l00030"></a>00030 %skeleton <span class="stringliteral">"lalr1.cc"</span>
<a name="l00031"></a>00031
<a name="l00032"></a>00032 <span class="comment">/* namespace to enclose parser in */</span>
<a name="l00033"></a><a class="code" href="parser_8yy.html#00025fc5594612204625c5d8ab7703e7">00033</a> %name-<a class="code" href="parser_8yy.html#00025fc5594612204625c5d8ab7703e7">prefix</a>=<span class="stringliteral">"example"</span>
<a name="l00034"></a>00034
<a name="l00035"></a>00035 <span class="comment">/* set the parser's class identifier */</span>
<a name="l00036"></a>00036 %define <span class="stringliteral">"parser_class_name"</span> <span class="stringliteral">"Parser"</span>
<a name="l00037"></a>00037
<a name="l00038"></a>00038 <span class="comment">/* keep track of the current position within the input */</span>
<a name="l00039"></a>00039 %locations
<a name="l00040"></a>00040 %initial-action
<a name="l00041"></a>00041 {
<a name="l00042"></a>00042     <span class="comment">// initialize the initial location object</span>
<a name="l00043"></a>00043     @$.begin.filename = @$.end.filename = &amp;driver.streamname;
<a name="l00044"></a>00044 };
<a name="l00045"></a>00045
<a name="l00046"></a>00046 <span class="comment">/* The driver is passed by reference to the parser and to the scanner. This</span>
<a name="l00047"></a>00047 <span class="comment"> * provides a simple but effective pure interface, not relying on global</span>
<a name="l00048"></a>00048 <span class="comment"> * variables. */</span>
<a name="l00049"></a>00049 %parse-param { <span class="keyword">class </span>Driver&amp; driver }
<a name="l00050"></a>00050
<a name="l00051"></a>00051 <span class="comment">/* verbose error messages */</span>
<a name="l00052"></a>00052 %error-verbose
<a name="l00053"></a>00053
<a name="l00054"></a>00054  <span class="comment">/*** BEGIN EXAMPLE - Change the example grammar's tokens below ***/</span>
<a name="l00055"></a>00055
<a name="l00056"></a>00056 %<span class="keyword">union </span>{
<a name="l00057"></a>00057     <span class="keywordtype">int</span>                         integerVal;
<a name="l00058"></a>00058     <span class="keywordtype">double</span>                      doubleVal;
<a name="l00059"></a>00059     std::string*                stringVal;
<a name="l00060"></a>00060     <span class="keyword">class </span><a class="code" href="classCalcNode.html" title="CalcNode is the abstract base class for calculation nodes.">CalcNode</a>*             calcnode;
<a name="l00061"></a>00061 }
<a name="l00062"></a>00062
<a name="l00063"></a>00063 %<a class="code" href="structexample_1_1Parser_1_1token.html" title="Tokens.">token</a>                  END          0  <span class="stringliteral">"end of file"</span>
<a name="l00064"></a>00064 %<a class="code" href="structexample_1_1Parser_1_1token.html" title="Tokens.">token</a>                  EOL             <span class="stringliteral">"end of line"</span>
<a name="l00065"></a>00065 %<a class="code" href="structexample_1_1Parser_1_1token.html" title="Tokens.">token &lt;integerVal&gt;</a>     INTEGER         <span class="stringliteral">"integer"</span>
<a name="l00066"></a>00066 %<a class="code" href="structexample_1_1Parser_1_1token.html" title="Tokens.">token &lt;doubleVal&gt;</a>      DOUBLE          <span class="stringliteral">"double"</span>
<a name="l00067"></a>00067 %<a class="code" href="structexample_1_1Parser_1_1token.html" title="Tokens.">token &lt;stringVal&gt;</a>      STRING          <span class="stringliteral">"string"</span>
<a name="l00068"></a>00068
<a name="l00069"></a>00069 %type &lt;calcnode&gt;        constant variable
<a name="l00070"></a>00070 %type &lt;calcnode&gt;        atomexpr powexpr unaryexpr mulexpr addexpr expr
<a name="l00071"></a>00071
<a name="l00072"></a>00072 %destructor { <span class="keyword">delete</span> $$; } STRING
<a name="l00073"></a>00073 %destructor { <span class="keyword">delete</span> $$; } constant variable
<a name="l00074"></a>00074 %destructor { <span class="keyword">delete</span> $$; } atomexpr powexpr unaryexpr mulexpr addexpr expr
<a name="l00075"></a>00075
<a name="l00076"></a>00076  <span class="comment">/*** END EXAMPLE - Change the example grammar's tokens above ***/</span>
<a name="l00077"></a>00077
<a name="l00078"></a>00078 %{
<a name="l00079"></a>00079
<a name="l00080"></a>00080 <span class="preprocessor">#include "<a class="code" href="driver_8h.html" title="Declaration of the example::Driver class.">driver.h</a>"</span>
<a name="l00081"></a>00081 <span class="preprocessor">#include "<a class="code" href="scanner_8h.html">scanner.h</a>"</span>
<a name="l00082"></a>00082
<a name="l00083"></a>00083 <span class="comment">/* this "connects" the bison parser in the driver to the flex scanner class</span>
<a name="l00084"></a>00084 <span class="comment"> * object. it defines the yylex() function call to pull the next token from the</span>
<a name="l00085"></a>00085 <span class="comment"> * current lexer object of the driver context. */</span>
<a name="l00086"></a>00086 <span class="preprocessor">#undef yylex</span>
<a name="l00087"></a>00087 <span class="preprocessor"></span><span class="preprocessor">#define yylex driver.lexer-&gt;lex</span>
<a name="l00088"></a>00088 <span class="preprocessor"></span>
<a name="l00089"></a>00089 %}
<a name="l00090"></a>00090
<a name="l00091"></a>00091 %% <span class="comment">/*** Grammar Rules ***/</span>
<a name="l00092"></a>00092
<a name="l00093"></a>00093  <span class="comment">/*** BEGIN EXAMPLE - Change the example grammar rules below ***/</span>
<a name="l00094"></a>00094
<a name="l00095"></a>00095 constant : INTEGER
<a name="l00096"></a>00096            {
<a name="l00097"></a>00097                $$ = <span class="keyword">new</span> <a class="code" href="classCNConstant.html" title="Calculation node always returning a constant value.">CNConstant</a>($1);
<a name="l00098"></a>00098            }
<a name="l00099"></a>00099          | DOUBLE
<a name="l00100"></a>00100            {
<a name="l00101"></a>00101                $$ = <span class="keyword">new</span> <a class="code" href="classCNConstant.html" title="Calculation node always returning a constant value.">CNConstant</a>($1);
<a name="l00102"></a>00102            }
<a name="l00103"></a>00103
<a name="l00104"></a>00104 variable : STRING
<a name="l00105"></a>00105            {
<a name="l00106"></a>00106                <span class="keywordflow">if</span> (!driver.calc.existsVariable(*$1)) {
<a name="l00107"></a>00107                    error(yyloc, std::string(<span class="stringliteral">"Unknown variable \""</span>) + *$1 + <span class="stringliteral">"\""</span>);
<a name="l00108"></a>00108                    <span class="keyword">delete</span> $1;
<a name="l00109"></a>00109                    <a class="code" href="parser_8cc.html#f1eef6197be78122699013d0784acc80">YYERROR</a>;
<a name="l00110"></a>00110                }
<a name="l00111"></a>00111                <span class="keywordflow">else</span> {
<a name="l00112"></a>00112                    $$ = <span class="keyword">new</span> <a class="code" href="classCNConstant.html" title="Calculation node always returning a constant value.">CNConstant</a>( driver.calc.getVariable(*$1) );
<a name="l00113"></a>00113                    <span class="keyword">delete</span> $1;
<a name="l00114"></a>00114                }
<a name="l00115"></a>00115            }
<a name="l00116"></a>00116
<a name="l00117"></a>00117 atomexpr : constant
<a name="l00118"></a>00118            {
<a name="l00119"></a>00119                $$ = $1;
<a name="l00120"></a>00120            }
<a name="l00121"></a>00121          | variable
<a name="l00122"></a>00122            {
<a name="l00123"></a>00123                $$ = $1;
<a name="l00124"></a>00124            }
<a name="l00125"></a>00125          | <span class="charliteral">'('</span> expr <span class="charliteral">')'</span>
<a name="l00126"></a>00126            {
<a name="l00127"></a>00127                $$ = $2;
<a name="l00128"></a>00128            }
<a name="l00129"></a>00129
<a name="l00130"></a>00130 powexpr : atomexpr
<a name="l00131"></a>00131           {
<a name="l00132"></a>00132               $$ = $1;
<a name="l00133"></a>00133           }
<a name="l00134"></a>00134         | atomexpr <span class="charliteral">'^'</span> powexpr
<a name="l00135"></a>00135           {
<a name="l00136"></a>00136               $$ = <span class="keyword">new</span> <a class="code" href="classCNPower.html" title="Calculation node raising one operand to the power of the second.">CNPower</a>($1, $3);
<a name="l00137"></a>00137           }
<a name="l00138"></a>00138
<a name="l00139"></a>00139 unaryexpr : powexpr
<a name="l00140"></a>00140             {
<a name="l00141"></a>00141                 $$ = $1;
<a name="l00142"></a>00142             }
<a name="l00143"></a>00143           | <span class="charliteral">'+'</span> powexpr
<a name="l00144"></a>00144             {
<a name="l00145"></a>00145                 $$ = $2;
<a name="l00146"></a>00146             }
<a name="l00147"></a>00147           | <span class="charliteral">'-'</span> powexpr
<a name="l00148"></a>00148             {
<a name="l00149"></a>00149                 $$ = <span class="keyword">new</span> <a class="code" href="classCNNegate.html" title="Calculation node negating the value of the operand subtree.">CNNegate</a>($2);
<a name="l00150"></a>00150             }
<a name="l00151"></a>00151
<a name="l00152"></a>00152 mulexpr : unaryexpr
<a name="l00153"></a>00153           {
<a name="l00154"></a>00154               $$ = $1;
<a name="l00155"></a>00155           }
<a name="l00156"></a>00156         | mulexpr <span class="charliteral">'*'</span> unaryexpr
<a name="l00157"></a>00157           {
<a name="l00158"></a>00158               $$ = <span class="keyword">new</span> <a class="code" href="classCNMultiply.html" title="Calculation node multiplying two operand nodes.">CNMultiply</a>($1, $3);
<a name="l00159"></a>00159           }
<a name="l00160"></a>00160         | mulexpr <span class="charliteral">'/'</span> unaryexpr
<a name="l00161"></a>00161           {
<a name="l00162"></a>00162               $$ = <span class="keyword">new</span> <a class="code" href="classCNDivide.html" title="Calculation node dividing two operand nodes.">CNDivide</a>($1, $3);
<a name="l00163"></a>00163           }
<a name="l00164"></a>00164         | mulexpr <span class="charliteral">'%'</span> unaryexpr
<a name="l00165"></a>00165           {
<a name="l00166"></a>00166               $$ = <span class="keyword">new</span> <a class="code" href="classCNModulo.html" title="Calculation node calculating the remainder of an integer division of two operand...">CNModulo</a>($1, $3);
<a name="l00167"></a>00167           }
<a name="l00168"></a>00168
<a name="l00169"></a>00169 addexpr : mulexpr
<a name="l00170"></a>00170           {
<a name="l00171"></a>00171               $$ = $1;
<a name="l00172"></a>00172           }
<a name="l00173"></a>00173         | addexpr <span class="charliteral">'+'</span> mulexpr
<a name="l00174"></a>00174           {
<a name="l00175"></a>00175               $$ = <span class="keyword">new</span> <a class="code" href="classCNAdd.html" title="Calculation node adding two operand nodes.">CNAdd</a>($1, $3);
<a name="l00176"></a>00176           }
<a name="l00177"></a>00177         | addexpr <span class="charliteral">'-'</span> mulexpr
<a name="l00178"></a>00178           {
<a name="l00179"></a>00179               $$ = <span class="keyword">new</span> <a class="code" href="classCNSubtract.html" title="Calculation node subtracting two operand nodes.">CNSubtract</a>($1, $3);
<a name="l00180"></a>00180           }
<a name="l00181"></a>00181
<a name="l00182"></a>00182 expr    : addexpr
<a name="l00183"></a>00183           {
<a name="l00184"></a>00184               $$ = $1;
<a name="l00185"></a>00185           }
<a name="l00186"></a>00186
<a name="l00187"></a>00187 assignment : STRING <span class="charliteral">'='</span> expr
<a name="l00188"></a>00188              {
<a name="l00189"></a>00189                  driver.calc.variables[*$1] = $3-&gt;evaluate();
<a name="l00190"></a>00190                  std::cout &lt;&lt; <span class="stringliteral">"Setting variable "</span> &lt;&lt; *$1
<a name="l00191"></a>00191                            &lt;&lt; <span class="stringliteral">" = "</span> &lt;&lt; driver.calc.variables[*$1] &lt;&lt; <span class="stringliteral">"\n"</span>;
<a name="l00192"></a>00192                  <span class="keyword">delete</span> $1;
<a name="l00193"></a>00193                  <span class="keyword">delete</span> $3;
<a name="l00194"></a>00194              }
<a name="l00195"></a>00195
<a name="l00196"></a>00196 start   : <span class="comment">/* empty */</span>
<a name="l00197"></a>00197         | start <span class="charliteral">';'</span>
<a name="l00198"></a>00198         | start EOL
<a name="l00199"></a>00199         | start assignment <span class="charliteral">';'</span>
<a name="l00200"></a>00200         | start assignment EOL
<a name="l00201"></a>00201         | start assignment END
<a name="l00202"></a>00202         | start expr <span class="charliteral">';'</span>
<a name="l00203"></a>00203           {
<a name="l00204"></a>00204               driver.calc.expressions.push_back($2);
<a name="l00205"></a>00205           }
<a name="l00206"></a>00206         | start expr EOL
<a name="l00207"></a>00207           {
<a name="l00208"></a>00208               driver.calc.expressions.push_back($2);
<a name="l00209"></a>00209           }
<a name="l00210"></a>00210         | start expr END
<a name="l00211"></a>00211           {
<a name="l00212"></a>00212               driver.calc.expressions.push_back($2);
<a name="l00213"></a>00213           }
<a name="l00214"></a>00214
<a name="l00215"></a>00215  <span class="comment">/*** END EXAMPLE - Change the example grammar rules above ***/</span>
<a name="l00216"></a>00216
<a name="l00217"></a>00217 %% <span class="comment">/*** Additional Code ***/</span>
<a name="l00218"></a>00218
<a name="l00219"></a>00219 <span class="keywordtype">void</span> <a class="code" href="classexample_1_1Parser.html#47c22356158b28f0e3da36f2254af7bb" title="Report a syntax error.">example::Parser::error</a>(<span class="keyword">const</span> Parser::location_type&amp; l,
<a name="l00220"></a>00220                             <span class="keyword">const</span> std::string&amp; m)
<a name="l00221"></a>00221 {
<a name="l00222"></a>00222     <a class="code" href="classexample_1_1Parser.html#e8fbd18ca0bed43352d5fe35957654f9">driver</a>.error(l, m);
<a name="l00223"></a>00223 }
</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&nbsp;
<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>
RSS 2.0 Weblog Feed Atom 1.0 Weblog Feed Valid XHTML 1.1 Valid CSS (2.1)