10

I have searched almost every material online. But I am still confused why lexer cannot identify yylval.

Here is the case: I have defined a bunch of ADT in node.h and realize them in node.c, my purpose is to generate a AST after these structures are properly stored. But I am stucked with bison file.

First, I change %union to union YYSTYPE {...}; and typedef union YYSTYPE YYSTYPE;, I don't why I need to do this, some other files posted online seems to work well with %uinion.

Then, I am stucked with yylval things. I have done bison -d things, and checked it already in parser.c(I have specified the bison output), so I think extern YYSTYPE yylval; should work. But it doesn't. So I wonder if there is another way to solve yylval undeclared problem.

I only use the two types of YYSTYPE struct, int and char *, can I separate the union YYSYTPE and struct for the AST? This means, the nonterminals will have no associated types. Do you guys have any other ideas??

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
chenrui
  • 8,910
  • 3
  • 33
  • 43
  • I still wonder what you mean "separate the union `YYSTYPE` and struct for the AST", and I don't think it necessary. Have you got problems when using `%union` and `%type` in bison? – neuront Jun 14 '11 at 11:44
  • @neuront Yes, I agree with you. We don't need to expplicitly specify YYSTYPE in bison. But the thing is I cannot get thru it without doing that. In other words, although `%union` and `%union YYSTYPE` works in the same way, however, in my case, I have to choose the latter. I don't know why? For `%union` and `%type`, I know the relationship between them. I just know how to solve the error with `YYSTYPE undefined`. When I searched online, I found it a common problem. – chenrui Jun 15 '11 at 13:56

3 Answers3

5

In case it's of help to others, I found (OpenBSD lex v2.5.4 + yacc) that including

extern YYSTYPE yylval;

in my lex source was insufficient to prevent a 'yylval undefined' problem, even though the y.tab.c file contains:

#ifndef YYSTYPE
typedef int YYSTYPE;
#endif

I fixed this by putting an explicit definition in the lex source:

#define YYSTYPE int
extern YYSTYPE yyltype

However, I am unclear whether defining in this way will propagate to the locale of the yacc source file...

Martin
  • 51
  • 1
  • 2
1

You should use %union. You do not need to typedef the union.

You use bison -d to get a header file with any declarations (such as types for terminals, nonterminals, and tokens). Example:

bison -d parser.y Would yield two files, parser.tab.c and parser.tab.h. You can include the latter file into your lexer file, so it'll know about your %token definitions. This file also includes the definition of yylval and its type.

You should not separate the union because you'll most likely expand it and need it for communicating between the lexer and parser.

Kizaru
  • 2,443
  • 3
  • 24
  • 39
0

Do you have %pure-parser (or similar) set in your .y file? This disables the normal lex declarations, as they are not re-entrant.

https://www.gnu.org/software/bison/manual/html_node/Pure-Decl.html

aaa90210
  • 11,295
  • 13
  • 51
  • 88