Professional Documents
Culture Documents
Lekser
%{
#include "times.tab.h"
%}
%%
[ \t]+ ;
%%
Parser
%{
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int elem;
}* Node;
%}
%union {
int integer;
%token PLUS
%token MINUS
%token ASTERISK
%token OPEN_PARENTHESES
%token CLOSE_PARENTHESES
%type <node_ptr> e
%left PLUS
%left MINUS
%left ASTERISK
%%
e : e PLUS e
$$ = (Node)malloc(sizeof(struct node_str));
$$->elem = '+';
$$->left = $1;
$$->right = $3;
ast = $$;
| e MINUS e
{
$$ = (Node)malloc(sizeof(struct node_str));
$$->elem = '-';
$$->left = $1;
$$->right = $3;
ast = $$;
| e ASTERISK e
$$ = (Node)malloc(sizeof(struct node_str));
$$->elem = '*';
$$->left = $1;
$$->right = $3;
ast = $$;
$$ = $2;
| INTCONST
$$ = (Node)malloc(sizeof(struct node_str));
$$->elem = $1;
$$->left = NULL;
$$->right = NULL;
ast = $$;
};
%%
void main() {
yyparse();
printf("%d\n", evaluate(ast));
int evaluate(Node l) {
if( l == NULL )
return 0;
return l->elem;
}
Drugi zadatak
Kod:
counter=0; result=2;
print result;
end;
counter++;
end
Program množi i oduzima isti broj, koji se postepeno povećava, od ukupnog rezultata. Dok god je
rezultat manji od 100 on se ispisuje za svako izvođenje petlje.