%{ #include #include #include #include #define YYDEBUG 1 using namespace std; #include "funkcije.hpp" void yyerror(string s) { cerr << s << endl; exit(EXIT_FAILURE); } int yylex(); map tablica; %} %token function_token sin_token cos_token %token id_token %token num_token %left '+' '-' %left '*' '/' %nonassoc '\'' %type E %union { char c; double d; Funkcija *f; } %% Program: Program Naredba | Naredba ; Naredba: function_token id_token '=' '"' E '"' '\n' { tablica[$2] = $5; } | id_token '\n' { map::iterator tmp = tablica.find($1); if (tmp == tablica.end()) yyerror(string("Funkcija ") + $1 + string(" nije definisana")); tmp->second->ispisi(); cout << endl; } | id_token '\'' '\n' { map::iterator tmp = tablica.find($1); if (tmp == tablica.end()) yyerror(string("Funkcija ") + $1 + string(" nije definisana")); Funkcija* izvod = tmp->second->izvod(); izvod->ispisi(); cout << endl; delete izvod; } ; 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); } | 'x' { $$ = new Identicka(); } | num_token { $$ = new Konstanta($1); } | E '\'' { $$ = $1->izvod(); delete $1; } ; %% int main() { //yydebug = 1; yyparse(); return 0; }