-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
980 lines (934 loc) · 30.4 KB
/
parser.c
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
//
// parser.c
// Strawberry
//
// Criado por Alvaro Costa Neto em 12/10/2013.
// Copyright (c) 2013 Alvaro Costa Neto. Todos os direitos reservados.
//
#include <stdbool.h>
#include <string.h>
#include "backend.h"
#include "errors.h"
#include "scanner.h"
#include "symbol_table.h"
#include "parser.h"
bool should_log;
// Funções de geração de código
void write_index_offset(item_t *item, item_t *index_item);
void write_field_offset(item_t *item, address_t offset);
void write_unary_op(symbol_t symbol, item_t *item);
void write_binary_op(symbol_t symbol, item_t *item, item_t *rhs_item);
void write_comparison(symbol_t symbol, item_t *item, item_t *rhs_item);
void write_branch(item_t *item, bool forward);
void write_inverse_branch(item_t *item, bool forward);
void write_label(item_t *item, const char *label);
void write_fixup(item_t *item);
void write_store(item_t *dst_item, item_t *src_item);
bool is_first(const char *non_terminal, symbol_t symbol)
{
// if (strcmp(non_terminal, "selector") == 0)
// return symbol == symbol_period ||
// symbol == symbol_open_bracket ||
// symbol == symbol_null;
// else if (strcmp(non_terminal, "factor") == 0)
// return symbol == symbol_open_paren ||
// symbol == symbol_not ||
// symbol == symbol_number ||
// symbol == symbol_id;
// else if (strcmp(non_terminal, "term") == 0)
// return symbol == symbol_open_paren ||
// symbol == symbol_not ||
// symbol == symbol_number ||
// symbol == symbol_id;
// else if (strcmp(non_terminal, "simple_expr") == 0)
// return symbol == symbol_plus ||
// symbol == symbol_minus ||
// symbol == symbol_open_paren ||
// symbol == symbol_not ||
// symbol == symbol_number ||
// symbol == symbol_id;
// else if (strcmp(non_terminal, "expr") == 0)
// return symbol == symbol_plus ||
// symbol == symbol_minus ||
// symbol == symbol_open_paren ||
// symbol == symbol_not ||
// symbol == symbol_number ||
// symbol == symbol_id;
// else if (strcmp(non_terminal, "assignment") == 0)
// return symbol == symbol_becomes;
// else if (strcmp(non_terminal, "actual_params") == 0)
// return symbol == symbol_open_paren;
// else if (strcmp(non_terminal, "proc_call") == 0)
// return symbol == symbol_open_paren ||
// symbol == symbol_null;
// else if (strcmp(non_terminal, "if_stmt") == 0)
// return symbol == symbol_if;
// else if (strcmp(non_terminal, "while_stmt") == 0)
// return symbol == symbol_while;
// else if (strcmp(non_terminal, "repeat_stmt") == 0)
// return symbol == symbol_repeat;
// else if (strcmp(non_terminal, "stmt") == 0)
// return symbol == symbol_id ||
// symbol == symbol_if ||
// symbol == symbol_while ||
// symbol == symbol_repeat ||
// symbol == symbol_null;
// else if (strcmp(non_terminal, "stmt_sequence") == 0)
// return symbol == symbol_id ||
// symbol == symbol_if ||
// symbol == symbol_while ||
// symbol == symbol_repeat ||
// symbol == symbol_null;
// else if (strcmp(non_terminal, "id_list") == 0)
// return symbol == symbol_id;
// else if (strcmp(non_terminal, "array_type") == 0)
// return symbol == symbol_array;
// else if (strcmp(non_terminal, "field_list") == 0)
// return symbol == symbol_id ||
// symbol == symbol_null;
// else if (strcmp(non_terminal, "record_type") == 0)
// return symbol == symbol_record;
// else if (strcmp(non_terminal, "type") == 0)
// return symbol == symbol_id ||
// symbol == symbol_array ||
// symbol == symbol_record;
// else if (strcmp(non_terminal, "formal_params_section") == 0)
// return symbol == symbol_id ||
// symbol == symbol_var;
// else if (strcmp(non_terminal, "formal_params") == 0)
// return symbol == symbol_open_paren;
// else if (strcmp(non_terminal, "proc_head") == 0)
// return symbol == symbol_proc;
// else if (strcmp(non_terminal, "proc_body") == 0)
// return symbol == symbol_end ||
// symbol == symbol_const ||
// symbol == symbol_type ||
// symbol == symbol_var ||
// symbol == symbol_proc ||
// symbol == symbol_begin;
// else if (strcmp(non_terminal, "proc_decl") == 0)
// return symbol == symbol_proc;
// else if (strcmp(non_terminal, "const_decl") == 0)
// return symbol == symbol_const;
// else if (strcmp(non_terminal, "type_decl") == 0)
// return symbol == symbol_type;
// else if (strcmp(non_terminal, "var_decl") == 0)
// return symbol == symbol_var;
// else if (strcmp(non_terminal, "declarations") == 0)
// return symbol == symbol_const ||
// symbol == symbol_type ||
// symbol == symbol_var ||
// symbol == symbol_proc ||
// symbol == symbol_begin;
// else if (strcmp(non_terminal, "module") == 0)
// return symbol == symbol_module;
return false;
}
bool is_follow(const char *non_terminal, symbol_t symbol)
{
// if (strcmp(non_terminal, "selector") == 0)
// return symbol == symbol_times ||
// symbol == symbol_div ||
// symbol == symbol_mod ||
// symbol == symbol_and ||
// symbol == symbol_plus ||
// symbol == symbol_minus ||
// symbol == symbol_or ||
// symbol == symbol_equal ||
// symbol == symbol_not_equal ||
// symbol == symbol_less ||
// symbol == symbol_less_equal ||
// symbol == symbol_greater ||
// symbol == symbol_greater_equal ||
// symbol == symbol_comma ||
// symbol == symbol_close_paren ||
// symbol == symbol_close_bracket ||
// symbol == symbol_becomes ||
// symbol == symbol_of ||
// symbol == symbol_then ||
// symbol == symbol_do ||
// symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "factor") == 0)
// return symbol == symbol_times ||
// symbol == symbol_div ||
// symbol == symbol_mod ||
// symbol == symbol_and ||
// symbol == symbol_plus ||
// symbol == symbol_minus ||
// symbol == symbol_or ||
// symbol == symbol_equal ||
// symbol == symbol_not_equal ||
// symbol == symbol_less ||
// symbol == symbol_less_equal ||
// symbol == symbol_greater ||
// symbol == symbol_greater_equal ||
// symbol == symbol_comma ||
// symbol == symbol_close_paren ||
// symbol == symbol_close_bracket ||
// symbol == symbol_becomes ||
// symbol == symbol_of ||
// symbol == symbol_then ||
// symbol == symbol_do ||
// symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "term") == 0)
// return symbol == symbol_plus ||
// symbol == symbol_minus ||
// symbol == symbol_or ||
// symbol == symbol_equal ||
// symbol == symbol_not_equal ||
// symbol == symbol_less ||
// symbol == symbol_less_equal ||
// symbol == symbol_greater ||
// symbol == symbol_greater_equal ||
// symbol == symbol_comma ||
// symbol == symbol_close_paren ||
// symbol == symbol_close_bracket ||
// symbol == symbol_becomes ||
// symbol == symbol_of ||
// symbol == symbol_then ||
// symbol == symbol_do ||
// symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "simple_expr") == 0)
// return symbol == symbol_plus ||
// symbol == symbol_minus ||
// symbol == symbol_or ||
// symbol == symbol_equal ||
// symbol == symbol_not_equal ||
// symbol == symbol_less ||
// symbol == symbol_less_equal ||
// symbol == symbol_greater ||
// symbol == symbol_greater_equal ||
// symbol == symbol_comma ||
// symbol == symbol_close_paren ||
// symbol == symbol_close_bracket ||
// symbol == symbol_becomes ||
// symbol == symbol_of ||
// symbol == symbol_then ||
// symbol == symbol_do ||
// symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "expr") == 0)
// return symbol == symbol_comma ||
// symbol == symbol_close_paren ||
// symbol == symbol_close_bracket ||
// symbol == symbol_becomes ||
// symbol == symbol_of ||
// symbol == symbol_then ||
// symbol == symbol_do ||
// symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "assignment") == 0)
// return symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "actual_params") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "proc_call") == 0)
// return symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "if_stmt") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "while_stmt") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "repeat_stmt") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "stmt") == 0)
// return symbol == symbol_semicolon ||
// symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "stmt_sequence") == 0)
// return symbol == symbol_end ||
// symbol == symbol_else ||
// symbol == symbol_elsif ||
// symbol == symbol_until;
// else if (strcmp(non_terminal, "id_list") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "array_type") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "field_list") == 0)
// return symbol == symbol_semicolon ||
// symbol == symbol_end;
// else if (strcmp(non_terminal, "record_type") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "type") == 0)
// return symbol == symbol_close_paren ||
// symbol == symbol_semicolon;
// else if (strcmp(non_terminal, "formal_params_section") == 0)
// return symbol == symbol_close_paren ||
// symbol == symbol_semicolon;
// else if (strcmp(non_terminal, "formal_params") == 0)
// return symbol == symbol_semicolon;
// else if (strcmp(non_terminal, "proc_head") == 0)
// return symbol == symbol_semicolon;
// else if (strcmp(non_terminal, "proc_body") == 0)
// return symbol == symbol_semicolon;
// else if (strcmp(non_terminal, "proc_decl") == 0)
// return symbol == symbol_semicolon;
// else if (strcmp(non_terminal, "const_decl") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "type_decl") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "var_decl") == 0)
// return symbol == symbol_null;
// else if (strcmp(non_terminal, "declarations") == 0)
// return symbol == symbol_end ||
// symbol == symbol_begin;
// else if (strcmp(non_terminal, "module") == 0)
// return symbol == symbol_eof;
return false;
}
bool scan()
{
if (current_token.lexem.symbol == symbol_eof)
return false;
do {
read_token();
} while (current_token.lexem.symbol == symbol_null);
return current_token.lexem.symbol == symbol_eof;
}
// Se “symbol_null” for passado como parâmetro, qualquer símbolo será reconhecido
bool verify(symbol_t symbol, bool mark_error, bool next)
{
if (current_token.lexem.symbol == symbol || symbol == symbol_null) {
if (should_log)
mark(error_log, "\"%s\" found.", current_token.lexem.id);
if (next)
scan();
return true;
}
if (mark_error)
mark_missing(symbol);
return false;
}
static inline bool consume(symbol_t symbol)
{
return verify(symbol, true, true);
}
// Esta função faz o mesmo que “consume”, mas não avança para a próxima ficha léxica
static inline bool assert(symbol_t symbol)
{
return verify(symbol, true, false);
}
// As versões com “try_” não mostram mensagens de erro caso não encontrem o símbolo passado como parâmetro
static inline bool try_consume(symbol_t symbol)
{
return verify(symbol, false, true);
}
static inline bool try_assert(symbol_t symbol)
{
return verify(symbol, false, false);
}
void expr(item_t *item);
// selector = {"." id | "[" expr "]"}
void selector(item_t *item, token_t entry_token)
{
position_t position = entry_token.position;
while (is_first("selector", current_token.lexem.symbol)) {
if (try_consume(symbol_period)) {
assert(symbol_id);
// TODO: Remover as verificações para “item” quando possível
if (!item || !item->type || item->type->form != form_record)
mark_at(error_parser, position, "Invalid record.");
else {
entry_t *field = find_entry(current_token.lexem.id, item->type->fields);
if (!field) {
mark(error_parser, "\"%s\" is not a valid field.", current_token.lexem.id);
// TODO: Mudar o endereçamento ao invés de anular o tipo?
item->type = NULL;
}
else {
// Lembrando: o endereço (“address”) do campo corresponde ao seu deslocamento com base no endereço do registro
// em si e não ao seu endereço global atual
if (item->addressing == addressing_indirect)
write_field_offset(item, field->address);
else
item->address = item->address + field->address;
item->type = field->type;
}
}
position = current_token.position;
scan();
}
else if (try_assert(symbol_open_bracket)) {
position_t open_pos = current_token.position;
scan();
if (!item || !item->type || item->type->form != form_array)
mark_at(error_parser, position, "Invalid array.");
else {
position_t index_pos = current_token.position;
item_t index_item;
expr(&index_item);
if (item->addressing != addressing_indirect && index_item.addressing == addressing_immediate) {
if (index_item.value < 0 || index_item.value > item->type->length - 1) {
mark_at(error_parser, index_pos, "Index is out of bounds.");
item->type = NULL;
}
else {
item->address = item->address + (index_item.value * item->type->base->size);
item->type = item->type->base;
}
}
else {
write_index_offset(item, &index_item);
item->type = item->type->base;
}
}
position = current_token.position;
if (try_assert(symbol_close_bracket))
scan();
else
mark(error_parser, "Missing \"]\" for (%d, %d).", open_pos.line, open_pos.column);
}
}
}
// factor = id selector | number | "(" expr ")" | "~" factor
void factor(item_t *item)
{
if (try_assert(symbol_id)) {
// TODO: Remover as verificações para “item” quando possível
token_t entry_token = current_token;
if (item)
item->addressing = addressing_unknown;
entry_t *entry = find_entry(current_token.lexem.id, symbol_table);
if (!entry)
mark(error_parser, "\"%s\" hasn't been declared yet.", current_token.lexem.id);
else {
// Como a tabela de símbolos armazena tanto variáveis e constantes, quanto tipos e procedimentos, é possível que o
// identificador encontrado não seja um fator válido (variável ou constante)
if (entry->class != class_var && entry->class != class_const)
mark(error_parser, "\"%s\" is not a valid factor.", current_token.lexem.id);
else {
if (item) {
item->addressing = (addressing_t)entry->class;
item->address = entry->address;
item->type = entry->type;
item->value = entry->value;
}
}
}
scan();
selector(item, entry_token);
}
else if (try_assert(symbol_integer)) {
if (item) {
item->addressing = addressing_immediate;
item->value = current_token.value;
}
scan();
}
else if (try_assert(symbol_open_paren)) {
position_t p = current_token.position;
scan();
expr(item);
if (try_assert(symbol_close_paren))
scan();
else
mark(error_parser, "Missing \")\" for (%d, %d).", p.line, p.column);
}
else if (try_consume(symbol_not)) {
factor(item);
write_unary_op(symbol_not, item);
}
else {
mark(error_parser, "Missing factor.");
// Sincroniza
while (!is_follow("factor", current_token.lexem.symbol) && scan());
}
}
// term = factor {("*" | "div" | "mod" | "&") factor}
void term(item_t *item)
{
factor(item);
while (current_token.lexem.symbol >= symbol_times && current_token.lexem.symbol <= symbol_and) {
symbol_t symbol = current_token.lexem.symbol;
consume(symbol);
// TODO: Gerar a instrução de salto para o caminho falso para o operador lógico “&”
item_t rhs_item;
factor(&rhs_item);
write_binary_op(symbol, item, &rhs_item);
}
}
// simple_expr = ["+" | "-"] term {("+" | "-" | "OR") term}
void simple_expr(item_t *item)
{
if (try_consume(symbol_plus)) {
term(item);
} else if (try_consume(symbol_minus)) {
term(item);
write_unary_op(symbol_minus, item);
} else {
term(item);
}
while (current_token.lexem.symbol >= symbol_plus && current_token.lexem.symbol <= symbol_or) {
symbol_t symbol = current_token.lexem.symbol;
consume(symbol);
// TODO: Gerar a instrução de salto para o caminho verdadeiro para o operador lógico “or”
item_t rhs_item;
term(&rhs_item);
write_binary_op(symbol, item, &rhs_item);
}
}
// expr = simple_expr [("=" | "#" | "<" | "<=" | ">" | ">=") simple_expr]
void expr(item_t *item)
{
simple_expr(item);
if (current_token.lexem.symbol >= symbol_equal && current_token.lexem.symbol <= symbol_greater_equal) {
symbol_t symbol = current_token.lexem.symbol;
consume(symbol);
item_t rhs_item;
simple_expr(&rhs_item);
write_comparison(symbol, item, &rhs_item);
item->type = boolean_type->type;
}
}
// actual_params = "(" [expr {"," expr}] ")"
void actual_params()
{
try_assert(symbol_open_paren);
position_t open_pos = current_token.position;
scan();
if (is_first("expr", current_token.lexem.symbol)) {
item_t item;
expr(&item);
while (try_consume(symbol_comma))
expr(&item);
}
if (try_assert(symbol_close_paren))
scan();
else
mark(error_parser, "Missing \")\" for (%d, %d).", open_pos.line, open_pos.column);
}
// proc_call = [actual_params]
void proc_call(entry_t *entry)
{
if (is_first("actual_params", current_token.lexem.symbol))
actual_params();
}
void stmt_sequence();
// if_stmt = "if" expr "then" stmt_sequence {"elsif" expr "then" stmt_sequence} ["else" stmt_sequence] "end"
void if_stmt()
{
item_t expr_item, end_item;
try_consume(symbol_if);
expr(&expr_item);
expr_item.links = NULL;
write_inverse_branch(&expr_item, true);
consume(symbol_then);
stmt_sequence();
// Este item serve como base para o salto para o final da estrutura condicional
end_item.addressing = addressing_condition;
end_item.condition = symbol_null;
end_item.links = NULL;
while (try_consume(symbol_elsif)) {
write_branch(&end_item, true);
write_label(&expr_item, NULL);
write_fixup(&expr_item);
expr(&expr_item);
write_inverse_branch(&expr_item, true);
consume(symbol_then);
stmt_sequence();
}
if (try_consume(symbol_else)) {
write_branch(&end_item, true);
write_label(&expr_item, NULL);
write_fixup(&expr_item);
stmt_sequence();
write_label(&end_item, NULL);
write_fixup(&end_item);
}
else {
write_label(&expr_item, NULL);
write_fixup(&expr_item);
if (end_item.links) {
strcpy(end_item.label, expr_item.label);
write_fixup(&end_item);
}
}
consume(symbol_end);
}
// while_stmt = "while" expr "do" stmt_sequence "end"
void while_stmt()
{
try_consume(symbol_while);
item_t expr_item;
expr_item.links = NULL;
expr(&expr_item);
write_inverse_branch(&expr_item, true);
consume(symbol_do);
item_t back_item;
back_item.addressing = addressing_condition;
back_item.condition = symbol_null;
back_item.links = NULL;
write_label(&back_item, NULL);
stmt_sequence();
write_branch(&back_item, false);
consume(symbol_end);
write_label(&expr_item, NULL);
write_fixup(&expr_item);
}
// repeat_stmt = "repeat" stmt_sequence "until" expr
void repeat_stmt()
{
try_consume(symbol_repeat);
item_t expr_item;
expr_item.links = NULL;
write_label(&expr_item, NULL);
stmt_sequence();
consume(symbol_until);
expr(&expr_item);
write_inverse_branch(&expr_item, false);
}
// assignment = ":=" expr
void assignment(item_t *item)
{
try_consume(symbol_becomes);
item_t expr_item;
expr(&expr_item);
write_store(item, &expr_item);
}
// stmt = [id selector (assignment | proc_call) | if_stmt | while_stmt | repeat_stmt]
void stmt()
{
if (try_assert(symbol_id)) {
item_t item;
item.addressing = addressing_unknown;
token_t entry_token = current_token;
entry_t *entry = find_entry(current_token.lexem.id, symbol_table);
if (!entry)
mark(error_parser, "\"%s\" hasn't been declared yet.", current_token.lexem.id);
else {
if (entry->class != class_var)
mark(error_parser, "\"%s\" is not a variable.", entry->id);
else {
item.addressing = addressing_register;
item.address = entry->address;
item.type = entry->type;
}
}
scan();
selector(&item, entry_token);
if (is_first("assignment", current_token.lexem.symbol))
assignment(&item);
else if (is_first("proc_call", current_token.lexem.symbol) || is_follow("proc_call", current_token.lexem.symbol))
proc_call(entry);
else {
mark(error_parser, "Invalid statement.");
// Sincroniza
while (!is_follow("stmt", current_token.lexem.symbol) && scan());
}
}
else if (is_first("if_stmt", current_token.lexem.symbol))
if_stmt();
else if (is_first("while_stmt", current_token.lexem.symbol))
while_stmt();
else if (is_first("repeat_stmt", current_token.lexem.symbol))
repeat_stmt();
if (!is_follow("stmt", current_token.lexem.symbol)) {
mark(error_parser, "Missing \";\" or \"end\".");
// Sincroniza
while (!is_follow("stmt", current_token.lexem.symbol) && scan());
}
}
// stmt_sequence = stmt {";" stmt}
void stmt_sequence()
{
stmt();
while (try_consume(symbol_semicolon))
stmt();
}
// id_list = id {"," id}
entry_t *id_list()
{
try_assert(symbol_id);
entry_t *new_entries = create_entry(current_token.lexem.id, current_token.position, class_var);
scan();
while (try_consume(symbol_comma)) {
if (assert(symbol_id)) {
add_entry(create_entry(current_token.lexem.id, current_token.position, class_var), &new_entries);
scan();
}
}
return new_entries;
}
type_t *type();
// array_type = "array" expr "of" type
type_t *array_type()
{
type_t *new_type = NULL;
try_consume(symbol_array);
new_type = create_type(form_array, 0, 0, NULL, NULL);
// expr();
value_t length = 0;
if (assert(symbol_number)) {
length = current_token.value;
scan();
}
consume(symbol_of);
type_t *base_type = type();
unsigned int size = 0;
if (base_type)
size = length * base_type->size;
if (new_type) {
new_type->length = length;
new_type->size = size;
new_type->base = base_type;
}
return new_type;
}
// field_list = [id_list ":" type]
entry_t *field_list()
{
if (is_first("id_list", current_token.lexem.symbol)) {
entry_t *new_fields = id_list();
consume(symbol_colon);
type_t *base_type = type();
entry_t *e = new_fields;
while (e) {
e->type = base_type;
e = e->next;
}
return new_fields;
}
return NULL;
}
// record_type = "record" field_list {";" field_list} "end"
type_t *record_type()
{
type_t *new_type = NULL;
try_consume(symbol_record);
new_type = create_type(form_record, 0, 0, NULL, NULL);
entry_t *fields = field_list();
while (try_consume(symbol_semicolon)) {
entry_t *more_fields = field_list();
if (fields && more_fields)
add_entry(more_fields, &fields);
}
// Efetua o cálculo do tamanho do tipo registro e dos deslocamentos de cada campo
unsigned int size = 0;
address_t offset = 0;
if (fields) {
entry_t *e = fields;
while (e && e->type) {
e->address = offset;
offset += e->type->size;
size += e->type->size;
e = e->next;
}
}
consume(symbol_end);
if (new_type) {
new_type->size = size;
new_type->fields = fields;
}
return new_type;
}
// type = id | array_type | record_type
type_t *type()
{
if (try_assert(symbol_id)) {
// Qualquer tipo atômico deve ser baseado em um dos tipos internos da linguagem (neste caso apenas “integer”)
entry_t *entry = find_entry(current_token.lexem.id, symbol_table);
if (entry) {
scan();
return entry->type;
} else {
mark(error_parser, "Unknown type \"%s\".", current_token.lexem.id);
scan();
return NULL;
}
} else if (is_first("array_type", current_token.lexem.symbol)) {
type_t *new_type = array_type();
if (!new_type)
mark(error_parser, "Invalid array type.");
return new_type;
} else if (is_first("record_type", current_token.lexem.symbol)) {
type_t *new_type = record_type();
if (!new_type)
mark(error_parser, "Invalid record type.");
return new_type;
}
// Sincroniza
mark(error_parser, "Missing type.");
while (!is_follow("type", current_token.lexem.symbol) && current_token.lexem.symbol != symbol_eof)
scan();
return NULL;
}
// formal_params_section = ["var"] id_list ":" type
entry_t *formal_params_section()
{
if (try_consume(symbol_var));
// TODO: Implementar a passagem de parâmetro por referência
entry_t *new_params = id_list();
if (!consume(symbol_colon))
mark_missing(symbol_colon);
type_t *base_type = type();
entry_t *e = new_params;
while (e) {
e->type = base_type;
e = e->next;
}
return new_params;
}
// formal_params = "(" [formal_params_section {";" formal_params_section}] ")"
entry_t *formal_params()
{
entry_t *params = NULL;
try_consume(symbol_open_paren);
if (is_first("formal_params_section", current_token.lexem.symbol)) {
params = formal_params_section();
while (try_consume(symbol_semicolon))
add_entry(formal_params_section(), ¶ms);
}
consume(symbol_close_paren);
return params;
}
// proc_head = "procedure" id [formal_params]
void proc_head()
{
try_consume(symbol_proc);
consume(symbol_id);
// TODO: Implementar parâmetros para a entrada do procedimento na tabela de símbolos
if (is_first("formal_params", current_token.lexem.symbol))
formal_params();
}
void declarations();
// proc_body = declarations ["begin" stmt_sequence] "end" id
void proc_body()
{
declarations();
if (try_consume(symbol_begin))
stmt_sequence();
consume(symbol_end);
consume(symbol_id);
}
// proc_decl = proc_head ";" proc_body
void proc_decl()
{
proc_head();
consume(symbol_semicolon);
proc_body();
}
// const_decl = "const" {id "=" expr ";"}
void const_decl()
{
try_consume(symbol_const);
while (try_assert(symbol_id)) {
entry_t *new_entry = create_entry(current_token.lexem.id, current_token.position, class_const);
scan();
consume(symbol_equal);
// expr();
if (assert(symbol_number)) {
if (new_entry) {
new_entry->value = current_token.value;
add_entry(new_entry, &symbol_table);
}
scan();
}
consume(symbol_semicolon);
}
}
// type_decl = "type" {id "=" type ";"}
void type_decl()
{
try_consume(symbol_type);
while (try_assert(symbol_id)) {
entry_t *new_entry = create_entry(current_token.lexem.id, current_token.position, class_type);
scan();
// TODO: Melhorar erro para o “igual”
consume(symbol_equal);
type_t *base = type();
if (new_entry && base) {
new_entry->type = base;
add_entry(new_entry, &symbol_table);
}
consume(symbol_semicolon);
}
}
// var_decl = "var" {id_list ":" type ";"}
void var_decl()
{
try_consume(symbol_var);
while (is_first("id_list", current_token.lexem.symbol)) {
entry_t *new_entries = id_list();
consume(symbol_colon);
type_t *base = type();
entry_t *e = new_entries;
while (e && base) {
e->address = current_address;
e->type = base;
current_address += e->type->size;
e = e->next;
}
add_entry(new_entries, &symbol_table);
consume(symbol_semicolon);
}
}
// declarations = [const_decl] [type_decl] [var_decl] {proc_decl ";"}
void declarations()
{
if (is_first("const_decl", current_token.lexem.symbol))
const_decl();
if (is_first("type_decl", current_token.lexem.symbol))
type_decl();
if (is_first("var_decl", current_token.lexem.symbol))
var_decl();
while (is_first("proc_decl", current_token.lexem.symbol)) {
proc_decl();
consume(symbol_semicolon);
}
}
// module = "module" id ";" declarations ["begin" stmt_sequence] "end" id "."
void module()
{
consume(symbol_module);
consume(symbol_id);
consume(symbol_semicolon);
declarations();
if (try_consume(symbol_begin))
stmt_sequence();
consume(symbol_end);
consume(symbol_id);
consume(symbol_period);
}
// Retorna se a inicialização do analisador léxico e da tabela de símbolos obteve sucesso ou não e se o arquivo de
// entrada estava em branco
bool initialize_parser(FILE *file)
{
should_log = false;
if (!initialize_table(0, &symbol_table))
return false;
initialize_scanner(file);
read_token();
return current_token.lexem.symbol != symbol_eof;
}
bool parse()
{
module();
clear_table(&symbol_table);
return true;
}