-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParserClass.php
454 lines (385 loc) · 12.8 KB
/
ParserClass.php
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
require_once("InstructionClass.php");
require_once("ArgumentClass.php");
/*
* Autor: Jiri Peska
* Login: xpeska05
*/
/*
* Trida, ktera parsuje vstupni program v ippcode18 a provadi kontrolu formatu operandu a instrukci.
* Ke kontrole vyuziva regularni vyrazy, pole instrukci, ktere obsahuje ocekavane operandy pro kazdou instrukci a funkce pro prevod vysledne
* struktury do XML.
* Cyklus: radek po radku nacita vstupni program, radky parsuje, kontroluje validitu naparsovanych dat a vytvari objekty pro instrukce a jeji parametry.
* Pokud vse probehne v poradku, tak se vysledna struktura preda funkci, ktera vytvori XML.
*/
class Parser
{
private $_source;
private $_programName;
private $_sourceCode = array();
private $_instructionArray = array(); // pole objektu Instrukce
private $_numberOfComments = 0;
private $_numberOfInstructions = 0;
private $_validTypes = array("int", "bool", "string", "label", "type", "var", "GF", "LF", "TF");
private $_basicTypes = array("int", "bool", "string", "float");
private $_validInstructions = array(
"MOVE" => array("var", "symb"),
"CREATEFRAME" => array(),
"PUSHFRAME" => array(),
"POPFRAME" => array(),
"DEFVAR" => array("var"),
"CALL" => array("label"),
"RETURN" => array(),
"PUSHS" => array("symb"),
"POPS" => array("var"),
"ADD" => array("var", "symb", "symb"),
"SUB" => array("var", "symb", "symb"),
"MUL" => array("var", "symb", "symb"),
"IDIV" => array("var", "symb", "symb"),
"LT" => array("var", "symb", "symb"),
"GT" => array("var", "symb", "symb"),
"EQ" => array("var", "symb", "symb"),
"AND" => array("var", "symb", "symb"),
"OR" => array("var", "symb", "symb"),
"NOT" => array("var", "symb"),
"INT2CHAR" => array("var", "symb"),
"STRI2INT" => array("var", "symb", "symb"),
"READ" => array("var", "type"),
"WRITE" => array("symb"),
"CONCAT" => array("var", "symb", "symb"),
"STRLEN" => array("var", "symb"),
"GETCHAR" => array("var", "symb", "symb"),
"SETCHAR" => array("var", "symb", "symb"),
"TYPE" => array("var", "symb"),
"LABEL" => array("label"),
"JUMP" => array("label"),
"JUMPIFEQ" => array("label", "symb", "symb"),
"JUMPIFNEQ" => array("label", "symb", "symb"),
"DPRINT" => array("symb"),
"BREAK" => array()
);
public function __construct()
{
//fwrite(STDERR, "Parser vytvoren\n");
$this->_source = STDIN;
}
/*
* Nahraje do bufferu radek po radku vstupni zdrojovy kod
*/
private function loadSource()
{
while($line = fgets($this->_source))
{
$this->_sourceCode[] = $line;
}
}
// hlavni funkce, ktera parsuje ten program
public function parseSourceCode()
{
// prvni radek
$firstLine = $this->getLineOfCode();
$this->_programName = trim($firstLine, " \x00..\x1F");
$this->_programName = $this->trimLine($this->_programName);
$programName = strtolower($this->_programName);
if($programName != ".ippcode18")
{
fwrite(STDERR, "Hodnota \"" . $programName . "\" neni validni nazev programu. Soubor je mozna prazdny.\n");
exit(21);
}
$this->_programName = "IPPcode18";
//nacteme do pole jednotlive radky kodu
$this->loadSource();
foreach($this->_sourceCode as $line)
{
$this->parseLineOfCode($line);
}
}
public function printStatistics($file, $order)
{
//if (file_exists($file))
//{
try {
$handle = fopen($file, "w");
if ( ! $handle)
{
throw new Exception("Otevreni souboru pro zapis se nezdarilo.");
}
}
catch(Exception $e)
{
fwrite(STDERR, "Nepovedlo se otevrit soubor pro zapsani statistik.\n");
exit(12);
}
//fwrite($handle, "parse.php Statistiky:\n");
if(! empty($order))
{
ksort($order);
foreach($order as $key)
{
if($key == "comments")
{
//fwrite($handle, " Pocet komentaru: ".$this->_numberOfComments."\n");
fwrite($handle, $this->_numberOfComments."\n");
}
elseif($key == "loc")
{
//fwrite($handle, " Pocet instrukci: ".$this->_numberOfInstructions."\n");
fwrite($handle, $this->_numberOfInstructions."\n");
}
}
}
fclose($handle);
//}
}
private function getLineOfCode()
{
return fgets($this->_source);
}
private function parseLineOfCode($line)
{
// nacitame znaky z puvodniho retezce a kdyz narazi na komentar, tak se zapocita a cyklus skonci
$newLine = $this->trimLine($line);
if($newLine == NULL)
{
return;
}
// rozdelime radek s triadresnym kodem na casti podle space
$rozdelene = explode(" ", $newLine);
// zbavime se prazdnych znaku u instrukci, operandu atd...
for($i = 0; $i < count($rozdelene); $i++)
{
$rozdelene[$i] = trim($rozdelene[$i], " \t\n\r\0\x0B");
}
// odstraneni prazdnych hodnot
$rozdelene = array_filter($rozdelene, 'strlen');
//reindexace
$rozdelene = array_values($rozdelene);
$rozdelene[0] = strtoupper($rozdelene[0]);
// kontrola existence instrukce
if(array_key_exists($rozdelene[0], $this->_validInstructions) == false)
{
fwrite(STDERR, "Instrukce " . $rozdelene[0] . " neexistuje.\n");
exit(21);
}
//kontrola, zda-li za instrukci nasleduje spravny pocet parametru
if((count($rozdelene)-1) != count($this->_validInstructions[$rozdelene[0]]))
{
fwrite(STDERR, "Instrukci \"" . $rozdelene[0] . "\" nebyl predan spravny pocet operandu (".(count($rozdelene)-1).", ocekavany ".count($this->_validInstructions[$rozdelene[0]]).").\n");
exit(21);
}
// instrukce
$this->_numberOfInstructions++;
$novaInstrukce = new Instruction();
$novaInstrukce->setOpcode($rozdelene[0]);
//zpracujeme parametry isntrukce
for($idxParam = 1; $idxParam < count($rozdelene); $idxParam++)
{
// zpracovani argumentu
$newParam = new Argument;
$parsedParam = explode("@", $rozdelene[$idxParam], 2);
// kontrola datoveho typu a hodnoty
if(count($parsedParam) == 1)
{
# TODO SOLVED !!! Nebude tady problem, pokud bude label jmenem int, string, bool? mozna bych se mel podivat,
# jestli na danem miste je ocekavan type nebo label a podle toho se rozhodnout.
if(in_array($parsedParam[0], array("int", "string", "bool")) && $novaInstrukce->getOpcode() != "LABEL")
{
#$newParam->setArgType($parsedParam[0]);
$newParam->setArgType("type");
$newParam->setArgValue($parsedParam[0]);
}
else
{
$newParam->setArgType("label");
// kontrola hodnoty labelu
if( ! $this->checkFormat($parsedParam[0]))
{
fwrite(STDERR, "Label \"" . $parsedParam[0] . "\" nevyhovuje regularnimu vyrazu.\n");
exit(21);
}
$newParam->setArgValue($parsedParam[0]);
}
}
else if(in_array($parsedParam[0], array("GF", "LF", "TF")))
{
$newParam->setArgType("var");
// kontrola hodnoty(nazvu) promenne
if( ! $this->checkFormat($parsedParam[1]))
{
fwrite(STDERR, "Promenna \"" . $parsedParam[1] . "\" nevyhovuje regularnimu vyrazu.\n");
exit(21);
}
$newParam->setArgValue($parsedParam[0]."@".$parsedParam[1]);
}
else
{
// kontrola existence datoveho typu
if(in_array($parsedParam[0], $this->_validTypes))
{
$newParam->setArgType($parsedParam[0]);
}
// kontrola hodnoty bool
if($parsedParam[0] == "bool")
{
//if( ! in_array(strtolower($parsedParam[1]), array("true", "false")))
if( ! in_array($parsedParam[1], array("true", "false")))
{
fwrite(STDERR, "Hodnota typu bool \"" . $parsedParam[1] . "\" neobsahuje povolenou hodnotu(true, false).\n");
exit(21);
}
}
// kontrola hodnoty string
if($parsedParam[0] == "string")
{
if( ! preg_match("/^(\\\[0-9]{3}|[^\s #\\\])*$/", $parsedParam[1]))
{
fwrite(STDERR, "Hodnota typu string \"" . $parsedParam[1] . "\" neobsahuje povolenou hodnotu(true, false).\n");
exit(21);
}
}
// kontrola hodnoty int
if($parsedParam[0] == "int")
{
if( ! preg_match("/^[+-]?[0-9]+$/", $parsedParam[1]))
{
fwrite(STDERR, "Hodnota typu int \"" . $parsedParam[1] . "\" neobsahuje povolenou hodnot.\n");
exit(21);
}
}
$newParam->setArgValue($parsedParam[1]);
}
// zjistime typ n-teho parametru podle instrukce (symb, label, var, type) a porovname s typem aktualniho operandu
$typAtributu = $this->_validInstructions[$novaInstrukce->getOpcode()];
if( ! empty($typAtributu))
{
$typAtributu = $typAtributu[$idxParam-1];
if( ! $this->checkOperandType($typAtributu, $newParam->getArgType()))
{
fwrite(STDERR, "Typ aktualniho operandu nesouhlasi s ocekavanym typem operandu na dane pozici.\n");
exit(21);
}
}
// vlozime do pole k aktualni instrukci
$novaInstrukce->insertNewArgument($newParam);
}
// vlozime kompletni objekt instrukce do pole
$this->_instructionArray[] = $novaInstrukce;
}
/*
* Funkce odstrani z retezce $line komentare zacinajici # a escape sekvence
* Vraci novy retezec bez komentaru a bez escape sekvenci a white spaces
*/
private function trimLine($line)
{
$newLine = "";
for($i = 0; $i < strlen($line); $i++)
{
if($line[$i] == "#")
{
$this->_numberOfComments++;
break;
}
$newLine .= $line[$i];
}
// pokud je retezec prazdny, znamena to, ze se jednalo o celoradkovy komentar
if(empty($newLine))
{
return NULL;
}
// odstranime z retezce escape sekvence a bile znaky
$newLine = trim($newLine, " \x00..\x1F");
return $newLine;
}
/*
* Zkontroluje, zda-li $var vyhovuje predepsanemu formatu
*/
public function checkFormat($var)
{
//return preg_match("/^[a-zA-Z_\-$&%*][a-zA-Z0-9_\-$&%*]*$/", $var);
return preg_match("/^[a-zA-Z_\\-$&%*][a-zA-Z0-9_\\-$&%*]*$/", $var);
}
/*
* Zjisti, jestli je na danem miste parametru dovoleny operand
*/
public function checkOperandType($operand, $actualOperand)
{
switch($operand)
{
case "var":
if($actualOperand == "var")
{
return true;
}
return false;
break;
case "symb":
if(in_array($actualOperand, $this->_basicTypes) || $actualOperand == "var")
{
return true;
}
return false;
break;
case "label":
if($actualOperand == "label")
{
return true;
}
return false;
break;
case "type":
if(in_array($actualOperand, $this->_basicTypes) || $actualOperand == "type")
{
return true;
}
return false;
break;
}
}
/*
* Tiskne instrukce a jejich obsah (hodnoty argumentuuu...)
*/
public function printAll()
{
echo "\n\n";
echo "_____________________________________________________________\n";
echo "Instrukce v poli: ". count($this->_instructionArray) . "\n\n";
foreach($this->_instructionArray as $instruction)
{
echo "Instrukce: " . $instruction->getOpcode() . "\n";
echo "Order: ".$instruction->getOrder() . "\n";
foreach($instruction->getArgArray() as $arg)
{
echo $arg->getArgType() . " -> " . $arg->getArgValue() . "\n";
}
echo "\n";
}
echo "_____________________________________________________________\n";
}
public function toXML()
{
$writer = new XMLWriter();
$writer->openURI("php://stdout");
$writer->startDocument('1.0', 'UTF-8');
$writer->setIndent(true);
$writer->setIndentString(" ");
$writer->startElement('program');
$writer->writeAttribute('language', $this->_programName);
foreach($this->_instructionArray as $instruction)
{
$writer->startElement('instruction');
$writer->writeAttribute('order', $instruction->getOrder());
$writer->writeAttribute('opcode', $instruction->getOpcode());
$argCnt = 1;
foreach($instruction->getArgArray() as $arg)
{
$writer->startElement('arg'.$argCnt);
$writer->writeAttribute('type', $arg->getArgType());
$writer->text($arg->getArgValue());
$writer->endElement();
$argCnt++;
}
$writer->endElement();
}
$writer->endElement();
}
}