Manual
Manual
Manual
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 109<br />
end position is advanced of its width. In case it matched ends of lines, the end cursor is<br />
adjusted, and each time blanks are matched, the begin cursor is moved onto the end cursor<br />
to effectively ignore the blanks preceding tokens. Comments would be treated equally.<br />
%{<br />
# define YY_USER_ACTION yylloc->columns (yyleng);<br />
%}<br />
%%<br />
%{<br />
yylloc->step ();<br />
%}<br />
{blank}+ yylloc->step ();<br />
[\n]+ yylloc->lines (yyleng); yylloc->step ();<br />
The rules are simple, just note the use of the driver to report errors. It is convenient to use<br />
a typedef to shorten yy::calcxx_parser::token::identifier into token::identifier<br />
for instance.<br />
%{<br />
typedef yy::calcxx_parser::token token;<br />
%}<br />
/* Convert ints to the actual type of tokens. */<br />
[-+*/] return yy::calcxx_parser::token_type (yytext[0]);<br />
":=" return token::ASSIGN;<br />
{int} {<br />
errno = 0;<br />
long n = strtol (yytext, NULL, 10);<br />
if (! (INT_MIN sval = new std::string (yytext); return token::IDENTIFIER;<br />
. driver.error (*yylloc, "invalid character");<br />
%%<br />
Finally, because the scanner related driver’s member function depend on the scanner’s data,<br />
it is simpler to implement them in this file.<br />
void<br />
calcxx_driver::scan_begin ()<br />
{<br />
yy_flex_debug = trace_scanning;<br />
if (!(yyin = fopen (file.c_str (), "r")))<br />
error (std::string ("cannot open ") + file);<br />
}<br />
void<br />
calcxx_driver::scan_end ()<br />
{<br />
fclose (yyin);