Reputation: 190659
I'm working on a bison c++ parser.
Most examples has error method with parameter location&
in .y file, but I'm not sure how to get the location_type to call this method.
typedef location location_type;
void
yy::c_parser::error (const location_type& l,
const std::string& m)
{
driver.error (l, m);
}
This is an example excerpt from http://panthema.net/2007/flex-bison-cpp-example/,
if (!driver.calc.existsVariable(*$1)) {
error(yyloc, std::string("Unknown variable \"") + *$1 + "\"");
However, I got an error that parser.yy:109: error: ‘yyloc’ was not declared in this scope
when compiling it.
Upvotes: 2
Views: 960
Reputation: 8759
Your question is slightly unclear: where do you want to call yyerror
from?
If you want to call it from the parser, then just use the @n
pseudo-variables:
exp: exp "/" exp
{
if (!$3)
{
yyerror(@3, "division by zero");
YYERROR;
}
else
{
$$ = $1 / $3;
}
}
If you want to call it from the scanner, then use the variable used there to denote the current location, probably something like yylloc
.
If you want to call it from elsewhere (e.g., from an AST traversal, but that would be weird), then find the location there.
But more importantly (sorry, I may state something that you already know): be aware that you typically do not need to call yyerror
: you have to provide it, so that parser can raise errors. Typical calls to yyerror
are in the generated code, not in code you are expected to write.
Upvotes: 2