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?
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:(where
identifieris one of the fields declared in the%union). Then$2,$3and so on will point to the right thing, without needing to go throughyylval(i.e. they will bechar *s in your case).