Thursday, 17 November 2016

Flex/Bison error lines compatible with Visual Studio

Flex and Bison can be built from source, to be found at github.com/AaronNGray/winflexbison. To get errors and warnings that you can double-click in Visual Studio, you can modify the code as follows:

In the file bison\src\location.c, find the function unsigned location_print (location loc, FILE *out) - and replace it with:

unsigned
location_print (location loc, FILE *out)
{
  unsigned res = 0;
  int end_col = 0 != loc.end.column ? loc.end.column - 1 : 0;
  res += fprintf (out, "%s",
                  quotearg_n_style (3, escape_quoting_style, loc.start.file));
  if(0 <= loc.start.line||loc.start.file != loc.end.file||0 <= loc.end.line)
      res += fprintf (out, "(");
  if (0 <= loc.start.line)
    {
      res += fprintf (out, "%d", loc.start.line);
      if (0 <= loc.start.column)
        res += fprintf (out, ",%d", loc.start.column);
    }
  if (loc.start.file != loc.end.file)
    {
  // Ignore: Visual Studio can't cope with this case.
    }
  if(0 <= loc.start.line||loc.start.file != loc.end.file||0 <= loc.end.line)
      res += fprintf (out, ")");
  return res;
}
That fixes it for Bison. For Flex, in flex/src/parse.c, change the function line_pinpoint( str, line ) to
void line_pinpoint( str, line )
const char *str;
int line;
 {
 fprintf( stderr, "%s(%d): %s\n", infilename, line, str );
 }

My branch incorporating these changes is here.