%{ #include #include #include #include "funkcije.hpp" using namespace std; void yyerror(string s) { cerr << s << endl; exit(EXIT_FAILURE); } extern int yylex(); map tablica; %} %union { Funkcija *f; char c; double d; } %type E %token function_token sin_token cos_token %token id_token %token num_token %left '+' '-' %left '*' '/' %nonassoc '\'' %% Program: Program Naredba '\n' | Naredba '\n' ; Naredba: function_token id_token '=' '"' E '"' { map::iterator tmp = tablica.find($2); if (tmp != tablica.end()) { cerr << "Funkcija " << $2 << " je vec definisana" << endl; exit(EXIT_FAILURE); } tablica[$2] = $5; } | id_token { map::iterator tmp = tablica.find($1); if (tmp == tablica.end()) { cerr << "Funkcija " << $1 << " nije definisana" << endl; exit(EXIT_FAILURE); } tmp->second->ispisi(); cout << endl; } | id_token '\'' { map::iterator tmp = tablica.find($1); if (tmp == tablica.end()) { cerr << "Funkcija " << $1 << " nije definisana" << endl; exit(EXIT_FAILURE); } Funkcija *i = tmp->second->izvod(); i->ispisi(); cout << endl; delete i; } ; E: E '+' E { $$ = new Zbir($1, $3); } | E '-' E { $$ = new Razlika($1, $3); } | E '*' E { $$ = new Proizvod($1, $3); } | E '/' E { $$ = new Kolicnik($1, $3); } | num_token { $$ = new Konstanta($1); } | '(' E ')' { $$ = $2; } | 'x' { $$ = new IdFja(); } | sin_token '(' E ')' { $$ = new Sin($3); } | cos_token '(' E ')' { $$ = new Cos($3); } | E '\'' { $$ = $1->izvod(); delete $1; } ; %% int main(int argc, char **argv) { yyparse(); for (map::iterator i = tablica.begin(); i != tablica.end(); i++) delete i->second; return 0; }