|
@@ -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);
|