-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.php
More file actions
347 lines (301 loc) · 8.66 KB
/
Copy pathparse.php
File metadata and controls
347 lines (301 loc) · 8.66 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
//1. project for IPP 2022/2023
//author: Timotej Halenar
//login: xhalen00
ini_set('display_errors', 'stderr');
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;
$order = 0;
$header = false;
$program = $doc->appendChild($doc->createElement('program'));
$program->setAttribute("language", "IPPcode23");
//look for --help option
if (count($argv)>1){
if (count($argv)!=2){
exit(10);
}
if ($argv[1] == "--help"){
echo("***********************\n");
echo("*******parse.php*******\n");
echo("***********************\n");
echo("description: reads IPPcode23 source from STDIN, performs lexical and syntactic checks and outputs XML representation to STDOUT.\n");
echo("usage: php parse.php <inputfile.ipp23\n");
echo("***********************\n");
exit(0);
}
else{
exit(10);
}
}
//parses line to XML and adds it to the main document
function line_to_xml($line, $program, $doc, $order){
$newline = $program->appendChild($doc->createElement('instruction'));
$newline->setAttribute("order", $order);
$newline->setAttribute("opcode", strtoupper($line[0]));
if (count($line)==1){
return;
}
if(count($line)>=2){
$type_value = determine_arg_type($line[1]);
$arg1 = $newline->appendChild($doc->createElement('arg1', htmlspecialchars(rtrim($type_value[1]))));
$arg1->setAttribute('type', $type_value[0]);
}
if(count($line)>=3){
$type_value = determine_arg_type($line[2]);
$arg1 = $newline->appendChild($doc->createElement('arg2', htmlspecialchars(rtrim($type_value[1]))));
$arg1->setAttribute('type', $type_value[0]);
}
if(count($line)==4){
$type_value = determine_arg_type($line[3]);
$arg1 = $newline->appendChild($doc->createElement('arg3', htmlspecialchars(rtrim($type_value[1]))));
$arg1->setAttribute('type', $type_value[0]);
}
}
//compares argument with expected type using determine_arg_type() function
//return: 0 on success, 1 on failure
function check_arg($arg, $type){
$type_value = determine_arg_type($arg);
if ($type_value[0]==$type){
return 0;
}
else{
return 1;
}
}
//checks, whether argument is in <symb> category
function check_symb($arg){
if (check_arg($arg, "var") == 1){
if (check_arg($arg, "bool") == 1){
if (check_arg($arg, "string") == 1){
if (check_arg($arg, "int") == 1){
if (check_arg($arg, "nil") == 1){
exit(23);
}
}
}
}
}
}
//checks, whether argument is <var>
function check_var($arg){
if (check_arg($arg, "var") == 1){
exit(23);
}
}
//checks, whether argument is <label>
function check_label($arg){
if (check_arg($arg, "label") == 1){
exit(23);
}
}
//checks, whether argument is <type>
function check_type($arg){
if ($arg != "string"){
if ($arg != "int"){
if ($arg != "bool"){
exit(23);
}
}
}
}
//determines argument type and parses argument
//return: $type_value[] array containing argument type and parsed argument itself
function determine_arg_type($arg){
$type_value = [];
if (preg_match("/^GF@|LF@|TF@/", $arg)){
$type_value[0] = "var";
$type_value[1] = $arg;
if (!preg_match("/(LF@|GF@|TF@)[a-zA-Z_\-$&%*!?]+.*/", $arg)){
exit(23);
}
return $type_value;
}
elseif (preg_match("/^string\z|^int\z|^bool\z/", $arg)){
$type_value[0] = "type";
$type_value[1] = $arg;
return $type_value;
}
elseif (preg_match("/^int@/", $arg)){
$type_value[0] = "int";
preg_match("/(?<=@).*/", $arg, $match);
$type_value[1] = $match[0];
return $type_value;
}
elseif (preg_match("/^string@/", $arg)){
$type_value[0] = "string";
preg_match("/(?<=@).*/", $arg, $match);
$type_value[1] = $match[0];
if (preg_match("/\\\\\d{0,2}(?!\d)/", $type_value[1])){
exit(23);
}
return $type_value;
}
elseif (preg_match("/^bool@/", $arg)){
$type_value[0] = "bool";
preg_match("/(?<=@).*/", $arg, $match);
$bool_array = ["true", "false"];
if (!in_array($match[0], $bool_array)){
exit(23);
}
$type_value[1] = $match[0];
return $type_value;
}
elseif (preg_match("/^nil@/", $arg)){
$type_value[0] = "nil";
if (!preg_match("/(?<=@)nil$/", $arg, $match)){
exit(23);
}
$type_value[1] = $match[0];
return $type_value;
}
else{
$type_value[0] = "label";
$type_value[1] = $arg;
if (!preg_match("/^[a-zA-Z_\-$&%*!?]+.*/", $arg)){
exit(23);
}
return $type_value;
}
}
//main loop for reading input
while ($f = fgets(STDIN)){
//remove comments
$f = preg_replace("/#.*/", '', $f);
//check for empty lines
if (preg_match("/^\s*$/", $f)){
continue;
}
//trim whitespace, explode into array
$f = rtrim($f);
$f = trim(preg_replace('/\s\s+/', ' ', str_replace("\n", " ", $f)));
$array = explode(' ', $f);
//check for header
if ($header == false){
if (strcmp($array[0], ".IPPcode23") == 0){
$header = true;
continue;
}
else{
exit(21);
}
}
//each case corresponds with an instruction
//instructions with equal argument types are grouped together
switch(strtoupper($array[0])){
//no args
case "CREATEFRAME":
case "PUSHFRAME":
case "POPFRAME":
case "RETURN":
case "BREAK":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=1){
exit(23);
}
break;
//<var>
case "DEFVAR":
case "POPS":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=2){
exit(23);
}
check_var($array[1]);
break;
//<var><symb>
case "MOVE":
case "INT2CHAR":
case "STRLEN":
case "TYPE":
case "NOT":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=3){
exit(23);
}
check_var($array[1]);
check_symb($array[2]);
break;
//<label>
case "CALL":
case "LABEL":
case "JUMP":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=2){
exit(23);
}
check_label($array[1]);
break;
//<symb>
case "PUSHS":
case "WRITE":
case "EXIT":
case "DPRINT":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=2){
exit(23);
}
check_symb($array[1]);
break;
//<var><symb1><symb2>
case "ADD":
case "SUB":
case "MUL":
case "IDIV":
case "LT":
case "GT":
case "EQ":
case "AND":
case "OR":
case "STRI2INT":
case "CONCAT":
case "GETCHAR":
case "SETCHAR":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=4){
exit(23);
}
check_var($array[1]);
check_symb($array[2]);
check_symb($array[3]);
break;
//<var><type>
case "READ":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=3){
exit(23);
}
check_var($array[1]);
check_type($array[2]);
break;
//<label><symb1><symb2>
case "JUMPIFEQ":
case "JUMPIFNEQ":
$order++;
line_to_xml($array, $program, $doc, $order);
if (count($array)!=4){
exit(23);
}
check_label($array[1]);
check_symb($array[2]);
check_symb($array[3]);
break;
default:
//comment
if (preg_match("/#/", $array[0])){
break;
}
else{
exit(22);
}
}
}
//print final XML document
echo $doc->saveXML();
?>