/* Parser to convert "C" assignments to lisp using Bison in C++. */
/* Demonstrates bison locations with yylex() taking yylval and yylloc (reflex option bison-locations) */
/* Compile: bison -d flexexample7.yxx */

%{
#include <iostream>
#include "lex.yy.h"  /* Generated by reflex --header-file */
void yyerror(const char*);
%}

%locations

%union {
    int   num;
    char *str;
}

%{
/* Patches old bison versions that don't produce correct YYLEX */
extern int yylex(YYSTYPE*, YYLTYPE*);
#define YYLEX_PARAM &yylval, &yylloc
%}

/* Add &yylval and &yyloc parameters to yylex() with a trick: use YYLEX_PARAM */
%lex-param { void *YYLEX_PARAM }

%token <str> STRING
%token <num> NUMBER

%%

assignments : assignment
            | assignment assignments
            ;
assignment  : STRING '=' NUMBER ';' { std::cout << "(setf " << $1 << " " << $3 << ")\n"; }
            ;

%%

int main()
{
  yyparse();
  return 0;
}

void yyerror(const char *msg)
{
  std::cerr << msg << " at (" << yylloc.first_line << "," << yylloc.first_column << ") to (" << yylloc.last_line << "," << yylloc.last_column << ")" << std::endl;
}
