%{ #include #include #include #include #include #include #include #include #include using namespace std; extern "C" int yylex(); extern "C" int yyparse(); extern "C" FILE *yyin; void yyerror(const char *s); vector nameTable; int getVarID(string name) { auto it=nameTable.end(); for(auto it=nameTable.begin(); it!=nameTable.end(); it++) { if(nameTable[it-nameTable.begin()]==name) return it-nameTable.begin(); } nameTable.push_back(name); return nameTable.size()-1; } vector argTable; int getArgID(string name) { auto it=argTable.end(); for(auto it=argTable.begin(); it!=argTable.end(); it++) { if(argTable[it-argTable.begin()]==name) { return (int)(it-argTable.begin())-(int)(argTable.size())-1; } } return 0; } void createArg(string arg) { argTable.push_back(arg); } ofstream out; %} %union { int ival; char* sval; } %token INT %token PLUS %token MINUS %token MUL %token LPARENS %token RPARENS %token DIV %token VARNAME %token EQ %token COMMA %token FUNCTION %token END %left PLUS MINUS MUL DIV FUNCTION %right EQ %% program: fh function ef program | fh function ef fh: FUNCTION VARNAME LPARENS args RPARENS {out << $2 << ":\n\tbpget\n\tspget\n\tbpset" << endl;} | FUNCTION VARNAME LPARENS RPARENS {out << $2 << ":\n\tbpget\n\tspget\n\tbpset" << endl;} args: VARNAME COMMA args {createArg($1);} | VARNAME {createArg($1);} ef: END {out << "\tbpget\n\tspset\n\tbpset\n\tret"<