00001 // $Id: driver.cc 39 2008-08-03 10:07:15Z tb $ 00004 #include <fstream> 00005 #include <sstream> 00006 00007 #include "driver.h" 00008 #include "scanner.h" 00009 00010 namespace example { 00011 00012 Driver::Driver(class CalcContext& _calc) 00013 : trace_scanning(false), 00014 trace_parsing(false), 00015 calc(_calc) 00016 { 00017 } 00018 00019 bool Driver::parse_stream(std::istream& in, const std::string& sname) 00020 { 00021 streamname = sname; 00022 00023 Scanner scanner(&in); 00024 scanner.set_debug(trace_scanning); 00025 this->lexer = &scanner; 00026 00027 Parser parser(*this); 00028 parser.set_debug_level(trace_parsing); 00029 return (parser.parse() == 0); 00030 } 00031 00032 bool Driver::parse_file(const std::string &filename) 00033 { 00034 std::ifstream in(filename.c_str()); 00035 if (!in.good()) return false; 00036 return parse_stream(in, filename); 00037 } 00038 00039 bool Driver::parse_string(const std::string &input, const std::string& sname) 00040 { 00041 std::istringstream iss(input); 00042 return parse_stream(iss, sname); 00043 } 00044 00045 void Driver::error(const class location& l, 00046 const std::string& m) 00047 { 00048 std::cerr << l << ": " << m << std::endl; 00049 } 00050 00051 void Driver::error(const std::string& m) 00052 { 00053 std::cerr << m << std::endl; 00054 } 00055 00056 } // namespace example