%{ #include #include #include #include #include "izrazi.hpp" #include "naredbe.hpp" #include #include using namespace std; void yyerror(string s) { cerr << s << endl; exit(EXIT_FAILURE); } extern int yylex(); map tablica; bool nazubi = false; %} %union { int vrednost; Izraz *i; vector *v; Naredba *n; string *s; } %token ispisi_token pocetak_token kraj_token mod_token ako_je_token onda_token inace_token unesi_token dok_je_token radi_sledece_token %token string_token id_token %token num_token %type E %type NizNaredbi %type Blok Naredba %nonassoc ':' %nonassoc inace_token %left '<' '>' %left '+' '-' %left '*' '/' mod_token %nonassoc UMINUS %% Program: Blok '.' { if (nazubi) { $1->ispisi(0); cout << '.' << endl; } else $1->izvrsi(); delete $1; } ; NizNaredbi: NizNaredbi ';' Naredba { $$ = $1; $$->push_back($3); } | Naredba { $$ = new vector(); $$->push_back($1); } ; Naredba: ispisi_token '(' string_token ')' { $$ = new IspisStringa(*$3); delete $3; } | ispisi_token '(' E ')' { $$ = new IspisIzraza($3); } | Blok { $$ = $1; } | id_token ':' '=' E { $$ = new Dodela(*$1, $4); delete $1; } | ako_je_token E onda_token ':' Naredba inace_token ':' Naredba { $$ = new AkoJeOndaInace($2, $5, $8); } | ako_je_token E onda_token ':' Naredba { $$ = new AkoJeOnda($2, $5); } | unesi_token '(' id_token ')' { $$ = new Unos(*$3); delete $3; } | dok_je_token E radi_sledece_token ':' Naredba { $$ = new DokJe($2, $5); } ; Blok: pocetak_token NizNaredbi kraj_token { $$ = new Blok(*$2); delete $2; } ; 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 '>' E { $$ = new Vece($1, $3); } | E '<' E { $$ = new Manje($1, $3); } | '(' E ')' { $$ = $2; } | E mod_token E { $$ = new Mod($1, $3); } | '-' E %prec UMINUS { $$ = new Minus($2); } | num_token { $$ = new Konstanta($1); } | id_token { $$ = new Promenljiva(*$1); delete $1; } ; %% extern FILE *yyin; int main(int argc, char **argv) { if (argc != 2 && argc != 3) { cerr << "usage: ./pascal [--nazubi] file" << endl; exit(EXIT_FAILURE); } if (argc == 3) { if (string(argv[1]) != "--nazubi") { cerr << "Opcija moze biti samo --nazubi" << endl; exit(EXIT_FAILURE); } nazubi = true; } yyin = fopen(argv[argc - 1], "r"); if (yyin == NULL) { cerr << "Neuspesno otvaranje fajla " << argv[1] << endl; exit(EXIT_FAILURE); } yyparse(); fclose(yyin); return 0; }