This is C Code but I guess it will be an indicative case for C++ too.
When we had to make a compiler in the university we had to define the YY_DECL in a .l
file like this:
#define YY_DECL int alpha_yylex(yylval_) void *yylval_;
The int alpha_yylex(yylval_)
is the function and the void *yylval
is the parameter type.
Clearly, you need this so for the parser to know where to start from aka which function is the one that parsing should be done from.
You also need to extern this function prototype (extern int alpha_yylex(void *yylval_);
) in your .y
file.
After that the Flex/Bison utilities will autogenerate the relevant .c
files for you.
In general, .l
is for the lexical rules and .y
is for grammatical rules. Therefore if you need to get the value from somewhere you must define a union in .y
like this (an example):
%union {
union {
int Integer;
double Double;
char Character;
char* String;
} value;
}
Then, depending on your syntactical and rules, you can do yylval.value.String = strdup(yytext)
in your .l file
and, depending on your grammatical rules, $$.Double = $1.Double + $3.Double
in your .y
file.
I don't remember a lot since it's been a while that I've done that but I guess you can try and you can refer to the documentation or ask here :)
P.S: (I had a good FAQ somewhere but can't remember where the heck it is -> will post later if I am lucky enough to find it xD :D xD)