Manual
Manual
Manual
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
104 Bison 2.3<br />
10.2.1 Calc++ — C++ Calculator<br />
Of course the grammar is dedicated to arithmetics, a single expression, possibly preceded<br />
by variable assignments. An environment containing possibly predefined variables such as<br />
one and two, is exchanged with the parser. An example of valid input follows.<br />
three := 3<br />
seven := one + two * three<br />
seven * seven<br />
10.2.2 Calc++ Parsing Driver<br />
To support a pure interface with the parser (and the scanner) the technique of the “parsing<br />
context” is convenient: a structure containing all the data to exchange. Since, in addition<br />
to simply launch the parsing, there are several auxiliary tasks to execute (open the file for<br />
parsing, instantiate the parser etc.), we recommend transforming the simple parsing context<br />
structure into a fully blown parsing driver class.<br />
The declaration of this driver class, ‘calc++-driver.hh’, is as follows. The first part<br />
includes the CPP guard and imports the required standard library components, and the<br />
declaration of the parser class.<br />
#ifndef CALCXX_DRIVER_HH<br />
# define CALCXX_DRIVER_HH<br />
# include <br />
# include <br />
# include "calc++-parser.hh"<br />
Then comes the declaration of the scanning function. Flex expects the signature of yylex<br />
to be defined in the macro YY_DECL, and the C++ parser expects it to be declared. We can<br />
factor both as follows.<br />
// Announce to Flex the prototype we want for lexing function, ...<br />
# define YY_DECL \<br />
yy::calcxx_parser::token_type \<br />
yylex (yy::calcxx_parser::semantic_type* yylval, \<br />
yy::calcxx_parser::location_type* yylloc, \<br />
calcxx_driver& driver)<br />
// ... and declare it for the parser’s sake.<br />
YY_DECL;<br />
The calcxx_driver class is then declared with its most obvious members.<br />
// Conducting the whole scanning and parsing of Calc++.<br />
class calcxx_driver<br />
{<br />
public:<br />
calcxx_driver ();<br />
virtual ~calcxx_driver ();<br />
std::map variables;<br />
int result;