%{ #include #include #include #include "funkcije.hpp" #include using namespace std; void yyerror(string s) { cerr << s << endl; exit(EXIT_FAILURE); } int yylex(); map tablica; %} %union { Funkcija *f; double d; char c; } %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()) yyerror("Funkcija vec postoji"); tablica[$2] = $5; } | id_token { map::iterator tmp = tablica.find($1); if (tmp == tablica.end()) yyerror("Funkcija ne postoji"); tmp->second->ispisi(); cout << endl; } | id_token '\'' { map::iterator tmp = tablica.find($1); if (tmp == tablica.end()) yyerror("Funkcija ne postoji"); Funkcija *f = tmp->second->izvod(); f->ispisi(); delete f; cout << endl; } | id_token '[' num_token ']' { map::iterator tmp = tablica.find($1); if (tmp == tablica.end()) yyerror("Funkcija ne postoji"); 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 Konstanta($1); } | 'x' { $$ = new Identicka(); } | E '\'' { $$ = $1->izvod(); delete $1; } ; %% int main() { yyparse(); for (auto i = tablica.begin(); i != tablica.end(); i++) delete i->second; return 0; }