aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam NAILI2018-05-04 01:03:21 +0200
committerAdam NAILI2018-05-04 01:03:21 +0200
commitc9c4fbbde6e4f4f3942777cd9930cf40375e0ea9 (patch)
treedc6c860b2934dadd75a000bd797934371a651f64
parent2793b31c330396956e4fbeef454e26a37b055ebd (diff)
downloadtpc-compiler-c9c4fbbde6e4f4f3942777cd9930cf40375e0ea9.tar.gz
Add sub working (local/global not fully implemented, checking type function available
-rw-r--r--res/nani.tpc1
-rw-r--r--res/test-table-symboles.tpc5
-rw-r--r--res/test_add.tpc11
-rw-r--r--src/symboltable.c124
-rw-r--r--src/symboltable.h3
-rw-r--r--src/tpc.y92
6 files changed, 172 insertions, 64 deletions
diff --git a/res/nani.tpc b/res/nani.tpc
new file mode 100644
index 0000000..dbb3731
--- /dev/null
+++ b/res/nani.tpc
@@ -0,0 +1 @@
/*Programme correct avec le compilateur mais qui ne devrait pas*/
diff --git a/res/test-table-symboles.tpc b/res/test-table-symboles.tpc
index 3c12144..b2cc31e 100644
--- a/res/test-table-symboles.tpc
+++ b/res/test-table-symboles.tpc
@@ -1,10 +1,11 @@
1/* test-table-symboles.tpc */ 1/* test-table-symboles.tpc */
2 2
3/* Test file for simplified translator of a declaration of variables in C */ 3/* Test file for simplified translator of a declaration of variables in C */
4 entier r1,b,s,c,r2 ; 4entier r1,b,s,c,r2 ;
5 caractere letter, digit, punct; 5caractere letter, digit, punct;
6 6
7entier main(void) { 7entier main(void) {
8
8 r1=12; 9 r1=12;
9 r2=24; 10 r2=24;
10 b=r1+r2; 11 b=r1+r2;
diff --git a/res/test_add.tpc b/res/test_add.tpc
new file mode 100644
index 0000000..01e0403
--- /dev/null
+++ b/res/test_add.tpc
@@ -0,0 +1,11 @@
1/* test-table-symboles.tpc */
2
3/* Test file for simplified translator of a declaration of variables in C */
4
5entier main(void) {
6 entier a,b,res;
7 a = 2;
8 b = 3;
9 res = a - b;
10 print(res);
11}
diff --git a/src/symboltable.c b/src/symboltable.c
index cdbd442..5c63d3f 100644
--- a/src/symboltable.c
+++ b/src/symboltable.c
@@ -2,28 +2,30 @@
2 2
3extern int lineno; /* from lexical analyser */ 3extern int lineno; /* from lexical analyser */
4 4
5SymbolTable glo_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; 5SymbolTable glo_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
6SymbolTable loc_symbol_table = {{{{0},0}},MAXSYMBOLS,0}; 6SymbolTable loc_symbol_table = {{{{0}, 0}}, MAXSYMBOLS, 0};
7 7
8void glo_addVar(const char name[], int type) { 8void glo_addVar(const char name[], int type) {
9 int count; 9 int count;
10 for (count = 0; count < glo_symbol_table.size; count++) { 10 for (count = 0; count < glo_symbol_table.size; count++) {
11 if (!strcmp(glo_symbol_table.entries[count].name, name)) { 11 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
12 fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, 12 fprintf(stderr,
13 lineno); 13 "semantic error, redefinition of variable %s near line %d\n",
14 name, lineno);
14 return; 15 return;
15 } 16 }
16 } 17 }
17 if (++glo_symbol_table.size > glo_symbol_table.maxsize) { 18 if (++glo_symbol_table.size > glo_symbol_table.maxsize) {
18 fprintf(stderr,"too many variables near line %d\n", lineno); 19 fprintf(stderr, "too many variables near line %d\n", lineno);
19 exit(1); 20 exit(1);
20 } 21 }
21 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); 22 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name);
22 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; 23 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type;
23 glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1)*8; 24 glo_symbol_table.entries[glo_symbol_table.size - 1].addr =
25 (glo_symbol_table.size - 1) * 8;
24} 26}
25 27
26//Verifies that the variable exists and returns the type 28// Verifies that the variable exists and returns the type
27int glo_lookup(const char name[]) { 29int glo_lookup(const char name[]) {
28 int count; 30 int count;
29 31
@@ -32,43 +34,58 @@ int glo_lookup(const char name[]) {
32 return glo_symbol_table.entries[count].type; 34 return glo_symbol_table.entries[count].type;
33 } 35 }
34 } 36 }
35 fprintf(stderr,"No definition of the variable %s near line %d\n", name, 37 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
36 lineno); 38 lineno);
37 return -1; 39 return -1;
38} 40}
41int glo_get_addr(const char name[]) {
42 int count;
39 43
40 44 for (count = 0; count < glo_symbol_table.size; count++) {
41void glo_display_table(){ 45 if (!strcmp(glo_symbol_table.entries[count].name, name)) {
42 int count; 46 return glo_symbol_table.entries[count].addr;
43 for (count=0;count<glo_symbol_table.size;count++) {
44 if(glo_symbol_table.entries[count].type == INT)
45 printf("entier: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr);
46 else
47 printf("caractere: %s, pos: %d \n", glo_symbol_table.entries[count].name, glo_symbol_table.entries[count].addr);
48 } 47 }
49 printf("\n"); 48 }
49 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
50 lineno);
51 return -1;
50} 52}
51 53
54void glo_display_table() {
55 int count;
56 for (count = 0; count < glo_symbol_table.size; count++) {
57 if (glo_symbol_table.entries[count].type == INT)
58 printf(";entier: %s, pos: %d \n",
59 glo_symbol_table.entries[count].name,
60 glo_symbol_table.entries[count].addr);
61 else
62 printf(";caractere: %s, pos: %d \n",
63 glo_symbol_table.entries[count].name,
64 glo_symbol_table.entries[count].addr);
65 }
66 printf("\n");
67}
52 68
53void loc_addVar(const char name[], int type) { 69void loc_addVar(const char name[], int type) {
54 int count; 70 int count;
55 for (count = 0; count < loc_symbol_table.size; count++) { 71 for (count = 0; count < loc_symbol_table.size; count++) {
56 if (!strcmp(loc_symbol_table.entries[count].name, name)) { 72 if (!strcmp(loc_symbol_table.entries[count].name, name)) {
57 fprintf(stderr,"semantic error, redefinition of variable %s near line %d\n", name, 73 fprintf(stderr,
58 lineno); 74 "semantic error, redefinition of variable %s near line %d\n",
75 name, lineno);
59 return; 76 return;
60 } 77 }
61 } 78 }
62 if (++glo_symbol_table.size > glo_symbol_table.maxsize) { 79 if (++loc_symbol_table.size > loc_symbol_table.maxsize) {
63 fprintf(stderr,"too many variables near line %d\n", lineno); 80 fprintf(stderr, "too many variables near line %d\n", lineno);
64 exit(1); 81 exit(1);
65 } 82 }
66 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); 83 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name);
67 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; 84 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type;
68 glo_symbol_table.entries[glo_symbol_table.size - 1].addr = (glo_symbol_table.size - 1)*8; 85 loc_symbol_table.entries[loc_symbol_table.size - 1].addr =
86 (loc_symbol_table.size - 1) * 8;
69} 87}
70 88
71
72int loc_lookup(const char name[]) { 89int loc_lookup(const char name[]) {
73 int count; 90 int count;
74 91
@@ -77,17 +94,52 @@ int loc_lookup(const char name[]) {
77 return loc_symbol_table.entries[count].type; 94 return loc_symbol_table.entries[count].type;
78 } 95 }
79 } 96 }
80 fprintf(stderr,"No definition of the variable %s near line %d\n", name, 97 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
81 lineno); 98 lineno);
82 return -1; 99 return -1;
83} 100}
84void loc_display_table(){ 101
85 int count; 102int loc_get_addr(const char name[]) {
86 for (count=0;count<loc_symbol_table.size;count++) { 103 int count;
87 if(loc_symbol_table.entries[count].type == INT) 104
88 printf("entier: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); 105 for (count = 0; count < loc_symbol_table.size; count++) {
89 else 106 if (!strcmp(loc_symbol_table.entries[count].name, name)) {
90 printf("caractere: %s, pos: %d \n", loc_symbol_table.entries[count].name, loc_symbol_table.entries[count].addr); 107 return loc_symbol_table.entries[count].addr;
91 } 108 }
92 printf("\n"); 109 }
110 fprintf(stderr, "No definition of the variable %s near line %d\n", name,
111 lineno);
112 return -1;
113}
114void loc_display_table() {
115 int count;
116 for (count = 0; count < loc_symbol_table.size; count++) {
117 if (loc_symbol_table.entries[count].type == INT)
118 printf(";entier: %s, pos: %d \n",
119 loc_symbol_table.entries[count].name,
120 loc_symbol_table.entries[count].addr);
121 else
122 printf(";caractere: %s, pos: %d \n",
123 loc_symbol_table.entries[count].name,
124 loc_symbol_table.entries[count].addr);
125 }
126 printf("\n");
127}
128
129void loc_clean_table() {
130 int i;
131 for (i = 0; i < loc_symbol_table.size; i++) {
132 printf("pop eax\n");
133 }
134 loc_symbol_table.size = 0;
135}
136
137void check_expected_type(int type_to_check, int type_expected) {
138 if (type_to_check != type_expected)
139 fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n",
140 type_expected == INT ? "INT" : type_expected == CHAR ? "CHAR"
141 : "UNDEFINED",
142 type_to_check == INT ? "INT" : type_to_check == CHAR ? "CHAR"
143 : "UNDEFINED",
144 lineno);
93} \ No newline at end of file 145} \ No newline at end of file
diff --git a/src/symboltable.h b/src/symboltable.h
index bd7a373..ac6db36 100644
--- a/src/symboltable.h
+++ b/src/symboltable.h
@@ -26,8 +26,11 @@ typedef struct {
26 26
27void glo_addVar(const char name[], int type); 27void glo_addVar(const char name[], int type);
28int glo_looku