-
Notifications
You must be signed in to change notification settings - Fork 0
/
task1.y
66 lines (54 loc) · 1.57 KB
/
task1.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "task1.h"
FILE *fp;
FILE *fp_read;
#include "task2.c"
extern FILE* yyin;
int yylex(void);
%}
%union
{
struct tnode* node;
};
%token START END NUM ASSIGN READ WRITE ID PLUS MINUS MUL DIV
%type<node> program SLIST stmt asgStmt inputStmt outputStmt expr START END NUM ASSIGN READ WRITE ID PLUS MINUS MUL DIV
%left PLUS MINUS
%left MUL DIV
%%
program : START SLIST END {GenerateCode($2); Interpret($2);}
| START END {}
;
SLIST : stmt {$$ = $1;}
| SLIST stmt {$$ = createTree(0,connector_node,NULL,$1,$2);}
;
stmt : asgStmt {$$ = $1;}
| inputStmt {$$ = $1;}
| outputStmt {$$ = $1;}
;
asgStmt : ASSIGN expr ';' {$$ = createTree(0,assign_node,NULL,$1,$2);};
inputStmt : READ '(' ID ')' ';' {$$ = createTree(0,read_node,NULL,$3,NULL);};
outputStmt : WRITE '(' expr ')' ';' {$$ = createTree(0,write_node,NULL,$3,NULL);};
expr : expr PLUS expr {$$ = createTree(0,plus_node,NULL,$1,$3);}
| expr MINUS expr {$$ = createTree(0,minus_node,NULL,$1,$3);}
| expr MUL expr {$$ = createTree(0,mul_node,NULL,$1,$3);}
| expr DIV expr {$$ = createTree(0,div_node,NULL,$1,$3);}
| '(' expr ')' {$$ = $2;}
| ID {$$ = $1;}
| NUM {$$ = $1;}
;
%%
void yyerror(char *S)
{
printf("\n%s",S);
}
int main()
{
fp = fopen("/home/shrey/xsm_expl/xsm_progs/task2.xsm","w");
fp_read = fopen("input.txt","r");
yyin = fp_read;
yyparse();
return 0;
}