<!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/driver.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 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/driver.cc</h1><a href="driver_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: driver.cc 39 2008-08-03 10:07:15Z tb $</span>
<a name="l00004"></a>00004 <span class="comment"></span><span class="preprocessor">#include <fstream></span>
<a name="l00005"></a>00005 <span class="preprocessor">#include <sstream></span>
<a name="l00006"></a>00006
<a name="l00007"></a>00007 <span class="preprocessor">#include "<a class="code" href="driver_8h.html" title="Declaration of the example::Driver class.">driver.h</a>"</span>
<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><a class="code" href="namespaceexample.html">00010</a> <span class="keyword">namespace </span>example {
<a name="l00011"></a>00011
<a name="l00012"></a><a class="code" href="classexample_1_1Driver.html#c7e62776c89658a2d2ff66761bed2a23">00012</a> <a class="code" href="classexample_1_1Driver.html#c7e62776c89658a2d2ff66761bed2a23" title="construct a new parser driver context">Driver::Driver</a>(<span class="keyword">class</span> <a class="code" href="classCalcContext.html" title="Calculator context used to save the parsed expressions.">CalcContext</a>& _calc)
<a name="l00013"></a>00013 : trace_scanning(false),
<a name="l00014"></a>00014 trace_parsing(false),
<a name="l00015"></a>00015 calc(_calc)
<a name="l00016"></a>00016 {
<a name="l00017"></a>00017 }
<a name="l00018"></a>00018
<a name="l00019"></a><a class="code" href="classexample_1_1Driver.html#a02195b250993e74acceb0fdd5e04414">00019</a> <span class="keywordtype">bool</span> <a class="code" href="classexample_1_1Driver.html#a02195b250993e74acceb0fdd5e04414" title="Invoke the scanner and parser for a stream.">Driver::parse_stream</a>(std::istream& in, <span class="keyword">const</span> std::string& sname)
<a name="l00020"></a>00020 {
<a name="l00021"></a>00021 <a class="code" href="classexample_1_1Driver.html#fc2dff95d108322cd7032d192c0a8f92" title="stream name (file or input stream) used for error messages.">streamname</a> = sname;
<a name="l00022"></a>00022
<a name="l00023"></a>00023 <a class="code" href="classexample_1_1Scanner.html" title="Scanner is a derived class to add some extra function to the scanner class.">Scanner</a> scanner(&in);
<a name="l00024"></a>00024 scanner.<a class="code" href="classexample_1_1Scanner.html#5625c85cbc498968f3045cea6fd67fb5" title="Enable debug output (via arg_yyout) if compiled into the scanner.">set_debug</a>(<a class="code" href="classexample_1_1Driver.html#2bb8042bae70236f6c1aaa0b60d3b171" title="enable debug output in the flex scanner">trace_scanning</a>);
<a name="l00025"></a>00025 this-><a class="code" href="classexample_1_1Driver.html#986f4e13999e2f328041740d3ca6e4c2" title="Pointer to the current lexer instance, this is used to connect the parser to the...">lexer</a> = &scanner;
<a name="l00026"></a>00026
<a name="l00027"></a>00027 <a class="code" href="classexample_1_1Parser.html" title="A Bison parser.">Parser</a> parser(*<span class="keyword">this</span>);
<a name="l00028"></a>00028 parser.<a class="code" href="classexample_1_1Parser.html#5fb28eb5133e2711450edeebc8d4e505" title="Set the current debugging level.">set_debug_level</a>(<a class="code" href="classexample_1_1Driver.html#680f62dafcd90219e3b943b9f89a7e5a" title="enable debug output in the bison parser">trace_parsing</a>);
<a name="l00029"></a>00029 <span class="keywordflow">return</span> (parser.<a class="code" href="classexample_1_1Parser.html#dab2581623d812342901cc27f0340b8b" title="Parse.">parse</a>() == 0);
<a name="l00030"></a>00030 }
<a name="l00031"></a>00031
<a name="l00032"></a><a class="code" href="classexample_1_1Driver.html#ffea06783586d2253a4231e89cfd1ff7">00032</a> <span class="keywordtype">bool</span> <a class="code" href="classexample_1_1Driver.html#ffea06783586d2253a4231e89cfd1ff7" title="Invoke the scanner and parser on a file.">Driver::parse_file</a>(<span class="keyword">const</span> std::string &filename)
<a name="l00033"></a>00033 {
<a name="l00034"></a>00034 std::ifstream in(filename.c_str());
<a name="l00035"></a>00035 <span class="keywordflow">if</span> (!in.good()) <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00036"></a>00036 <span class="keywordflow">return</span> <a class="code" href="classexample_1_1Driver.html#a02195b250993e74acceb0fdd5e04414" title="Invoke the scanner and parser for a stream.">parse_stream</a>(in, filename);
<a name="l00037"></a>00037 }
<a name="l00038"></a>00038
<a name="l00039"></a><a class="code" href="classexample_1_1Driver.html#34d3f98359759bd7dbec2ae9cf14ca6f">00039</a> <span class="keywordtype">bool</span> <a class="code" href="classexample_1_1Driver.html#34d3f98359759bd7dbec2ae9cf14ca6f" title="Invoke the scanner and parser on an input string.">Driver::parse_string</a>(<span class="keyword">const</span> std::string &input, <span class="keyword">const</span> std::string& sname)
<a name="l00040"></a>00040 {
<a name="l00041"></a>00041 std::istringstream iss(input);
<a name="l00042"></a>00042 <span class="keywordflow">return</span> <a class="code" href="classexample_1_1Driver.html#a02195b250993e74acceb0fdd5e04414" title="Invoke the scanner and parser for a stream.">parse_stream</a>(iss, sname);
<a name="l00043"></a>00043 }
<a name="l00044"></a>00044
<a name="l00045"></a><a class="code" href="classexample_1_1Driver.html#e86d346bd0baa8e13222f5c38a13623d">00045</a> <span class="keywordtype">void</span> <a class="code" href="classexample_1_1Driver.html#e86d346bd0baa8e13222f5c38a13623d" title="Error handling with associated line number.">Driver::error</a>(<span class="keyword">const</span> <span class="keyword">class</span> <a class="code" href="classexample_1_1location.html" title="Abstract a location.">location</a>& l,
<a name="l00046"></a>00046 <span class="keyword">const</span> std::string& m)
<a name="l00047"></a>00047 {
<a name="l00048"></a>00048 std::cerr << l << <span class="stringliteral">": "</span> << m << std::endl;
<a name="l00049"></a>00049 }
<a name="l00050"></a>00050
<a name="l00051"></a><a class="code" href="classexample_1_1Driver.html#4c1ddc62f8957c89302f9bfa6e75067f">00051</a> <span class="keywordtype">void</span> <a class="code" href="classexample_1_1Driver.html#e86d346bd0baa8e13222f5c38a13623d" title="Error handling with associated line number.">Driver::error</a>(<span class="keyword">const</span> std::string& m)
<a name="l00052"></a>00052 {
<a name="l00053"></a>00053 std::cerr << m << std::endl;
<a name="l00054"></a>00054 }
<a name="l00055"></a>00055
<a name="l00056"></a>00056 } <span class="comment">// namespace example</span>
</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>