1/* Parser to convert "C" assignments to lisp using Bison in C++. */
2/* Demonstrates bison locations with yylex() taking yylval and yylloc (reflex option bison-locations) */
3/* Compile: bison -d flexexample7.yxx */
4
5%{
6#include <iostream>
7#include "lex.yy.h"  /* Generated by reflex --header-file */
8void yyerror(const char*);
9%}
10
11%locations
12
13%union {
14    int   num;
15    char *str;
16}
17
18%{
19/* Patches old bison versions that don't produce correct YYLEX */
20extern int yylex(YYSTYPE*, YYLTYPE*);
21#define YYLEX_PARAM &yylval, &yylloc
22%}
23
24/* Add &yylval and &yyloc parameters to yylex() with a trick: use YYLEX_PARAM */
25%lex-param { void *YYLEX_PARAM }
26
27%token <str> STRING
28%token <num> NUMBER
29
30%%
31
32assignments : assignment
33            | assignment assignments
34            ;
35assignment  : STRING '=' NUMBER ';' { std::cout << "(setf " << $1 << " " << $3 << ")\n"; }
36            ;
37
38%%
39
40int main()
41{
42  yyparse();
43  return 0;
44}
45
46void yyerror(const char *msg)
47{
48  std::cerr << msg << " at (" << yylloc.first_line << "," << yylloc.first_column << ") to (" << yylloc.last_line << "," << yylloc.last_column << ")" << std::endl;
49}
50