forked from vivekzhere/spsil
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspsil.y
129 lines (120 loc) · 2.46 KB
/
spsil.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
%{
#include<stdio.h>
#include<stdlib.h>
#include "spsil.h"
%}
%union
{
struct tree *n;
}
%token ALIAS DEFINE DO ELSE ENDIF ENDWHILE IF IRETURN LOAD STORE THEN WHILE REG NUM ASSIGNOP ARITHOP1 ARITHOP2 RELOP LOGOP NOTOP ID
%type<n> IF IRETURN LOAD STORE WHILE REG NUM ASSIGNOP ARITHOP1 ARITHOP2 RELOP LOGOP NOTOP ID stmtlist stmt expr ids ifpad whilepad
%left LOGOP
%left RELOP
%left ARITHOP1 // + and -
%left ARITHOP2 // * , / and %
%right NOTOP // NOT Operator
%%
body: definelistpad stmtlist {
}
;
definelistpad: definelist {
add_predefined_constants();
}
;
definelist: {
}
|definelist definestmt {
}
;
definestmt: DEFINE ID NUM ';' {
insert_constant($2->name,$3->value);
}
;
stmtlist: stmtlist stmt {
$$=create_nonterm_node("Body",$1,$2);
}
|stmt {
$$=$1;
}
;
stmt: ids ASSIGNOP expr ';' {
$$=create_tree($2,$1,$3,NULL);
}
|ifpad expr THEN stmtlist ENDIF ';' {
$$=create_tree($1,$2,$4,NULL);
flag_alias--;
}
|ifpad expr THEN stmtlist
ELSE stmtlist ENDIF ';' {
$$=create_tree($1,$2,$4,$6);
flag_alias--;
}
|whilepad expr DO stmtlist ENDWHILE ';' {
$$=create_tree($1,$2,$4,NULL);
flag_alias--;
}
|ALIAS ID REG ';' {
insert_alias($2->name,$3->value);
$$=NULL;
}
|LOAD '(' expr ',' expr ')' {
$$=create_tree($1,$3,$5,NULL);
}
|STORE '(' expr ',' expr ')' {
$$=create_tree($1,$3,$5,NULL);
}
;
expr: expr ARITHOP1 expr {
$$=create_tree($2,$1,$3,NULL);
}
|expr ARITHOP2 expr {
$$=create_tree($2,$1,$3,NULL);
}
|expr RELOP expr {
$$=create_tree($2,$1,$3,NULL);
}
|expr LOGOP expr {
$$=create_tree($2,$1,$3,NULL);
}
|NOTOP expr {
$$=create_tree($1,$2,NULL,NULL);
}
|'('expr')' {
$$=$2;
}
|NUM {
$$=$1;
}
|ids {
$$=$1;
}
;
ifpad: IF {
flag_alias++;
$$=$1;
}
;
whilepad: WHILE {
flag_alias++;
$$=$1;
}
;
ids: ID {
$$=substitute_id($1);
}
|REG {
$$=$1;
}
;
%%
int main (void)
{
//fp=fopen("sim.asm","w");
//fprintf(fp,"START\n");
return yyparse();
}
int yyerror (char *msg)
{
return fprintf (stderr, "%d: %s\n",linecount,msg);
}