-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.y
More file actions
831 lines (675 loc) · 20.3 KB
/
Copy pathparser.y
File metadata and controls
831 lines (675 loc) · 20.3 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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
%token <cadena> identifier
%token quotedstringconstant
%token unsignedinteger
%token quotedcharacterconstant
%token digitsequence
%token unsignednumber
%token <cadena> ordinaltypereservedwords
%token tk_program
%token tk_begin
%token tk_uses
%token tk_unit
%token tk_interface
%token tk_implementation
%token tk_label
%token tk_const
%token tk_type
%token tk_real
%token tk_word
%token tk_true
%token tk_false
%token tk_string
%token tk_packed
%token tk_array
%token tk_record
%token tk_end
%token tk_case
%token tk_otherwise
%token tk_of
%token tk_set
%token tk_var
%token tk_forward
%token tk_external
%token tk_function
%token tk_procedure
%token tk_file
%token tk_goto
%token tk_if
%token tk_then
%token tk_else
%token tk_repeat
%token tk_until
%token tk_while
%token tk_do
%token tk_for
%token tk_to
%token tk_downto
%token tk_with
%token tk_nil
%token tk_read
%token tk_readln
%token tk_write
%token tk_writeln
%token tk_in
%token tk_or
%token tk_div
%token tk_mod
%token tk_and
%token tk_not
%token comparison_op
%token assignment_op
%token range_op
%token tk_assign
%token tk_close
%token tk_reset
%token sign
%token tk_inc
%token tk_dec
%{
#include <stdio.h>
#include <string>
#include <cstring>
#include <unordered_map>
#include <vector>
#pragma warning(disable: 4996 6385 6011 4267 4244 4013 4312 4005 6387 26451)
extern FILE *yyin;
extern int yylex(void);
extern int linea;
int yyerror(const char *s);
using namespace std;
struct identificador
{
string id;
char *ambito;
int linea_definicion;
char *tipo;
vector <int> lineas;
};
unordered_map<string, identificador> tabla_simbolos;
char *current_ambito;
char *current_tipo;
vector<int> current_identifiers;
vector<identificador> pending_identifiers;
void insertar_simbolo(char *id_key, char *tipo, int linea){
string key = id_key + string(current_ambito);
if (tabla_simbolos.find(key) != tabla_simbolos.end())
{
string id_key_string = id_key;
string error = "El identificador " + id_key_string + " ya ha sido declarado en el mismo ambito";
yyerror(error.c_str());
return;
}
struct identificador id;
id.ambito = current_ambito;
id.linea_definicion = linea;
id.tipo = tipo;
id.id = id_key;
tabla_simbolos[key] = id;
}
void resolve_pending_identifiers(){
for (int i = 0; i < pending_identifiers.size(); i++)
{
string key = pending_identifiers[i].id + current_ambito;
identificador id = pending_identifiers[i];
id.ambito = current_ambito;
if (tabla_simbolos.find(key) != tabla_simbolos.end())
{
string id_key_string = pending_identifiers[i].id;
string error = "El identificador " + id_key_string + " ya ha sido declarado en el mismo ambito";
yyerror(error.c_str());
return;
}
tabla_simbolos[key] = id;
}
pending_identifiers.clear();
}
void update_tipo(){
for (int i = 0; i < current_identifiers.size(); i++)
{
pending_identifiers[current_identifiers[i]].tipo = current_tipo;
}
current_identifiers.clear();
}
char * concatenar_ambito(char *nuevo_ambito)
{
char *ambito = (char *)malloc(strlen(current_ambito) + strlen(nuevo_ambito) + 2);
strcpy(ambito, current_ambito);
strcat(ambito, ".");
strcat(ambito, nuevo_ambito);
return ambito;
}
char * return_char_from_string(string str){
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
return cstr;
}
void quitar_ambito_anterior(){
if(strchr(current_ambito, '.') == NULL){
return;
}
char *p = strrchr(current_ambito, '.');
int size = p - current_ambito;
char *ambito = (char *)malloc(size + 1);
strncpy(ambito, current_ambito, size);
ambito[size] = '\0';
current_ambito = ambito;
}
void check_identifier_exists(char *id_to_check){
string key = id_to_check + string(current_ambito);
if (tabla_simbolos.find(key) != tabla_simbolos.end())
{
tabla_simbolos[key].lineas.push_back(linea);
return;
}
char *ambito = current_ambito;
while(strchr(ambito, '.') != NULL){
char *p = strrchr(ambito, '.');
int size = p - ambito;
char *ambito_anterior = (char *)malloc(size + 1);
strncpy(ambito_anterior, ambito, size);
ambito_anterior[size] = '\0';
string key = id_to_check + string(ambito_anterior);
if (tabla_simbolos.find(key) != tabla_simbolos.end())
{
tabla_simbolos[key].lineas.push_back(linea);
return;
}
ambito = ambito_anterior;
}
key = id_to_check + string(ambito);
if (tabla_simbolos.find(key) == tabla_simbolos.end())
{
string id_key_string = id_to_check;
string error = "El identificador " + id_key_string + " no ha sido declarado en el ambito actual";
yyerror(error.c_str());
return;
}
}
%}
%union YYSTYPE {
char *cadena;
int numero;
}
%output = "parser.tab.cpp"
%left '=' comparison_op '<' '>' tk_in
%left '+' '-' tk_or
%left '*' '/' tk_div tk_mod tk_and
%left '@' tk_not
%%
s: program | regularunit;
program: programheading ';' optionalprogramuseclause block
;
label: digitsequence
;
procedureidentifier: tk_read
| tk_readln
| tk_write
| tk_writeln
| tk_assign
| tk_close
| tk_reset
| tk_inc
| tk_dec
;
typeidentifier: type
;
constantdeclaration: identifier '=' constant ';' {insertar_simbolo($1, current_tipo, linea);}
| identifier '=' identifier ';' {insertar_simbolo($1, current_tipo, linea);}
;
constant: '+' identifier {current_tipo =return_char_from_string("LONGINT")}
| '-' identifier {current_tipo = return_char_from_string("LONGINT")}
| sign unsignednumber {current_tipo = return_char_from_string("LONGINT")}
| sign unsignedinteger {current_tipo = return_char_from_string("LONGINT")}
| sign digitsequence {current_tipo =return_char_from_string("INTEGER")}
| quotedstringconstant {current_tipo = return_char_from_string("WORD")}
| quotedcharacterconstant {current_tipo = return_char_from_string("CHAR")}
| digitsequence {current_tipo = return_char_from_string("INTEGER")}
;
block: optionallabeldeclarationpart optionalconstantdeclarationpart optionaltypedeclarationpart optionalvariabledeclarationpart optionalprocedureandfunctiondeclarationpart optionalstatementpart
;
labeldeclarationpart: tk_label labels ';'
;
labels: label
| label ',' labels
;
optionallabeldeclarationpart: labeldeclarationpart
|
;
constantdeclarationpart: tk_const constantdeclarations
;
constantdeclarations: constantdeclaration
| constantdeclaration constantdeclarations
;
optionalconstantdeclarationpart: constantdeclarationpart
|
;
typedeclarationpart: tk_type typedeclarations
;
typedeclarations: typedeclaration
| typedeclaration typedeclarations
;
optionaltypedeclarationpart: typedeclarationpart
|
;
variabledeclarationpart: tk_var variabledeclarations
;
;
variabledeclarations: variabledeclaration
| variabledeclaration variabledeclarations
;
variabledeclaration: identifierlist ':' type ';' {resolve_pending_identifiers();}
| identifierlist ':' identifier ';' {resolve_pending_identifiers(); check_identifier_exists($3);}
;
identifierlist:
identifier {
identificador temp;
temp.id = $1;
temp.linea_definicion = linea;
pending_identifiers.push_back(temp);
current_identifiers.push_back(pending_identifiers.size() - 1);
}
| identifier ',' identifierlist {
identificador temp;
temp.id = $1;
temp.linea_definicion = linea;
pending_identifiers.push_back(temp);
current_identifiers.push_back(pending_identifiers.size() - 1);
}
;
optionalvariabledeclarationpart: variabledeclarationpart
|
;
procedureandfunctiondeclarationpart: procedureandfunction
| procedureandfunction procedureandfunctiondeclarationpart
;
procedureandfunction: proceduredeclaration
| functiondeclaration
;
optionalprocedureandfunctiondeclarationpart: procedureandfunctiondeclarationpart
|
;
statementpart: compoundstatement
;
optionalstatementpart: statementpart
|
;
typedeclaration: identifier '=' type ';' {insertar_simbolo($1, return_char_from_string("TYPE"), linea);}
| identifier '=' identifier ';' {insertar_simbolo($1, return_char_from_string("TYPE"), linea);}
;
;
type: simpletype
| structuredtype
| pointertype
;
simpletype: ordinaltype
| realtype
| stringtype
;
realtype: realtypeidentifier
;
realtypeidentifier: tk_real {current_tipo = return_char_from_string("REAL");}
;
ordinaltypeidentifier: ordinaltypereservedwords {current_tipo = $1;update_tipo();}
;
ordinaltype: subrangetype
| enumeratedtype
| ordinaltypeidentifier
;
stringtype: tk_string '[' sizeattribute ']'
;
sizeattribute: unsignedinteger
;
enumeratedtype: '(' identifierlist ')'
;
subrangetype: constant range_op constant {current_tipo = return_char_from_string("SUBRANGE");}
| constant range_op identifier {current_tipo = return_char_from_string("SUBRANGE"); check_identifier_exists($3);}
| identifier range_op constant {current_tipo = return_char_from_string("SUBRANGE"); check_identifier_exists($1);}
| identifier range_op identifier {current_tipo = return_char_from_string("SUBRANGE"); check_identifier_exists($1); check_identifier_exists($3);}
;
;
structuredtype: arraytype {current_tipo = return_char_from_string("ARRAY");}
| settype {current_tipo = return_char_from_string("SET");}
| filetype {current_tipo = return_char_from_string("FILE");}
| recordtype {current_tipo = return_char_from_string("RECORD");}
| tk_packed arraytype
| tk_packed settype
| tk_packed filetype
| tk_packed recordtype
;
arraytype: tk_array '[' indextypes ']' tk_of type {current_tipo = return_char_from_string("ARRAY"); update_tipo();}
| tk_array '[' indextypes ']' tk_of identifier {current_tipo = $6; update_tipo(); check_identifier_exists($6);}
| tk_array '[' ranges ']' tk_of type {current_tipo = return_char_from_string("ARRAY"); update_tipo();}
| tk_array '[' ranges ']' tk_of identifier {current_tipo = $6; update_tipo(); check_identifier_exists($6);}
;
ranges: subrangetype
| subrangetype ',' ranges
;
indextypes: indextype
| indextype ',' indextypes
;
indextype: ordinaltype
;
recordtype: tk_record tk_end
| tk_record fieldlist tk_end
;
fieldlist: fixedpart
| fixedpart ';'
| fixedpart ';' variantpart
| fixedpart ';' variantpart ';'
| variantpart
| variantpart ';'
;
fixedpart: fielddeclarations
;
fielddeclarations: fielddeclaration
| fielddeclarations ';' fielddeclaration
;
fielddeclaration: identifierlist ':' type
| identifierlist ':' identifier {check_identifier_exists($3);}
;
variantpart: tk_case tagfieldtype tk_of variants
| tk_case identifier ':' tagfieldtype tk_of variants
;
variants: variant
| variant ';' variants
;
variant: constants ':' '(' ')'
| constants ':' '(' fieldlist ')'
;
constants: constant
| constant ',' constants
| identifier
| identifier ',' constants
;
tagfieldtype: ordinaltypeidentifier
| identifier
;
settype: tk_set tk_of ordinaltype
;
filetype: tk_file
| tk_file tk_of type
| tk_file tk_of identifier {check_identifier_exists($3);}
;
pointertype: '^' basetype {current_tipo = return_char_from_string("POINTER");}
;
basetype: typeidentifier
;
variablereference: identifier qualifiers {check_identifier_exists($1);}
;
qualifiers : qualifier
| qualifier qualifiers
;
qualifier: index
| fielddesignator
| '^'
;
index : '[' expressions']'
;
expressions: expression
| expression ',' expressions
;
fielddesignator: '.' identifier
;
factor: '@' variablereference
| '@' identifier {check_identifier_exists($2);}
| unsignedconstant
| functioncall
| setconstructor
| '(' expression ')'
| tk_not factor
;
unsignedconstant: unsignednumber
| tk_nil
| digitsequence
;
term: factor
| factor '*' term
| factor '/' term
| factor tk_div term
| factor tk_mod term
| factor tk_and term
| identifier '*' term {check_identifier_exists($1);}
| identifier '/' term {check_identifier_exists($1);}
| identifier tk_div term {check_identifier_exists($1);}
| identifier tk_mod term {check_identifier_exists($1);}
| identifier tk_and term {check_identifier_exists($1);}
| quotedstringconstant '*' term
| quotedstringconstant '/' term
| quotedstringconstant tk_div term
| quotedstringconstant tk_mod term
| quotedstringconstant tk_and term
| variablereference '*' term
| variablereference '/' term
| variablereference tk_div term
| variablereference tk_mod term
| variablereference tk_and term
| identifier {check_identifier_exists($1);}
| quotedstringconstant
| quotedcharacterconstant
;
simpleexpression: unsignedexpression
|sign unsignedexpression
;
unsignedexpression: term sign unsignedexpression
| term tk_or unsignedexpression
| term
| identifier sign unsignedexpression {check_identifier_exists($1);}
| identifier tk_or unsignedexpression {check_identifier_exists($1);}
;
expression: simpleexpression comparison_op simpleexpression
| simpleexpression '=' simpleexpression
| simpleexpression tk_in simpleexpression
| simpleexpression '>' simpleexpression
| simpleexpression '<' simpleexpression
| simpleexpression
;
functioncall: identifier actualparameterlist {check_identifier_exists($1);}
actualparameterlist: '(' actualparametergroup ')'
;
actualparametergroup: actualparameter
| actualparametergroup ',' actualparameter
|
;
actualparameter: expression
| variablereference
| procedureidentifier
;
setconstructor: '[' membergroups ']'
| '[' ']'
;
membergroups: membergroup
| membergroup ',' membergroups
;
membergroup: expression
| expression range_op expression
;
statement: label range_op simplestatement
| label range_op structuredstatement
| label range_op
| simplestatement
| structuredstatement
|
;
simplestatement: assignmentstatement
| procedurestatement
| gotostatement
;
assignmentstatement: variablereference assignment_op expression
| identifier assignment_op expression {check_identifier_exists($1);}
| identifier assignment_op tk_false {check_identifier_exists($1);}
| identifier assignment_op tk_true {check_identifier_exists($1);}
;
procedurestatement: procedureidentifier
| procedureidentifier actualparameterlist
| identifier actualparameterlist {check_identifier_exists($1);}
;
gotostatement: tk_goto label
;
structuredstatement: compoundstatement
| conditionalstatement
| repetitivestatement
| withstatement
;
compoundstatement: tk_begin statements tk_end
| tk_begin statements tk_end '.'
;
statements : statement
| statements ';' statement
;
conditionalstatement: ifstatement
| casestatement
;
ifstatement: tk_if expression tk_then statement
| tk_if expression tk_then statement tk_else statement
;
casestatement: tk_case expression tk_of cases tk_end
| tk_case expression tk_of cases otherwiseclause tk_end
| tk_case expression tk_of cases ';' tk_end
| tk_case expression tk_of cases otherwiseclause ';' tk_end
;
cases: case ',' cases
| case
;
case: constants ':' statement;
otherwiseclause: ';' tk_otherwise statement
;
repetitivestatement: repeatstatement
| whilestatement
| forstatement
;
repeatstatement: tk_repeat statements tk_until expression
;
whilestatement: tk_while expression tk_do statement
;
forstatement: tk_for identifier assignment_op initialvalue tk_to finalvalue tk_do statements {check_identifier_exists($2);}
| tk_for identifier assignment_op initialvalue tk_downto finalvalue tk_do statements {check_identifier_exists($2);}
| tk_for identifier assignment_op initialvalue tk_to identifier tk_do statements {check_identifier_exists($2); check_identifier_exists($6);}
| tk_for identifier assignment_op initialvalue tk_downto identifier tk_do statements {check_identifier_exists($2); check_identifier_exists($6);}
;
initialvalue: expression
;
finalvalue:expression
;
withstatement: tk_with recordvariablereferences tk_do statement
;
recordvariablereferences: recordvariablereference
| recordvariablereference ',' recordvariablereferences
;
recordvariablereference: variablereference
| identifier
;
proceduredeclaration: procedureheading ';' procedurebody ';'
| procedureheading ';' procedurebody
;
procedurebody: block
| tk_forward
| tk_external
;
procedureheading: tk_procedure identifier optionalformalparameterlist {quitar_ambito_anterior(); insertar_simbolo($2,return_char_from_string("PROCEDURE"), linea); current_ambito = concatenar_ambito($2); resolve_pending_identifiers();}
;
optionalformalparameterlist: formalparameterlist
| '('')'
|
;
functiondeclaration: functionheading ';' functionbody ';'
;
functionbody: block
| tk_forward
| tk_external
;
functionheading: tk_function identifier optionalformalparameterlist ':' resulttype {quitar_ambito_anterior(); insertar_simbolo($2, return_char_from_string("FUNCTION"), linea); current_ambito = concatenar_ambito($2);}
;
resulttype: ordinaltypeidentifier
| realtypeidentifier
| identifier {check_identifier_exists($1);}
;
formalparameter: parameterdeclaration
| procedureheading
| functionheading
;
parameterdeclaration: tk_var identifierlist ':' typeidentifier
| identifierlist ':' typeidentifier
;
formalparameters: formalparameter ';' formalparameters
| formalparameter
;
formalparameterlist: '(' formalparameters ')'
;
optionalprogramuseclause: usesclause ';'
|
;
programheading: tk_program identifier optionalprogramheadingparameters { current_ambito=$2; insertar_simbolo($2, return_char_from_string("PROGRAM"), linea);}
;
optionalprogramheadingparameters: '(' programparameters ')'
|
;
programparameters: identifierlist
;
usesclause: tk_uses identifierlist
;
regularunit: unitheading ';' interfacepart implementationpart tk_end '.'
;
unitheading: tk_unit identifier
;
interfacepart: tk_interface optionalunituseclause optionalunitconstantdeclarationpart optionalunittypedeclarationpart optionalunitvariabledeclarationpart optionalunitprocedureandfunctiondeclarationpart
;
implementationpart: tk_implementation optionalunitconstantdeclarationpart optionalunittypedeclarationpart optionalunitvariabledeclarationpart optionalunitprocedureandfunctiondeclarationpart
;
optionalunituseclause: usesclause
|
;
optionalunitconstantdeclarationpart: constantdeclarationpart
|
;
optionalunittypedeclarationpart: typedeclarationpart
|
;
optionalunitvariabledeclarationpart: variabledeclarationpart
|
;
optionalunitprocedureandfunctiondeclarationpart: procedureandfunctiondeclarationpart
|
;
%%
int yyerror(const char *s)
{
char mensaje[100];
if ( !strcmp( s, "parse error" ) )
strcpy( mensaje, "Error de sintaxis" );
else
strcpy( mensaje, s );
printf("Error en linea %d: %s\n", linea, mensaje);
return 1;
}
int main(int argc, char* argv[]) {
if ( argc > 1 ){
printf("Archivo: %s\n", argv[1]);
yyin = fopen( argv[1], "r" );
}
else{
printf("Archivo: stdin\n");
yyin = stdin;
}
yyparse();
printf("Tabla de simbolos\n");
printf("------------------------------------------------------------------------------------------------------------------------\n");
printf("%21s ", " Identificador |");
printf("%25s", " Ambito |");
printf("%7s" , "Linea de definicion |");
printf("%21s" , " Tipo |");
printf("%19s" , " Lineas de uso\n");
printf("------------------------------------------------------------------------------------------------------------------------\n");
for (auto it = tabla_simbolos.begin(); it != tabla_simbolos.end(); ++it)
{
printf("%19s |", it->second.id.c_str());
printf("%24s |", it->second.ambito);
printf("%19d |", it->second.linea_definicion);
printf("%19s |", it->second.tipo);
for (int i = 0; i < it->second.lineas.size(); i++)
{
printf("%d,", it->second.lineas[i]);
}
printf("\n");
}
return 0;
}