c - Using union in yacc file -
i'm trying develop basic compiler , i'm using union yylval
follows:
%{ #include <string.h> #include <stdio.h> struct info { int line; /* details unimportant */ }; %} %union{ char *str; struct info *ptr; }
in lexer definition, have
%{ #include "parse.tab.h" %}
but when compile generated lexer, following errors:
y.tab.h: unknown type name 'yystype'. error: request member str in not structure or union.
do need #define yystype
well?
(i edited original question insert enough information source files make question answerable. mistakes in transcription fault , apologize -- rici.)
no. if use %union
declaration, must not #define yystype
; bison manual makes clear.
however, necessary declarations -- in case, declaration of struct info
-- must included in lexer description file (parse.l
) well. 2 generated files independent of each other, fact struct info
declared in parser not make definition automatically available lexer.
in order avoid repeating declarations, idea put them in separate header file:
file: info.h (added)
#ifndef info_h_header_ #define info_h_header_ struct info { int line; /* details unimportant */ }; // ... #endif
file: parse.y (now #include's info.h instead of in-line struct declaration)
%{ #include <stdio.h> #include <string.h> #include "info.h" %} %union{ char *str; struct info *ptr; }
file: parse.l (also #includes info.h)
%{ #include <stdio.h> #include <string.h> /* must come *before* including parse.tab.h */ #include "info.h" #include "parse.tab.h" %}
Comments
Post a Comment