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