-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.y
More file actions
199 lines (193 loc) · 4.84 KB
/
Copy pathsyntax.y
File metadata and controls
199 lines (193 loc) · 4.84 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
%{
#include <stdio.h>
#include "IR_structure.h"
#include "lex.yy.c"
int yyerror(char* msg);
%}
/* declared types */
%union {
int type_int;
char* relop_name;
Operand type_operand;
InterCode type_InterCode;
InterCodes type_InterCodes;
}
%locations
/* declared tokens */
%token <type_int> INT
%token <relop_name> RELOP ID
%token COLON FUNCTION LABEL ASSIGNOP EOL
%token PLUS MINUS STAR DIV AND SHARP
%token IF GOTO RETURN DEC ARG CALL PARAM READ WRITE
%type <type_operand> var label function
%type <type_InterCode> Code
%type <type_InterCodes> Program Codes
%%
Program : Codes{
$$=inter_code_head;
};
Codes : Codes Code EOL{
InterCodes c=(InterCodes)malloc(sizeof(struct InterCodes_));
c->code=$2;
inter_code_count++;c->num=inter_code_count;
c->prev=$1;c->next=NULL;
if($1==NULL)inter_code_head=c;
else $1->next=c;
$$=c;
}
| /*empty*/{
inter_code_head=NULL;
$$=NULL;
};
Code : LABEL label COLON{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=LABEL_IR;
c->u.one.op=$2;
$$=c;
}
| FUNCTION function COLON{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=FUNCTION_IR;
c->u.one.op=$2;
$$=c;
}
| var ASSIGNOP var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=ASSIGN_IR;
c->u.two.op1=$1;
c->u.two.op2=$3;
$$=c;
}
| var ASSIGNOP var PLUS var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=ADD_IR;
c->u.three.res=$1;
c->u.three.op1=$3;
c->u.three.op2=$5;
$$=c;
}
| var ASSIGNOP var MINUS var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=SUB_IR;
c->u.three.res=$1;
c->u.three.op1=$3;
c->u.three.op2=$5;
$$=c;
}
| var ASSIGNOP var STAR var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=MUL_IR;
c->u.three.res=$1;
c->u.three.op1=$3;
c->u.three.op2=$5;
$$=c;
}
| var ASSIGNOP var DIV var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=DIV_IR;
c->u.three.res=$1;
c->u.three.op1=$3;
c->u.three.op2=$5;
$$=c;
}
| GOTO label{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=GOTO_IR;
c->u.one.op=$2;
$$=c;
}
| IF var RELOP var GOTO label{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=IFGOTO_IR;
c->u.four.op1=$2;
c->u.four.op2=$4;
c->u.four.op3=$6;
strcpy(c->u.four.relop,$3);
$$=c;
}
| RETURN var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=RETURN_IR;
c->u.one.op=$2;
$$=c;
}
|DEC var INT{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=DEC_IR;
c->u.dec.x=$2;
c->u.dec.size=$3;
$$=c;
}
| ARG var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=ARG_IR;
c->u.one.op=$2;
$$=c;
}
| var ASSIGNOP CALL function{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=CALL_IR;
c->u.two.op1=$1;
c->u.two.op2=$4;
$$=c;
}
| PARAM var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=PARAM_IR;
c->u.one.op=$2;
$$=c;
}
| READ var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=READ_IR;
c->u.one.op=$2;
$$=c;
}
| WRITE var{
InterCode c=(InterCode)malloc(sizeof(struct InterCode_));
c->kind=WRITE_IR;
c->u.one.op=$2;
$$=c;
};
var : STAR var{
Operand op=$2;
op->addr=STAR_OP;
$$=op;
}
| AND var{
Operand op=$2;
op->addr=AND_OP;
$$=op;
}
| ID{
Operand op=(Operand)malloc(sizeof(struct Operand_));
op->kind=VARIABLE_OP;
op->addr=NORMAL_OP;
op->op_u.var_no=get_var_num($1);
$$=op;
}
| SHARP INT{
Operand op=(Operand)malloc(sizeof(struct Operand_));
op->kind=CONSTANT_OP;
op->addr=NORMAL_OP;
op->op_u.value=$2;
$$=op;
};
label : ID{
Operand op=(Operand)malloc(sizeof(struct Operand_));
op->kind=LABEL_OP;
op->addr=NORMAL_OP;
op->op_u.var_no=get_label_num($1);
$$=op;
};
function : ID{
Operand op=(Operand)malloc(sizeof(struct Operand_));
op->kind=FUNCTION_OP;
op->addr=NORMAL_OP;
strcpy(op->op_u.name,$1);
$$=op;
};
%%
int yyerror(char*msg){
printf("IR syntax error: %s\n", msg);
}