I'm trying to build a scanner-parser to handle input like this x1+x2+x3+x4; and I'm blocked by this error. I understand that when you declare a union in the flex flex2.y file the yylval is connected with this union and after you compile the file flex2.y 2 files are created on the same folder. The one is flex2.tab.y and the other is flex2.tab.h. So after that I have to include the flex2.tab.h file in the flex2.l file so that I can use yylval in the flex2.l file to return the value of the tokens. But for some reason the .l file doesn't recognise the yylval. If someone can point my mistake i would be really grateful.
flex2.y code:
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int yyerror(char *message)
{
return 0;
}
%}
%union {
double val;
char sym;
}
%token <val> NUMBER
%token <sym> PLUS Q_MARK
%type <val> addition_list addition
%start addition_list
%%
addition_list : addition Q_MARK {printf("apotelesma: %d\n", $1);}
| addition_list addition Q_MARK {}
;
addition : NUMBER PLUS NUMBER {$$ = $1 + $3;}
| addition PLUS NUMBER {$$ = $1 + $3;}
;
%%
void main(int argc, char *argv[])
{
yyparse();
}
flex2.l code:
%option noyywrap
%{
#include "flex2.tab.h"
%}
%%
\+ { yylval.sym = yytext[0]; return PLUS; }
; { yylval.sym = yytext[0]; return Q_MARK; }
0|([-+]?(([1-9][0-9]*)|(0\.[0-9]+)|([1-9][0-9]*\.[0-9]+))) {yylval.val = atof(yytext); return NUMBER; }
%%
And here is the flex2.tab.h content where I notice that it sais typedef YYSTYPE somewhere but from what I've read the union is managed by yylval.
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
NUMBER = 258,
PLUS = 259,
Q_MARK = 260
};
#endif
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 1676 of yacc.c */
#line 14 ".\\flex2.y"
double val;
char sym;
/* Line 1676 of yacc.c */
#line 64 "flex2.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE flex2lval;