idlebox / 2007 / flex-bison-cpp-example / flex-bison-cpp-example-0.1.4 / doxygen-html / exprtest_8cc_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/exprtest.cc 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/exprtest.cc</h1><a href="exprtest_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// $Id: exprtest.cc 18 2007-08-19 19:51:52Z tb $</span>
<a name="l00002"></a>00002
<a name="l00003"></a>00003 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;fstream&gt;</span>
<a name="l00005"></a>00005
<a name="l00006"></a>00006 <span class="preprocessor">#include "<a class="code" href="driver_8h.html" title="Declaration of the example::Driver class.">driver.h</a>"</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include "<a class="code" href="expression_8h.html" title="Implements an example calculator class group.">expression.h</a>"</span>
<a name="l00008"></a>00008
<a name="l00009"></a><a class="code" href="exprtest_8cc.html#0ddf1224851353fc92bfbff6f499fa97">00009</a> <span class="keywordtype">int</span> <a class="code" href="exprtest_8cc.html#0ddf1224851353fc92bfbff6f499fa97">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[])
<a name="l00010"></a>00010 {
<a name="l00011"></a>00011     <a class="code" href="classCalcContext.html" title="Calculator context used to save the parsed expressions.">CalcContext</a> calc;
<a name="l00012"></a>00012     <a class="code" href="classexample_1_1Driver.html" title="The Driver class brings together all components.">example::Driver</a> driver(calc);
<a name="l00013"></a>00013     <span class="keywordtype">bool</span> readfile = <span class="keyword">false</span>;
<a name="l00014"></a>00014
<a name="l00015"></a>00015     <span class="keywordflow">for</span>(<span class="keywordtype">int</span> ai = 1; ai &lt; argc; ++ai)
<a name="l00016"></a>00016     {
<a name="l00017"></a>00017         <span class="keywordflow">if</span> (argv[ai] == std::string (<span class="stringliteral">"-p"</span>)) {
<a name="l00018"></a>00018             driver.<a class="code" href="classexample_1_1Driver.html#680f62dafcd90219e3b943b9f89a7e5a" title="enable debug output in the bison parser">trace_parsing</a> = <span class="keyword">true</span>;
<a name="l00019"></a>00019         }
<a name="l00020"></a>00020         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (argv[ai] == std::string (<span class="stringliteral">"-s"</span>)) {
<a name="l00021"></a>00021             driver.<a class="code" href="classexample_1_1Driver.html#2bb8042bae70236f6c1aaa0b60d3b171" title="enable debug output in the flex scanner">trace_scanning</a> = <span class="keyword">true</span>;
<a name="l00022"></a>00022         }
<a name="l00023"></a>00023         <span class="keywordflow">else</span>
<a name="l00024"></a>00024         {
<a name="l00025"></a>00025             <span class="comment">// read a file with expressions</span>
<a name="l00026"></a>00026
<a name="l00027"></a>00027             std::fstream infile(argv[ai]);
<a name="l00028"></a>00028             <span class="keywordflow">if</span> (!infile.good())
<a name="l00029"></a>00029             {
<a name="l00030"></a>00030                 std::cerr &lt;&lt; <span class="stringliteral">"Could not open file: "</span> &lt;&lt; argv[ai] &lt;&lt; std::endl;
<a name="l00031"></a>00031                 <span class="keywordflow">return</span> 0;
<a name="l00032"></a>00032             }
<a name="l00033"></a>00033
<a name="l00034"></a>00034             calc.<a class="code" href="classCalcContext.html#8121847568350b2eb3073c64f15696cc" title="free all saved expression trees">clearExpressions</a>();
<a name="l00035"></a>00035             <span class="keywordtype">bool</span> result = driver.<a class="code" href="classexample_1_1Driver.html#a02195b250993e74acceb0fdd5e04414" title="Invoke the scanner and parser for a stream.">parse_stream</a>(infile, argv[ai]);
<a name="l00036"></a>00036             <span class="keywordflow">if</span> (result)
<a name="l00037"></a>00037             {
<a name="l00038"></a>00038                 std::cout &lt;&lt; <span class="stringliteral">"Expressions:"</span> &lt;&lt; std::endl;
<a name="l00039"></a>00039                 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> ei = 0; ei &lt; calc.<a class="code" href="classCalcContext.html#b1847ab7be9ec42a7e448d49c1fe8c6b" title="array of unassigned expressions found by the parser.">expressions</a>.size(); ++ei)
<a name="l00040"></a>00040                 {
<a name="l00041"></a>00041                     std::cout &lt;&lt; <span class="stringliteral">"["</span> &lt;&lt; ei &lt;&lt; <span class="stringliteral">"]:"</span> &lt;&lt; std::endl;
<a name="l00042"></a>00042                     std::cout &lt;&lt; <span class="stringliteral">"tree:"</span> &lt;&lt; std::endl;
<a name="l00043"></a>00043                     calc.<a class="code" href="classCalcContext.html#b1847ab7be9ec42a7e448d49c1fe8c6b" title="array of unassigned expressions found by the parser.">expressions</a>[ei]-&gt;print(std::cout);
<a name="l00044"></a>00044                     std::cout &lt;&lt; <span class="stringliteral">"evaluated: "</span>
<a name="l00045"></a>00045                               &lt;&lt; calc.<a class="code" href="classCalcContext.html#b1847ab7be9ec42a7e448d49c1fe8c6b" title="array of unassigned expressions found by the parser.">expressions</a>[ei]-&gt;evaluate()
<a name="l00046"></a>00046                               &lt;&lt; std::endl;
<a name="l00047"></a>00047                 }
<a name="l00048"></a>00048             }
<a name="l00049"></a>00049
<a name="l00050"></a>00050             readfile = <span class="keyword">true</span>;
<a name="l00051"></a>00051         }
<a name="l00052"></a>00052     }
<a name="l00053"></a>00053
<a name="l00054"></a>00054     <span class="keywordflow">if</span> (readfile) <span class="keywordflow">return</span> 0;
<a name="l00055"></a>00055    
<a name="l00056"></a>00056     std::cout &lt;&lt; <span class="stringliteral">"Reading expressions from stdin"</span> &lt;&lt; std::endl;
<a name="l00057"></a>00057
<a name="l00058"></a>00058     std::string line;
<a name="l00059"></a>00059     <span class="keywordflow">while</span>( std::cout &lt;&lt; <span class="stringliteral">"input: "</span> &amp;&amp;
<a name="l00060"></a>00060            std::getline(std::cin, line) &amp;&amp;
<a name="l00061"></a>00061            !line.empty() )
<a name="l00062"></a>00062     {
<a name="l00063"></a>00063         calc.<a class="code" href="classCalcContext.html#8121847568350b2eb3073c64f15696cc" title="free all saved expression trees">clearExpressions</a>();
<a name="l00064"></a>00064         <span class="keywordtype">bool</span> result = driver.<a class="code" href="classexample_1_1Driver.html#34d3f98359759bd7dbec2ae9cf14ca6f" title="Invoke the scanner and parser on an input string.">parse_string</a>(line, <span class="stringliteral">"input"</span>);
<a name="l00065"></a>00065
<a name="l00066"></a>00066         <span class="keywordflow">if</span> (result)
<a name="l00067"></a>00067         {
<a name="l00068"></a>00068             <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> ei = 0; ei &lt; calc.<a class="code" href="classCalcContext.html#b1847ab7be9ec42a7e448d49c1fe8c6b" title="array of unassigned expressions found by the parser.">expressions</a>.size(); ++ei)
<a name="l00069"></a>00069             {
<a name="l00070"></a>00070                 std::cout &lt;&lt; <span class="stringliteral">"tree:"</span> &lt;&lt; std::endl;
<a name="l00071"></a>00071                 calc.<a class="code" href="classCalcContext.html#b1847ab7be9ec42a7e448d49c1fe8c6b" title="array of unassigned expressions found by the parser.">expressions</a>[ei]-&gt;print(std::cout);
<a name="l00072"></a>00072                 std::cout &lt;&lt; <span class="stringliteral">"evaluated: "</span>
<a name="l00073"></a>00073                           &lt;&lt; calc.<a class="code" href="classCalcContext.html#b1847ab7be9ec42a7e448d49c1fe8c6b" title="array of unassigned expressions found by the parser.">expressions</a>[ei]-&gt;evaluate()
<a name="l00074"></a>00074                           &lt;&lt; std::endl;
<a name="l00075"></a>00075             }
<a name="l00076"></a>00076         }
<a name="l00077"></a>00077     }
<a name="l00078"></a>00078 }
</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)