2

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.

prosseek
  • 182,215
  • 215
  • 566
  • 871
  • May be related to [SO/how-does-flex-support-bison-location-exactly](http://stackoverflow.com/questions/656703/how-does-flex-support-bison-location-exactly) – Jarod42 Jan 05 '14 at 23:41

1 Answers1

2

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.

akim
  • 8,255
  • 3
  • 44
  • 60
  • Where is yyloc defined? Error: Undeclared identifier. I can't locate it in the bison parser files. – Linus Fernandes Jun 30 '21 at 04:57
  • It depends what you are looking for: location in the scanner, in the parser? One the scanner side, I don't know what name you chose. In the grammar rules, that's @n. In other places, such as yyerror, it's an additional argument. Also, what language? Read the documentation for all the details. For instance in C++: https://www.gnu.org/software/bison/manual/html_node/C_002b_002b-Location-Values.html. – akim Jul 01 '21 at 06:28
  • I was trying to enable locations for this example :https://www.gnu.org/software/bison/manual/html_node/A-Simple-C_002b_002b-Example.html#A-Simple-C_002b_002b-Example The method signatures change with locations enabled. Now, the user-defined makexxx methods don't have access to the location object and the symbol_type constructors need location_type as parameter. I'm new to Bison and Flex. – Linus Fernandes Jul 01 '21 at 06:59
  • You have an example using locations and make_* functions [here](https://github.com/akimd/bison/blob/master/examples/c%2B%2B/variant-11.yy). – akim Jul 01 '21 at 07:45
  • Thanks @akim. I did assume that the location object would have to be created. Just wasn't sure what to fill into its contents. – Linus Fernandes Jul 01 '21 at 09:01