%{ #include #include #include #include "syn_tree.hpp" #include #define YYDEBUG 1 using namespace std; int yylex(); void yyerror(string s) { cerr << s << endl; exit(EXIT_FAILURE); } %} %union { int i; string *s; SynTree *t; vector *v; } %nonassoc then_token %nonassoc else_token %left '<' '>' %left '+' '-' %left '*' '/' %token begin_token end_token print_token while_token do_token if_token then_token else_token %token num_token %token id_token %type E Naredba Blok %type Niz %% Program: Blok '.' { $1->compile(); delete $1; } ; Blok: begin_token Niz end_token { $$ = new Seq(*$2); delete $2; } ; Niz: Niz ';' Naredba { $$ = $1; $$->push_back($3); } | Naredba { $$ = new vector(); $$->push_back($1); } ; Naredba: id_token ':' '=' E { $$ = new Assign(new Variable(*$1), $4); delete $1; } | print_token E { $$ = new Print($2); } | while_token E do_token Naredba { $$ = new While($2, $4); } | Blok { $$ = $1; } | { $$ = new Eps(); } | if_token E then_token Naredba else_token Naredba { $$ = new IfThenElse($2, $4, $6); } | if_token E then_token Naredba { $$ = new IfThen($2, $4); } ; E: E '+' E { $$ = new Add($1, $3); } | E '-' E { $$ = new Sub($1, $3); } | E '*' E { $$ = new Mul($1, $3); } | E '/' E { $$ = new Div($1, $3); } | E '>' E { $$ = new Greater($1, $3); } | E '<' E { $$ = new Less($1, $3); } | '(' E ')' { $$ = $2; } | num_token { $$ = new Constant($1); } | id_token { $$ = new Variable(*$1); delete $1; } ; %% int main() { //yydebug = 1; yyparse(); return 0; }