Inconsistent generation of tab.code.c using a separate header tab.h
zosrothko opened this issue · 1 comments
Hi
The CLI option -d creates a header file called prefix.tab.h along with prefix.tab.c, containing the symbol definitions and a declaration for YYSTYPE and yylval. If the union YYSTYPE contains C or C++ types that need an include like for exemple , then the generated code does not compile, while without the option -d, the generated code compiles without error.
%{
#include <vector>
#include <string>
using namespace std;
%}
%left LO '+' '-'
%left HI '*' '/' '%'
%nonassoc UNARY
%union {
string* s;
long val;
long (*binop)(long, long);
long (*unop)(long);
}
With the snippet above,
btyacc -E -l -d -v -r -b t3 t3.y generates the t3.code.c below
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Yshort;
#include "t3.tab.h"
#include <vector>
#include <string>
using namespace std;
and t3.tab.h
#ifndef _t3_tab_h_
#define _t3_tab_h_
#ifdef __cplusplus
enum class YYTOKEN {
#else
enum YYTOKEN {
#endif
LO = 257,
HI = 258,
UNARY = 259,
};
#define YYERRCODE 256
typedef union {
string* s; // line 17
long val;
long (*binop)(long, long);
long (*unop)(long);
} YYSTYPE;
#define YYSTYPE YYSTYPE
extern YYSTYPE yylval;
#if defined(YYPOSN)
extern YYPOSN yyposn;
#endif
#endif
which results in a compile error:
error C4430: missing type specifier line 17
I would suggest to add a new Yacc block introduced by the keyword %include or the keyword %header as
%include {
#include <string>
%}
This block could be generated at the top of the prefix.tab.h so that all include files be included as needed.
Fixed -- the #include now appears in the tab.c file where the union declaration would have been without the -d flag -- it is after any %{
...%}
blocks that are before the %union
and before any blocks that are after it.