I am writing a parser with flex and bison and so far have these tokens for flex:
[ \t\n] ;
(x[0-9]+) {
yylval.var = strdup(yytext);
return VARIABLE;
}
~(x[0-9]+) {
yylval.var = strdup(yytext);
return NEG_VARIABLE;
}
[a-zA-Z0-9]+ {
yylval.name = strdup(yytext);
return NAME;
}
~[a-zA-Z0-9]+ {
yylval.name = strdup(yytext);
return NEG_NAME;
}
[\{\},\(\)] { return yytext[0];}
. ;
and these parse rules for bison are:
fol:
clauses {cout << "Done with file"<<endl;}
;
clauses:
clauses clause
| clause
;
clause:
startc terms endc
;
startc:
'{' {cout << "Bison found start of clause" << endl;}
;
endc:
'}' {cout << "Bison found end of clause" << endl;}
;
function:
NAME startfun endfun {cout << "Bison found a function " << $1 << endl;}
|NEG_NAME startfun endfun {cout << "Bison found a negative function " << $1 << endl;}
;
startfun:
'(' {cout << "Bison found start of params" << endl;}
;
endfun:
terms ')' {cout << "Bison found a function end" << endl;}
;
terms:
terms ',' term
| term
;
term:
VARIABLE {cout << "Bison found a variable "<< $1 << endl;}
| NEG_VARIABLE {cout << "Bison found a negative variable " << $1 << endl;}
| NAME {cout << "Bison found a constant " << $1 << endl;}
|function
;
Now it all works great except that when it parses a function it first parses the parameters and parens and then gives me the function name at the end. I can work around this, but it makes my life harder since I am storing functions as disjoint sets and I would need to keep a list of the parameters until I can get the name of the function to create the root and then union them in instead of creating it directly.
Can anyone show me how to get Bison to give me the function name before the parameters? I have been trying for over an hour with no luck.