Browse Source

start implementing symbol table

Arnaud Vergnet 3 years ago
parent
commit
d250a8f837
6 changed files with 134 additions and 53 deletions
  1. 5
    4
      .gitignore
  2. 6
    4
      Makefile
  3. 93
    40
      as.y
  4. 5
    0
      symbol_table.c
  5. 25
    0
      symbol_table.h
  6. 0
    5
      test.c

+ 5
- 4
.gitignore View File

@@ -1,5 +1,6 @@
1
-as.tab.c
2
-as.tab.h
3
-compilateur
4
-lex.yy.c
1
+/as.tab.c
2
+/as.tab.h
3
+/compilateur
4
+/lex.yy.c
5 5
 
6
+/as.output

+ 6
- 4
Makefile View File

@@ -1,9 +1,11 @@
1 1
 LEX = flex
2
-YACC = bison -d
2
+YACC = bison -d -v
3 3
 CC = gcc
4 4
 
5
-compilateur: as.tab.c lex.yy.c
6
-	$(CC) -o compilateur as.tab.c lex.yy.c
5
+all: compilateur
6
+
7
+compilateur: as.tab.c lex.yy.c symbol_table.c
8
+	$(CC) -o compilateur as.tab.c lex.yy.c symbol_table.c
7 9
 
8 10
 as.tab.c: as.y
9 11
 	$(YACC) as.y
@@ -16,4 +18,4 @@ lex.yy.c: al.lex
16 18
 build: compilateur
17 19
 
18 20
 clean:
19
-	rm as.tab.c as.tab.h lex.yy.c compilateur
21
+	rm as.tab.c as.tab.h lex.yy.c compilateur as.output

+ 93
- 40
as.y View File

@@ -47,46 +47,48 @@
47 47
 
48 48
 %%
49 49
 
50
-S : Main ;
50
+S : Main { printf("S\n"); } ;
51 51
 
52
-Main : tINT tMAIN tPO Params tPF MainBody ;
52
+Main : tINT tMAIN tPO Params tPF MainBody { printf("Main\n"); } ;
53 53
 
54
-Params : | Param SuiteParams ;
55
-Param : tINT tID ;
54
+Params : | Param SuiteParams { printf("Params\n"); } ;
55
+Param : tINT tID  { printf("Param\n"); } ;
56 56
 
57
-SuiteParams : tVIRG Param SuiteParams ;
57
+SuiteParams : tVIRG Param SuiteParams  { printf("SuiteParam\n"); } ;
58 58
 
59
-MainBody : tAO Declarations Instructions tAF ;
59
+MainBody : tAO Declarations Instructions tAF { printf("MainBody\n"); } ;
60 60
 
61
-Body : tAO Instructions tAF ;
61
+Body : tAO Instructions tAF  { printf("Body\n"); } ;
62 62
 
63
-Instructions : Instruction Instructions | ;
63
+Instructions : Instruction Instructions |  { printf("Instructions\n"); } ;
64 64
 
65
-Instruction : Aff | Printf  | If ;
65
+Instruction : Aff | Printf { printf("Instruction\n"); } ;
66 66
 
67
-Aff : tID tEQ E tPV ;
67
+Aff : tID tEQ E tPV { printf("Aff\n"); } ;
68 68
 
69
-E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E ;
69
+E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E { printf("E\n"); } ;
70 70
 
71
-Declarations : | Declaration Declarations ;
71
+Declarations : | Declaration Declarations { printf("Declarations\n"); } ;
72 72
 
73
-Declaration : DeclarationInt | DeclarationConst ;
73
+Declaration : DeclarationInt | DeclarationConst { printf("Declaration\n"); } ;
74 74
 
75
-DeclarationInt : tINT DeclarationBody tPV ;
75
+DeclarationInt : tINT DeclarationBody tPV { printf("DeclarationInt\n"); } ;
76 76
 
77
-DeclarationConst : tCONST DeclarationBody tPV ;
77
+DeclarationConst : tCONST DeclarationBody tPV { printf("DeclarationConst\n"); } ;
78 78
 
79
-DeclarationBody : tID tVIRG DeclarationBody | tID ;
79
+DeclarationBody : tID tVIRG DeclarationBody | tID { printf("DeclarationBody\n"); } ;
80 80
 
81
-If : IfSimple | IfElse ;
82
-
83
-IfSimple : tIF tPO Cond tPF Body ;
84
-
85
-IfElse : IfSimple tELSE Body  ;
86
-
87
-Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT Cond | tNOT tPO Cond tPF ;
81
+// While : tWHILE tPO Cond tPF Body { printf("While\n"); } ;
82
+// 
83
+// If : IfSimple | IfElse { printf("If\n"); } ;
84
+// 
85
+// IfSimple : tIF tPO Cond tPF Body { printf("IfSimple\n"); } ;
86
+// 
87
+// IfElse : IfSimple tELSE Body { printf("IfElse\n"); } ;
88
+// 
89
+// Cond : tNOT Cond | Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT tPO Cond tPF { printf("Cond\n"); } ;
88 90
 
89
-Printf : tPRINTF tPO tID tPF tPV
91
+Printf : tPRINTF tPO tID tPF tPV  { printf("Printf\n"); } ;
90 92
 
91 93
 // If : tIF tPO Cond tPF Body ;
92 94
 // 
@@ -98,24 +100,75 @@ Printf : tPRINTF tPO tID tPF tPV
98 100
 
99 101
 // Return : tRET E tPV ;
100 102
 
101
-
102
-/* S -> E ; S
103
- * S ->
104
- */
105
-// S : E tPV
106
-// 						{ printf("RES: %d\n", $1); }
107
-// 		S
108
-// 	|					{ printf("END\n"); }
109
-// 	;
110
-// 
111
-// E : E tADD E	{ $$ = $1 + $3; }
112
-// 	| E tSUB E	{ $$ = $1 - $3; }
113
-// 	| tOB E tCB	{ $$ = $2; }
114
-// 	| tNB				{ $$ = $1; }
115
-// 	;
116
-
117 103
 %%
118 104
 
105
+#include <string.h>
106
+
107
+#define LABEL_SIZE 10
108
+
109
+// ADD @ + @ -> @
110
+// SUB @ - @ -> @
111
+// AFC nb -> @
112
+// CPY @ -> @
113
+
114
+// Exemple
115
+// y = 1 + 2;
116
+// AFC 1  32
117
+// AFC 2  33
118
+// ADD 32 33 32
119
+// CPY 32 @y
120
+
121
+/*
122
+table des symboles (tS) :
123
+sous forme de pile
124
+                     -> où sera la variable lors de l'exec
125
+                    |
126
+|  nom  |  type  |  @  |  init  |
127
+|-------|--------|-----|--------|
128
+|   a   |        |  0  |        |
129
+|   b   |        |  1  |        |
130
+|   c   |        |  2  |        |
131
+|       |        |     |        |
132
+|   .   |        |  .  |        |    -> entiers sur 1 octet
133
+|   .   |        |  .  |        |
134
+|   .   |        |  .  |        |
135
+|       |        |     |        |
136
+|   y   |        | 26  |        |
137
+
138
+Donc on remplace @y par 26 dans CPY 32 @y
139
+
140
+/!\
141
+
142
+E + E + E
143
+|   |   |
144
+->  ->  -> vt variable temporaire (autre expression)
145
+->  ->  -> variable -> ADD @E1 @E2 vt
146
+->  ->  -> nombre
147
+
148
+Donc 9 possibilités (3 par E)
149
+
150
+On suppose que E est tout le temps une vt, donc on génère tout le temps le même code
151
+
152
+*/
153
+
154
+int id_counter = 0;
155
+
156
+typedef struct node {
157
+    char label[LABEL_SIZE];
158
+    int left_child;
159
+    int right_child;
160
+    int id;
161
+} node;
162
+
163
+int create_node(char* l, int n1, int n2) {
164
+     node n;
165
+     strcpy(l, n.label);
166
+     n.left_child = n1;
167
+     n.right_child = n2;
168
+     n.id = id_counter;
169
+     id_counter++;
170
+}
171
+
119 172
 void yyerror(char const* err) {
120 173
     printf("%s\n", err);
121 174
     exit(1);

+ 5
- 0
symbol_table.c View File

@@ -0,0 +1,5 @@
1
+#include "symbol_table.h"
2
+
3
+void add_symbol(SymbolTable table, enum Type type, char *name) {
4
+
5
+}

+ 25
- 0
symbol_table.h View File

@@ -0,0 +1,25 @@
1
+#ifndef SYMBOL_TABLE_H_INCLUDED
2
+#define SYMBOL_TABLE_H_INCLUDED
3
+
4
+#define SYMBOL_TABLE_SIZE 100
5
+
6
+enum Type {
7
+    TYPE_CONST,
8
+    TYPE_INT
9
+};
10
+
11
+struct SymbolItem {
12
+    enum Type type;
13
+    char* name;
14
+    int address;
15
+    int init;
16
+};
17
+
18
+typedef struct SymbolTable {
19
+    struct SymbolIte table[SYMBOL_TABLE_SIZE];
20
+    int index;
21
+} SymbolTable;
22
+
23
+void add_symbol(SymbolTable table, enum Type type, char* name);
24
+
25
+#endif /* !SYMBOL_TABLE_H_INCLUDED  */

+ 0
- 5
test.c View File

@@ -2,9 +2,4 @@ int main(){
2 2
     int x, y, z;
3 3
     const PL5_op, b, c;
4 4
     printf(x);
5
-    if (x == x && y != 5 + 6 || 1 + 2 == 2 + 1 && !(x == x)) {
6
-        printf(x);
7
-    } else {
8
-        x = 3 + 2;
9
-    }
10 5
 }

Loading…
Cancel
Save