06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 10: C++ Language Interface 105<br />

To encapsulate the coordination with the Flex scanner, it is useful to have two members<br />

function to open and close the scanning phase. members.<br />

// Handling the scanner.<br />

void scan_begin ();<br />

void scan_end ();<br />

bool trace_scanning;<br />

Similarly for the parser itself.<br />

// Handling the parser.<br />

void parse (const std::string& f);<br />

std::string file;<br />

bool trace_parsing;<br />

To demonstrate pure handling of parse errors, instead of simply dumping them on the<br />

standard error output, we will pass them to the compiler driver using the following two<br />

member functions. Finally, we close the class declaration and CPP guard.<br />

// Error handling.<br />

void error (const yy::location& l, const std::string& m);<br />

void error (const std::string& m);<br />

};<br />

#endif // ! CALCXX_DRIVER_HH<br />

The implementation of the driver is straightforward. The parse member function deserves<br />

some attention. The error functions are simple stubs, they should actually register<br />

the located error messages and set error state.<br />

#include "calc++-driver.hh"<br />

#include "calc++-parser.hh"<br />

calcxx_driver::calcxx_driver ()<br />

: trace_scanning (false), trace_parsing (false)<br />

{<br />

variables["one"] = 1;<br />

variables["two"] = 2;<br />

}<br />

calcxx_driver::~calcxx_driver ()<br />

{<br />

}<br />

void<br />

calcxx_driver::parse (const std::string &f)<br />

{<br />

file = f;<br />

scan_begin ();<br />

yy::calcxx_parser parser (*this);<br />

parser.set_debug_level (trace_parsing);<br />

parser.parse ();<br />

scan_end ();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!