Reputation: 24840
I am using bison to implement a simple parser. And one line of the syntax looks like:
prefix_definition : PREFIX IDENTIFIER IDENTIFIER ABBR IDENTIFIER ';'
I am unsure how to access the 1st, 2nd and 3rd IDENTIFIER
separately. My flex file reads the IDENTIFIER
like this:
IDENTIFIER_REGEX (_|[A_Za-z])(_|[0-9A-Za-z])*
{IDENTIFIER_REGEX} { yylval.identifier=strdup(yytext); return IDENTIFIER; }
I could not use simply yylval.identifier
. I tried $2.identifier
or so but it simply does not work(and it is not supposed to be work anyway). Is there any way of solving this problem?
I am considering to use a FIFO queue if the bison/flex does not support such access. Is this a good solution?
Upvotes: 1
Views: 405
Reputation: 37167
You can specify the type of a token while declaring it (in the bison file) the same way you would for nonterminals (where you'd use %type
) like so:
%token <identifier> IDENTIFIER
(where identifier
is one of the fields declared in the %union
). Then $2
, $3
and so on will point to the right thing, without needing to go through yylval
(i.e. they will be char *
s in your case).
Upvotes: 2