initial commit
Clement COLMERAUER

Clement COLMERAUER commited on 2024-10-20 09:16:49
Showing 25 changed files, with 1742 additions and 0 deletions.

... ...
@@ -0,0 +1,6 @@
1
+*.java
2
+*.tokens
3
+*.interp
4
+*.class
5
+*.mvap
6
+*.cbap
... ...
@@ -0,0 +1,37 @@
1
+rat a;
2
+int b;
3
+int c;
4
+
5
+a = 1/2;
6
+b = 0;
7
+repeter
8
+{
9
+	a = a + 1/2;
10
+	b++;
11
+	afficher(a);
12
+}
13
+jusque b == 10;
14
+
15
+a = 1/2;
16
+b = 0;
17
+c = 0;
18
+tant que b < 10
19
+{
20
+	tant que c < 10
21
+	{
22
+		a = a + 1/2;
23
+		c++;
24
+		afficher(a);
25
+	}
26
+	b++;
27
+}
28
+
29
+
30
+a = 1/2;
31
+b = 0;
32
+tant que b < 10
33
+{
34
+	a = a + 1/2;
35
+	b++;
36
+	afficher(a);
37
+}
... ...
@@ -0,0 +1,117 @@
1
+// Déclarations
2
+rat a;
3
+rat b;
4
+int c;
5
+int d;
6
+bool e;
7
+bool f;
8
+
9
+// Initialisations
10
+a = 1/2;
11
+b = 3/2;
12
+c = 1;
13
+d = 0;
14
+e = true;
15
+f = false;
16
+
17
+// Conditions sous la forme si..sinon..
18
+si a <> b
19
+{
20
+    afficher(1/1);
21
+}
22
+sinon
23
+{
24
+    afficher(0/1);
25
+}
26
+
27
+si 3/2 <> 1/2
28
+{
29
+    afficher(1/1);
30
+}
31
+sinon
32
+{
33
+    afficher(0/1);
34
+}
35
+
36
+
37
+si non(c == d)
38
+{
39
+    afficher(1/1);
40
+}
41
+sinon
42
+{
43
+    afficher(0/1);
44
+}
45
+
46
+si d < c
47
+{
48
+    afficher(1/1);
49
+}
50
+sinon
51
+{
52
+    afficher(0/1);
53
+}
54
+
55
+si c > d
56
+{
57
+    afficher(1/1);
58
+}
59
+sinon
60
+{
61
+    afficher(0/1);
62
+}
63
+
64
+si c >= d
65
+{
66
+    afficher(1/1);
67
+}
68
+sinon
69
+{
70
+    afficher(0/1);
71
+}
72
+
73
+si d <= c
74
+{
75
+    afficher(1/1);
76
+}
77
+sinon
78
+{
79
+    afficher(0/1);
80
+}
81
+
82
+si a <> b
83
+{
84
+    afficher(1/1);
85
+}
86
+sinon
87
+{
88
+    afficher(0/1);
89
+}
90
+
91
+si 0 <> 1
92
+{
93
+    afficher(1/1);
94
+}
95
+sinon
96
+{
97
+    afficher(0/1);
98
+}
99
+
100
+
101
+si non(e et f)
102
+{
103
+    afficher(1/1);
104
+}
105
+sinon
106
+{
107
+    afficher(0/1);
108
+}
109
+si e ou f
110
+{
111
+    afficher(1/1);
112
+}
113
+sinon
114
+{
115
+    afficher(0/1);
116
+}
117
+
... ...
@@ -0,0 +1,6 @@
1
+rat a;
2
+rat b;
3
+a = 1/2;
4
+b = 3/2;
5
+afficher(a+b);
6
+afficher(1/2+3/2);
... ...
@@ -0,0 +1,8 @@
1
+rat a;
2
+a = 3/2;
3
+afficher([a]);
4
+afficher([-a-]);
5
+afficher([+a+]);
6
+afficher([3/2]);
7
+afficher([-3/2-]);
8
+afficher([+3/2+]);
0 9
\ No newline at end of file
... ...
@@ -0,0 +1,160 @@
1
+rat a;
2
+rat b;
3
+int c;
4
+int d;
5
+bool e;
6
+bool f;
7
+
8
+a = 1/2;
9
+b = 3/2;
10
+c = 1;
11
+d = 0;
12
+e = true;
13
+f = false;
14
+
15
+si non(a == b)
16
+{
17
+	afficher(1);
18
+}
19
+sinon
20
+{
21
+	afficher(0);
22
+}
23
+
24
+si a < b
25
+{
26
+	afficher(1);
27
+}
28
+sinon
29
+{
30
+	afficher(0);
31
+}
32
+
33
+si a > b
34
+{
35
+	afficher(0);
36
+}
37
+sinon
38
+{
39
+	afficher(1);
40
+}
41
+
42
+si a >= (b - (a+a))
43
+{
44
+	afficher(1);
45
+}
46
+sinon
47
+{
48
+	afficher(0);
49
+}
50
+
51
+si a <= b - (a+a)
52
+{
53
+	afficher(1);
54
+}
55
+sinon
56
+{
57
+	afficher(0);
58
+}
59
+
60
+si a <> b
61
+{
62
+	afficher(1);
63
+}
64
+sinon
65
+{
66
+	afficher(0);
67
+}
68
+
69
+si 3/2 <> 1/2
70
+{
71
+	afficher(1);
72
+}
73
+sinon
74
+{
75
+	afficher(0);
76
+}
77
+
78
+3/2 <> 1/2 ? afficher(1) : afficher(0);
79
+
80
+si non(c == d)
81
+{
82
+	afficher(1);
83
+}
84
+sinon
85
+{
86
+	afficher(0);
87
+}
88
+
89
+si d < c
90
+{
91
+	afficher(1);
92
+}
93
+sinon
94
+{
95
+	afficher(0);
96
+}
97
+
98
+si c > d
99
+{
100
+	afficher(1);
101
+}
102
+sinon
103
+{
104
+	afficher(0);
105
+}
106
+
107
+si c >= d
108
+{
109
+	afficher(1);
110
+}
111
+sinon
112
+{
113
+	afficher(0);
114
+}
115
+
116
+si d <= c
117
+{
118
+	afficher(1);
119
+}
120
+sinon
121
+{
122
+	afficher(0);
123
+}
124
+
125
+si a <> b
126
+{
127
+	afficher(1);
128
+}
129
+sinon
130
+{
131
+	afficher(0);
132
+}
133
+
134
+si 0 <> 1
135
+{
136
+	afficher(1);
137
+}
138
+sinon
139
+{
140
+	afficher(0);
141
+}
142
+3 <> 1 ? afficher(1) : afficher(0);
143
+
144
+si non(e et f)
145
+{
146
+	afficher(1);
147
+}
148
+sinon
149
+{
150
+	afficher(0);
151
+}
152
+si e ou f
153
+{
154
+	afficher(1);
155
+}
156
+sinon
157
+{
158
+	afficher(0);
159
+}
160
+true ou false ? afficher(1) : afficher(0);
0 161
\ No newline at end of file
... ...
@@ -0,0 +1,11 @@
1
+rat a;
2
+int b;
3
+a = 1/2;
4
+b = 0;
5
+tant que b < 10
6
+{
7
+	a = a + 1/2;
8
+	b++;
9
+	// GWAK !
10
+	afficher(a);
11
+}
... ...
@@ -0,0 +1,9 @@
1
+rat a;
2
+rat b;
3
+rat c;
4
+b = 1/2;
5
+a = 3/9;
6
+c = a : b;
7
+afficher(c);
8
+afficher(a:b);
9
+afficher(1/2:3/9);
0 10
\ No newline at end of file
... ...
@@ -0,0 +1,16 @@
1
+rat a;
2
+int b;
3
+int c;
4
+a = 1/2;
5
+b = 0;
6
+c = 0;
7
+tant que b < 10
8
+{
9
+	tant que c < 10
10
+	{
11
+		a = a + 1/2;
12
+		c++;
13
+		afficher(a);
14
+	}
15
+	b++;
16
+}
... ...
@@ -0,0 +1,11 @@
1
+rat a;
2
+int b;
3
+a = 1/2;
4
+b = 0;
5
+repeter
6
+{
7
+	a = a + 1/2;
8
+	b++;
9
+	afficher(a);
10
+}
11
+jusque b == 10;
0 12
\ No newline at end of file
... ...
@@ -0,0 +1,23 @@
1
+si true et true
2
+{
3
+	si true et true
4
+	{
5
+		afficher(1/1);
6
+	}
7
+	sinon 
8
+	{
9
+		afficher(0/1);
10
+	}
11
+}
12
+sinon
13
+{
14
+	si true et true
15
+	{
16
+		afficher(1/1);
17
+	}
18
+	sinon 
19
+	{
20
+		afficher(0/1);
21
+	}
22
+	
23
+}
0 24
\ No newline at end of file
... ...
@@ -0,0 +1,8 @@
1
+int a;
2
+rat b;
3
+a = lire();
4
+b = lire();
5
+afficher(a);
6
+afficher(b);
7
+afficher(b:lire());
8
+afficher(b**lire());
0 9
\ No newline at end of file
... ...
@@ -0,0 +1,9 @@
1
+rat a;
2
+rat b;
3
+rat c;
4
+b = 1/2;
5
+a = 3/9;
6
+c = a * b;
7
+afficher(c);
8
+afficher(a*b);
9
+afficher(1/2*3/9);
0 10
\ No newline at end of file
... ...
@@ -0,0 +1,6 @@
1
+int a;
2
+int b;
3
+a = 21;
4
+b = 3;
5
+afficher(pgcd(a,b));
6
+afficher(pgcd(9,6));
... ...
@@ -0,0 +1 @@
1
+pgcd(64, 32);
... ...
@@ -0,0 +1,3 @@
1
+afficher(1/2%);
2
+afficher(1/3%);
3
+afficher(2/3%);
... ...
@@ -0,0 +1,6 @@
1
+int a;
2
+int b;
3
+a = 1;
4
+b = 2;
5
+afficher(ppcm(a,b));
6
+afficher(ppcm(1,2));
... ...
@@ -0,0 +1,9 @@
1
+rat a;
2
+int b;
3
+rat c;
4
+b = 2;
5
+a = 3/9;
6
+c = a ** b;
7
+afficher(c);
8
+afficher(a**b);
9
+afficher(3/9**2);
0 10
\ No newline at end of file
... ...
@@ -0,0 +1,4 @@
1
+rat a;
2
+a = 1024/64;
3
+afficher(sim(a));
4
+afficher(sim(1024/64));
0 5
\ No newline at end of file
... ...
@@ -0,0 +1,9 @@
1
+rat a;
2
+rat b;
3
+rat c;
4
+a = 1/2;
5
+b = 4/3;
6
+c = a-b;
7
+afficher(a-b);
8
+afficher(c);
9
+afficher(-c);
0 10
\ No newline at end of file
... ...
@@ -0,0 +1,9 @@
1
+rat a;
2
+rat b;
3
+rat c;
4
+b = -1/2;
5
+a = 3/2;
6
+c = a - b;
7
+afficher(c);
8
+afficher(a-b);
9
+afficher(1/2+3/2);
0 10
\ No newline at end of file
... ...
@@ -0,0 +1,10 @@
1
+rat a;
2
+int b;
3
+a = 1/2;
4
+b = 0;
5
+tant que b < 10
6
+{
7
+	a = a + 1/2;
8
+	b++;
9
+	afficher(a);
10
+}
... ...
@@ -0,0 +1,27 @@
1
+// Déclaration
2
+rat a;
3
+rat b;
4
+rat c;
5
+
6
+// Assignation des valeurs
7
+a = 1/2;
8
+b = 3/2; // Pour tester la simplification
9
+c = 1/3; // Pour tester les opérations avec des rationnels avec ≠ dénominateurs
10
+
11
+// Simplification
12
+afficher(a + b);
13
+afficher(a - b);
14
+
15
+// Dénominateurs ≠
16
+afficher(a + c);
17
+afficher(a - c);
18
+
19
+// Rationnels négatifs
20
+afficher(-a);
21
+
22
+b = 1/2;
23
+a = 3/9;
24
+c = a : b;
25
+afficher(c);
26
+afficher(a:b);
27
+afficher(1/2:3/9);
0 28
\ No newline at end of file
... ...
@@ -0,0 +1,15 @@
1
+rat a;
2
+rat b;
3
+
4
+a = 1024/64;
5
+b = 3/2;
6
+
7
+afficher(sim(a));
8
+afficher(sim(1024/64));
9
+
10
+afficher([b]);
11
+afficher([-b-]);
12
+afficher([+b+]);
13
+afficher([3/2]);
14
+afficher([-3/2-]);
15
+afficher([+3/2+]);
... ...
@@ -0,0 +1,1222 @@
1
+// Ugo CESANO         22007734
2
+// Clement COLMERAUER 22107959
3
+
4
+grammar Rationnel;
5
+
6
+
7
+@parser::header
8
+{
9
+    import java.util.HashMap;
10
+    import java.lang.StringBuilder;
11
+    import java.io.File;
12
+    import java.io.FileWriter;
13
+    import java.io.BufferedWriter;
14
+    import java.io.IOException;
15
+}
16
+
17
+
18
+@parser::members
19
+{
20
+    HashMap<String, Integer> registre   = new HashMap<String, Integer>();
21
+    Integer                  adresse    = 0;
22
+    int                      label      = 0;
23
+    int                      sp         = 0;
24
+
25
+
26
+    public String pgcd(String a, String b)
27
+    {
28
+        StringBuilder code = new StringBuilder();
29
+
30
+        // Initialisation
31
+        code.append(a);
32
+        code.append(b);
33
+        code.append("STOREG 1\nSTOREG 0\nPUSHG 1\nPUSHG 0\nSUP\nJUMPF ");
34
+        code.append(String.valueOf(label));
35
+        code.append("\nPUSHG 0\nPUSHG 1\nSTOREG 0\nSTOREG 1\nLABEL ");
36
+        code.append(String.valueOf(label));
37
+        code.append("\n");
38
+        label++;
39
+
40
+        // Boucle
41
+        code.append("LABEL ");
42
+        code.append(String.valueOf(label));
43
+        code.append("\nPUSHG 1\nPUSHI 0\nNEQ\nJUMPF ");
44
+        code.append(String.valueOf(label + 1));
45
+        code.append("\nPUSHG 0\nPUSHG 0\nPUSHG 1\nDIV\nPUSHG 1\nMUL\nSUB\nPUSHG 1\nSTOREG 0\nSTOREG 1\nJUMP ");
46
+        code.append(String.valueOf(label));
47
+        code.append("\nLABEL ");
48
+        code.append(String.valueOf(label + 1));
49
+        code.append("\n");
50
+
51
+        // Résultat
52
+        code.append("PUSHG 0\n");
53
+        label += 2;
54
+
55
+        return code.toString();
56
+    }
57
+
58
+
59
+    public String ppcm(String a, String b)
60
+    {
61
+        StringBuilder code = new StringBuilder();
62
+
63
+        // Calcul de la valeur absolue
64
+        code.append(a);
65
+        code.append(b);
66
+        code.append("MUL\nDUP\nPUSHI 0\nINF\nJUMPF ");
67
+        code.append(String.valueOf(label));
68
+        code.append("\nPUSHI -1\nMUL\nLABEL ");
69
+        code.append(String.valueOf(label));
70
+        code.append("\n");
71
+        label++;
72
+
73
+        // Calcul du PGCD
74
+        code.append(pgcd(a, b));
75
+
76
+        // Résultat
77
+        code.append("DIV\n");
78
+
79
+        return code.toString();
80
+    }
81
+}
82
+
83
+
84
+start: calcul EOF;
85
+
86
+
87
+calcul returns [ StringBuilder code ]
88
+@init
89
+{
90
+    $code = new StringBuilder();
91
+    // Espace mémoire utilisé pour les calculs.
92
+    $code.append("ALLOC 10\n\n");
93
+    adresse += 10;
94
+    sp      += 10;
95
+}
96
+@after
97
+{
98
+    File outputFile = new File("./a.mvap");
99
+    try(BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) { writer.append($code); }
100
+    catch(IOException e) { e.printStackTrace(); }
101
+}
102
+    : (declaration { $code.append($declaration.code.toString()); })*
103
+    (instruction { $code.append($instruction.code.toString()); })*
104
+    {
105
+        $code.append("\nFREE ");
106
+        $code.append(String.valueOf(sp));
107
+        $code.append("\n\nHALT\n");
108
+    }
109
+;
110
+
111
+
112
+finInstruction: ';';
113
+
114
+
115
+instruction returns [ StringBuilder code]
116
+    : a=expression finInstruction
117
+    {
118
+        $code = $a.code;
119
+    }
120
+    | b=assignation finInstruction
121
+    {
122
+        $code = $b.code;
123
+    }
124
+    | c=affichage finInstruction
125
+    {
126
+        $code = $c.code;
127
+    }
128
+    | d=structureConditionnelle
129
+    {
130
+        $code = $d.code;
131
+    }
132
+    | e=boucle
133
+    {
134
+        $code = $e.code;
135
+    }
136
+;
137
+
138
+
139
+declaration returns [ StringBuilder code ]
140
+    : TYPE IDENTIFIANT finInstruction
141
+    {
142
+        $code = new StringBuilder();
143
+        String id = $IDENTIFIANT.text;
144
+        boolean exists = registre.containsKey("i_" + id) ^
145
+                         registre.containsKey("r_" + id) ^
146
+                         registre.containsKey("b_" + id);
147
+
148
+        switch($TYPE.text)
149
+        {
150
+            case "int":
151
+                if(!exists)
152
+                {
153
+                    registre.put("i_" + id, adresse);
154
+                    $code.append("PUSHI 0\n");
155
+                    adresse++;
156
+                    sp++;
157
+                }
158
+                break;
159
+            case "rat":
160
+                if(!exists)
161
+                {
162
+                    registre.put("r_" + id, adresse);
163
+                    $code.append("PUSHI 1\nPUSHI 0\n");
164
+                    adresse += 2;
165
+                    sp += 2;
166
+                }
167
+                break;
168
+            case "bool":
169
+                if(!exists)
170
+                {
171
+                    registre.put("b_" + id, adresse);
172
+                    $code.append("PUSHI 0\n");
173
+                    adresse++;
174
+                    sp++;
175
+                }
176
+                break;
177
+        }
178
+    }
179
+;
180
+
181
+
182
+assignation returns [ StringBuilder code ]
183
+    : IDENTIFIANT '=' 'lire()'
184
+    {
185
+        $code = new StringBuilder();
186
+        String id = $IDENTIFIANT.text;
187
+
188
+        if(registre.containsKey("i_" + id))
189
+        {
190
+            $code.append("READ\n");
191
+            $code.append("STOREG ");
192
+            $code.append(String.valueOf(registre.get("i_" + id)));
193
+            $code.append("\n");
194
+        }
195
+        else if(registre.containsKey("r_" + id))
196
+        {
197
+            $code.append("READ\n");
198
+            $code.append("STOREG ");
199
+            $code.append(String.valueOf(registre.get("r_" + id) + 1));
200
+            $code.append("\nREAD\n");
201
+            $code.append("STOREG ");
202
+            $code.append(String.valueOf(registre.get("r_" + id)));
203
+            $code.append("\n");
204
+        }
205
+        else if(registre.containsKey("b_" + id))
206
+        {
207
+            $code.append("READ\n");
208
+            $code.append("STOREG ");
209
+            $code.append(String.valueOf(registre.get("b_" + id)));
210
+            $code.append("\n");
211
+        }
212
+    }
213
+    | IDENTIFIANT '=' a=expression
214
+    {
215
+        $code = new StringBuilder();
216
+        String id = $IDENTIFIANT.text;
217
+
218
+        $code.append($a.code.toString());
219
+        if(registre.containsKey("i_" + id))
220
+        {
221
+            $code.append("STOREG ");
222
+            $code.append(String.valueOf(registre.get("i_" + id)));
223
+            $code.append("\n");
224
+            sp--;
225
+        }
226
+        else if(registre.containsKey("r_" + id))
227
+        {
228
+            $code.append("STOREG ");
229
+            $code.append(String.valueOf(registre.get("r_" + id) + 1));
230
+            $code.append("\nSTOREG ");
231
+            $code.append(String.valueOf(registre.get("r_" + id)));
232
+            $code.append("\n");
233
+            sp -= 2;
234
+        }
235
+        else if(registre.containsKey("b_" + id))
236
+        {
237
+            $code.append("STOREG ");
238
+            $code.append(String.valueOf(registre.get("b_" + id)));
239
+            $code.append("\n");
240
+            sp--;
241
+        }
242
+    }
243
+;
244
+
245
+
246
+expression returns [ StringBuilder code ]
247
+    : a=expressionArithmetiqueEntiere
248
+    {
249
+        $code = $a.code;
250
+    }
251
+    | c=expressionArithmetiqueRationnelle
252
+    {
253
+        $code = $c.code;
254
+    }
255
+    | d=expressionArithmetiqueBooleene
256
+    {
257
+        $code = $d.code;
258
+    }
259
+    | e=arrondi
260
+    {
261
+        $code = $e.code;
262
+    }
263
+    | b=affichage
264
+    {
265
+        $code = $b.code;
266
+    }
267
+    | f=expressionArithmetiqueBooleene'?'g=expression':'h=expression
268
+    {
269
+        $code = $f.code;
270
+        $code.append("JUMPF ");
271
+        $code.append(String.valueOf(label + 1));
272
+        $code.append("\nLABEL ");
273
+        $code.append(String.valueOf(label));
274
+        $code.append("\n");
275
+        $code.append($g.code.toString());
276
+        $code.append("JUMP ");
277
+        $code.append(String.valueOf(label + 2));
278
+        $code.append("\nLABEL ");
279
+        $code.append(String.valueOf(label + 1));
280
+        $code.append("\n");
281
+        $code.append($h.code.toString());
282
+        $code.append("LABEL ");
283
+        $code.append(String.valueOf(label + 2));
284
+        $code.append("\n");
285
+        label += 3;
286
+        sp--;
287
+    }
288
+;
289
+
290
+
291
+expressionArithmetiqueEntiere returns [ StringBuilder code ]
292
+    : '('a=expressionArithmetiqueEntiere')'
293
+    {
294
+        $code = $a.code;
295
+    }
296
+    | 'num('IDENTIFIANT')'
297
+    {
298
+        $code = new StringBuilder();
299
+        String id = $IDENTIFIANT.text;
300
+
301
+        if(registre.containsKey("r_" + id))
302
+        {
303
+            $code.append("PUSHG ");
304
+            $code.append(String.valueOf(registre.get("r_" + id) + 1));
305
+            $code.append("\n");
306
+            sp++;
307
+        }
308
+    }
309
+    | 'num('a=expressionArithmetiqueEntiere')'
310
+    {
311
+        $code = $a.code;
312
+        sp++;
313
+    }
314
+    | 'num('a=expressionArithmetiqueEntiere'/'b=expressionArithmetiqueEntiere')'
315
+    {
316
+        $code = $a.code;
317
+        sp++;
318
+    }
319
+    | 'denum('IDENTIFIANT')'
320
+    {
321
+        $code = new StringBuilder();
322
+        String id = $IDENTIFIANT.text;
323
+
324
+        if(registre.containsKey("r_" + id))
325
+        {
326
+            $code.append("PUSHG ");
327
+            $code.append(String.valueOf(registre.get("r_" + id)));
328
+            $code.append("\n");
329
+            sp++;
330
+        }
331
+    }
332
+    | 'denum('a=expressionArithmetiqueEntiere')'
333
+    {
334
+        $code = new StringBuilder("PUSHI 1\n");
335
+        sp++;
336
+    }
337
+    | 'denum('a=expressionArithmetiqueEntiere'/'b=expressionArithmetiqueEntiere')'
338
+    {
339
+        $code = $b.code;
340
+        sp++;
341
+    }
342
+    | 'pgcd('a=expressionArithmetiqueEntiere','b=expressionArithmetiqueEntiere')'
343
+    {
344
+        $code = new StringBuilder(pgcd($a.code.toString(), $b.code.toString()));
345
+    }
346
+    | 'ppcm('a=expressionArithmetiqueEntiere','b=expressionArithmetiqueEntiere')'
347
+    {
348
+        $code = new StringBuilder(ppcm($a.code.toString(), $b.code.toString()));
349
+    }
350
+    | 'lire()'
351
+    {
352
+        $code = new StringBuilder("READ\n");
353
+        sp++;
354
+    }
355
+    | IDENTIFIANT'++'
356
+    {
357
+        $code = new StringBuilder();
358
+        String id = $IDENTIFIANT.text;
359
+
360
+        if(registre.containsKey("i_" + id))
361
+        {
362
+            $code.append("PUSHG ");
363
+            $code.append(String.valueOf(registre.get("i_" + id)));
364
+            $code.append("\nPUSHI 1\nADD\nSTOREG ");
365
+            $code.append(String.valueOf(registre.get("i_" + id)));
366
+            $code.append("\n");
367
+        }
368
+    }
369
+    | IDENTIFIANT'--'
370
+    {
371
+        $code = new StringBuilder();
372
+        String id = $IDENTIFIANT.text;
373
+
374
+        if(registre.containsKey("i_" + id))
375
+        {
376
+            $code.append("PUSHG ");
377
+            $code.append(String.valueOf(registre.get("i_" + id)));
378
+            $code.append("\nPUSHI 1\nSUB\nSTOREG ");
379
+            $code.append(String.valueOf(registre.get("i_" + id)));
380
+            $code.append("\n");
381
+        }
382
+    }
383
+    | IDENTIFIANT
384
+    {
385
+        $code = new StringBuilder();
386
+        String id = $IDENTIFIANT.text;
387
+
388
+        if(registre.containsKey("i_" + id))
389
+        {
390
+            $code.append("PUSHG ");
391
+            $code.append(String.valueOf(registre.get("i_" + id)));
392
+            $code.append("\n");
393
+        }
394
+    }
395
+    | ENTIER
396
+    {
397
+        $code = new StringBuilder();
398
+        $code.append("PUSHI ");
399
+        $code.append(String.valueOf(Integer.parseInt($ENTIER.text)));
400
+        $code.append("\n");
401
+        sp++;
402
+    }
403
+;
404
+
405
+
406
+expressionArithmetiqueRationnelle returns [ StringBuilder code ]
407
+    : '('c=expressionArithmetiqueRationnelle')'
408
+    {
409
+        $code = $c.code;
410
+    }
411
+    | a=expressionArithmetiqueEntiere '/' b=expressionArithmetiqueEntiere
412
+    {
413
+        $code = new StringBuilder();
414
+        $code.append($b.code.toString());
415
+        $code.append($a.code.toString());
416
+    }
417
+    | '-'c=expressionArithmetiqueRationnelle
418
+    {
419
+        $code = $c.code;
420
+        $code.append("STOREG 3\nPUSHI 0\nPUSHG 3\nSUB\n");
421
+    }
422
+    | c=expressionArithmetiqueRationnelle'*'d=expressionArithmetiqueRationnelle
423
+    {
424
+        $code = $c.code;
425
+        $code.append($d.code.toString());
426
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\nPUSHG 2\nPUSHG 4\nMUL\nPUSHG 3\nPUSHG 5\nMUL\n");
427
+        sp -= 2;
428
+    }
429
+    | c=expressionArithmetiqueRationnelle':'d=expressionArithmetiqueRationnelle
430
+    {
431
+        $code = $c.code;
432
+        $code.append($d.code.toString());
433
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\nPUSHG 3\nPUSHG 4\nMUL\nPUSHG 2\nPUSHG 5\nMUL\nSTOREG 5\nSTOREG 4\nPUSHG 5\nPUSHG 4\n");
434
+        sp -= 2;
435
+    }
436
+    | c=expressionArithmetiqueRationnelle'+'d=expressionArithmetiqueRationnelle
437
+    {
438
+        $code = $c.code;
439
+        $code.append($d.code.toString());
440
+        $code.append("STOREG 3\nSTOREG 2\nSTOREG 5\nSTOREG 4\n");
441
+        $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
442
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nADD\n");
443
+        sp -= 2;
444
+    }
445
+    | c=expressionArithmetiqueRationnelle'-'d=expressionArithmetiqueRationnelle
446
+    {
447
+        $code = $c.code;
448
+        $code.append($d.code.toString());
449
+        $code.append("STOREG 3\nSTOREG 2\nSTOREG 5\nSTOREG 4\n");
450
+        $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
451
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nPUSHG 3\nPUSHI 0\nINF\nJUMPF ");
452
+        $code.append(String.valueOf(label));
453
+        $code.append("\nPUSHI -1\nMUL\nADD\nJUMP ");
454
+        $code.append(String.valueOf(label + 1));
455
+        $code.append("\nLABEL ");
456
+        $code.append(String.valueOf(label));
457
+        $code.append("\nSUB\nLABEL ");
458
+        $code.append(String.valueOf(label + 1));
459
+        $code.append("\n");
460
+        label += 2;
461
+        sp -= 2;
462
+    }
463
+    | IDENTIFIANT'**'a=expressionArithmetiqueEntiere
464
+    {
465
+        $code = new StringBuilder();
466
+        String id = $IDENTIFIANT.text;
467
+
468
+        if(registre.containsKey("r_" + id))
469
+        {
470
+            $code.append("PUSHG ");
471
+            $code.append(String.valueOf(registre.get("r_" + id)));
472
+            $code.append("\nPUSHG ");
473
+            $code.append(String.valueOf(registre.get("r_" + id) + 1));
474
+            $code.append("\n");
475
+
476
+            $code.append($a.code.toString());
477
+            $code.append("STOREG 6\nSTOREG 3\nSTOREG 2\nPUSHG 3\nSTOREG 5\nPUSHG 2\nSTOREG 4\nPUSHI 1\nSTOREG 7\nLABEL ");
478
+            $code.append(String.valueOf(label));
479
+            $code.append("\nPUSHG 7\nPUSHG 6\nINF\nJUMPF ");
480
+            $code.append(String.valueOf(label + 1));
481
+            $code.append("\nPUSHG 3\nPUSHG 5\nMUL\nSTOREG 3\nPUSHG 2\nPUSHG 4\nMUL\nSTOREG 2\nPUSHG 7\nPUSHI 1\nADD\nSTOREG 7\nJUMP ");
482
+            $code.append(String.valueOf(label));
483
+            $code.append("\nLABEL ");
484
+            $code.append(String.valueOf(label + 1));
485
+            $code.append("\nPUSHG 2\nPUSHG 3\n");
486
+            label += 2;
487
+            sp++;
488
+        }
489
+    }
490
+    | c=expressionArithmetiqueRationnelle'**'a=expressionArithmetiqueEntiere
491
+    {
492
+        $code = $c.code;
493
+        $code.append($a.code.toString());
494
+        $code.append("STOREG 6\nSTOREG 3\nSTOREG 2\nPUSHG 3\nSTOREG 5\nPUSHG 2\nSTOREG 4\nPUSHI 1\nSTOREG 7\nLABEL ");
495
+        $code.append(String.valueOf(label));
496
+        $code.append("\nPUSHG 7\nPUSHG 6\nINF\nJUMPF ");
497
+        $code.append(String.valueOf(label + 1));
498
+        $code.append("\nPUSHG 3\nPUSHG 5\nMUL\nSTOREG 3\nPUSHG 2\nPUSHG 4\nMUL\nSTOREG 2\nPUSHG 7\nPUSHI 1\nADD\nSTOREG 7\nJUMP ");
499
+        $code.append(String.valueOf(label));
500
+        $code.append("\nLABEL ");
501
+        $code.append(String.valueOf(label + 1));
502
+        $code.append("\nPUSHG 2\nPUSHG 3\n");
503
+        label += 2;
504
+        sp--;
505
+    }
506
+    | 'sim('c=expressionArithmetiqueRationnelle')'
507
+    {
508
+        $code = $c.code;
509
+        $code.append("STOREG 3\nSTOREG 2\nPUSHG 2\nPUSHG 3\nSTOREG 5\nSTOREG 4\n");
510
+        $code.append(pgcd("PUSHG 2\n","PUSHG 3\n"));
511
+        $code.append("STOREG 6\nLABEL ");
512
+        $code.append(String.valueOf(label));
513
+        $code.append("\nPUSHG 6\nPUSHI 1\nNEQ\nJUMPF ");
514
+        $code.append(String.valueOf(label + 1));
515
+        $code.append("\n");
516
+        $code.append(pgcd("PUSHG 2\n","PUSHG 3\n"));
517
+        $code.append("STOREG 6\nPUSHG 3\nPUSHG 6\nDIV\nSTOREG 3\nPUSHG 2\nPUSHG 6\nDIV\nSTOREG 2\nLABEL ");
518
+        $code.append(String.valueOf(label + 1));
519
+        $code.append("\nPUSHG 2\nPUSHG 3\n");
520
+        label += 2;
521
+    }
522
+    | IDENTIFIANT
523
+    {
524
+        $code = new StringBuilder();
525
+        String id = $IDENTIFIANT.text;
526
+
527
+        if(registre.containsKey("r_" + id))
528
+        {
529
+            $code.append("PUSHG ");
530
+            $code.append(String.valueOf(registre.get("r_" + id)));
531
+            $code.append("\nPUSHG ");
532
+            $code.append(String.valueOf(registre.get("r_" + id) + 1));
533
+            $code.append("\n");
534
+        }
535
+    }
536
+    | 'lire()'
537
+    {
538
+        $code = new StringBuilder("READ\nSTOREG 3\nREAD\nSTOREG 2\nPUSHG 2\nPUSHG 3\n");
539
+        sp += 2;
540
+    }
541
+;
542
+
543
+
544
+expressionArithmetiqueBooleene returns [ StringBuilder code ]
545
+    : '('a=expressionArithmetiqueBooleene')'
546
+    {
547
+        $code = $a.code;
548
+    }
549
+    | 'non'a=expressionArithmetiqueBooleene
550
+    {
551
+        $code = $a.code;
552
+        $code.append("PUSHI 1\nINF\n");
553
+    }
554
+    | a=expressionArithmetiqueBooleene'et'b=expressionArithmetiqueBooleene
555
+    {
556
+        $code = new StringBuilder($a.code.toString());
557
+        $code.append("JUMPF ");
558
+        $code.append(String.valueOf(label));
559
+        $code.append("\n");
560
+        $code.append($a.code.toString());
561
+        $code.append($b.code.toString());
562
+        $code.append("MUL\nJUMP ");
563
+        $code.append(String.valueOf(label + 1));
564
+        $code.append("\nLABEL ");
565
+        $code.append(String.valueOf(label));
566
+        $code.append("\nPUSHI 0\nLABEL ");
567
+        $code.append(String.valueOf(label + 1));
568
+        $code.append("\n");
569
+        label += 2;
570
+        sp--;
571
+    }
572
+    | a=expressionArithmetiqueBooleene'ou'b=expressionArithmetiqueBooleene
573
+    {
574
+        $code = new StringBuilder($a.code.toString());
575
+        $code.append("PUSHI 1\nINF\nJUMPF ");
576
+        $code.append(String.valueOf(label));
577
+        $code.append("\n");
578
+        $code.append($a.code.toString());
579
+        $code.append($b.code.toString());
580
+        $code.append("ADD\nJUMP ");
581
+        $code.append(String.valueOf(label + 1));
582
+        $code.append("\nLABEL ");
583
+        $code.append(String.valueOf(label));
584
+        $code.append("\nPUSHI 1\nLABEL ");
585
+        $code.append(String.valueOf(label + 1));
586
+        $code.append("\n");
587
+        label += 2;
588
+        sp--;
589
+    }
590
+    | e=comparaisonVariable
591
+    {
592
+        $code = $e.code;
593
+    }
594
+    | c=comparaisonRationnel
595
+    {
596
+        $code = $c.code;
597
+    }
598
+    | d=comparaisonEntier
599
+    {
600
+        $code = $d.code;
601
+    }
602
+    | BOOL
603
+    {
604
+        $code = new StringBuilder();
605
+        $code.append("PUSHI ");
606
+        $code.append(Boolean.parseBoolean($BOOL.text) ? "1\n" : "0\n");
607
+        sp++;
608
+    }
609
+    | IDENTIFIANT
610
+    {
611
+        $code = new StringBuilder();
612
+        String id = $IDENTIFIANT.text;
613
+
614
+        if(registre.containsKey("b_" + id))
615
+        {
616
+            $code.append("PUSHG ");
617
+            $code.append(String.valueOf(registre.get("b_" + id)));
618
+            $code.append("\n");
619
+        }
620
+    }
621
+;
622
+
623
+
624
+comparaisonVariable returns [ StringBuilder code ]
625
+    : a=IDENTIFIANT'<'b=IDENTIFIANT
626
+    {
627
+        $code = new StringBuilder();
628
+
629
+        if(registre.containsKey("r_" + $a.text) && registre.containsKey("r_" + $b.text))
630
+        {
631
+            int n = registre.get("r_" + $a.text);
632
+
633
+            $code.append("PUSHG ");
634
+            $code.append(String.valueOf(n));
635
+            $code.append("\nPUSHG ");
636
+            $code.append(String.valueOf(n + 1));
637
+            $code.append("\n");
638
+
639
+            n = registre.get("r_" + $b.text);
640
+
641
+            $code.append("PUSHG ");
642
+            $code.append(String.valueOf(n));
643
+            $code.append("\nPUSHG ");
644
+            $code.append(String.valueOf(n + 1));
645
+            $code.append("\n");
646
+
647
+            $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
648
+            $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
649
+            $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nINF\n");
650
+
651
+            sp++;
652
+        }
653
+
654
+        if(registre.containsKey("i_" + $a.text) && registre.containsKey("i_" + $b.text))
655
+        {
656
+            int n = registre.get("i_" + $a.text);
657
+
658
+            $code.append("PUSHG ");
659
+            $code.append(String.valueOf(n));
660
+            $code.append("\n");
661
+
662
+            n = registre.get("i_" + $b.text);
663
+
664
+            $code.append("PUSHG ");
665
+            $code.append(String.valueOf(n));
666
+            $code.append("\nINF\n");
667
+
668
+            sp++;
669
+        }
670
+    }
671
+    | a=IDENTIFIANT'>'b=IDENTIFIANT
672
+    {
673
+        $code = new StringBuilder();
674
+
675
+        if(registre.containsKey("r_" + $a.text) && registre.containsKey("r_" + $b.text))
676
+        {
677
+            int n = registre.get("r_" + $a.text);
678
+
679
+            $code.append("PUSHG ");
680
+            $code.append(String.valueOf(n));
681
+            $code.append("\nPUSHG ");
682
+            $code.append(String.valueOf(n + 1));
683
+            $code.append("\n");
684
+
685
+            n = registre.get("r_" + $b.text);
686
+
687
+            $code.append("PUSHG ");
688
+            $code.append(String.valueOf(n));
689
+            $code.append("\nPUSHG ");
690
+            $code.append(String.valueOf(n + 1));
691
+            $code.append("\n");
692
+
693
+            $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
694
+            $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
695
+            $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nSUP\n");
696
+
697
+            sp++;
698
+        }
699
+
700
+        if(registre.containsKey("i_" + $a.text) && registre.containsKey("i_" + $b.text))
701
+        {
702
+            int n = registre.get("i_" + $a.text);
703
+
704
+            $code.append("PUSHG ");
705
+            $code.append(String.valueOf(n));
706
+            $code.append("\n");
707
+
708
+            n = registre.get("i_" + $b.text);
709
+
710
+            $code.append("PUSHG ");
711
+            $code.append(String.valueOf(n));
712
+            $code.append("\nSUP\n");
713
+
714
+            sp++;
715
+        }
716
+    }
717
+    | a=IDENTIFIANT'=='b=IDENTIFIANT
718
+    {
719
+        $code = new StringBuilder();
720
+
721
+        if(registre.containsKey("r_" + $a.text) && registre.containsKey("r_" + $b.text))
722
+        {
723
+            int n = registre.get("r_" + $a.text);
724
+
725
+            $code.append("PUSHG ");
726
+            $code.append(String.valueOf(n));
727
+            $code.append("\nPUSHG ");
728
+            $code.append(String.valueOf(n + 1));
729
+            $code.append("\n");
730
+
731
+            n = registre.get("r_" + $b.text);
732
+
733
+            $code.append("PUSHG ");
734
+            $code.append(String.valueOf(n));
735
+            $code.append("\nPUSHG ");
736
+            $code.append(String.valueOf(n + 1));
737
+            $code.append("\n");
738
+
739
+            $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
740
+            $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
741
+            $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nEQUAL\n");
742
+
743
+            sp++;
744
+        }
745
+
746
+        if(registre.containsKey("i_" + $a.text) && registre.containsKey("i_" + $b.text))
747
+        {
748
+            int n = registre.get("i_" + $a.text);
749
+
750
+            $code.append("PUSHG ");
751
+            $code.append(String.valueOf(n));
752
+            $code.append("\n");
753
+
754
+            n = registre.get("i_" + $b.text);
755
+
756
+            $code.append("PUSHG ");
757
+            $code.append(String.valueOf(n));
758
+            $code.append("\nEQUAL\n");
759
+
760
+            sp++;
761
+        }
762
+    }
763
+    | a=IDENTIFIANT'<>'b=IDENTIFIANT
764
+    {
765
+        $code = new StringBuilder();
766
+
767
+        if(registre.containsKey("r_" + $a.text) && registre.containsKey("r_" + $b.text))
768
+        {
769
+            int n = registre.get("r_" + $a.text);
770
+
771
+            $code.append("PUSHG ");
772
+            $code.append(String.valueOf(n));
773
+            $code.append("\nPUSHG ");
774
+            $code.append(String.valueOf(n + 1));
775
+            $code.append("\n");
776
+
777
+            n = registre.get("r_" + $b.text);
778
+
779
+            $code.append("PUSHG ");
780
+            $code.append(String.valueOf(n));
781
+            $code.append("\nPUSHG ");
782
+            $code.append(String.valueOf(n + 1));
783
+            $code.append("\n");
784
+
785
+            $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
786
+            $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
787
+            $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nNEQ\n");
788
+
789
+            sp++;
790
+        }
791
+
792
+        if(registre.containsKey("i_" + $a.text) && registre.containsKey("i_" + $b.text))
793
+        {
794
+            int n = registre.get("i_" + $a.text);
795
+
796
+            $code.append("PUSHG ");
797
+            $code.append(String.valueOf(n));
798
+            $code.append("\n");
799
+
800
+            n = registre.get("i_" + $b.text);
801
+
802
+            $code.append("PUSHG ");
803
+            $code.append(String.valueOf(n));
804
+            $code.append("\nNEQ\n");
805
+
806
+            sp++;
807
+        }
808
+    }
809
+    | a=IDENTIFIANT'>='b=IDENTIFIANT
810
+    {
811
+        $code = new StringBuilder();
812
+
813
+        if(registre.containsKey("r_" + $a.text) && registre.containsKey("r_" + $b.text))
814
+        {
815
+            int n = registre.get("r_" + $a.text);
816
+
817
+            $code.append("PUSHG ");
818
+            $code.append(String.valueOf(n));
819
+            $code.append("\nPUSHG ");
820
+            $code.append(String.valueOf(n + 1));
821
+            $code.append("\n");
822
+
823
+            n = registre.get("r_" + $b.text);
824
+
825
+            $code.append("PUSHG ");
826
+            $code.append(String.valueOf(n));
827
+            $code.append("\nPUSHG ");
828
+            $code.append(String.valueOf(n + 1));
829
+            $code.append("\n");
830
+
831
+            $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
832
+            $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
833
+            $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nSUPEQ\n");
834
+
835
+            sp++;
836
+        }
837
+
838
+        if(registre.containsKey("i_" + $a.text) && registre.containsKey("i_" + $b.text))
839
+        {
840
+            int n = registre.get("i_" + $a.text);
841
+
842
+            $code.append("PUSHG ");
843
+            $code.append(String.valueOf(n));
844
+            $code.append("\n");
845
+
846
+            n = registre.get("i_" + $b.text);
847
+
848
+            $code.append("PUSHG ");
849
+            $code.append(String.valueOf(n));
850
+            $code.append("\nSUPEQ\n");
851
+
852
+            sp++;
853
+        }
854
+    }
855
+    | a=IDENTIFIANT'<='b=IDENTIFIANT
856
+    {
857
+        $code = new StringBuilder();
858
+
859
+        if(registre.containsKey("r_" + $a.text) && registre.containsKey("r_" + $b.text))
860
+        {
861
+            int n = registre.get("r_" + $a.text);
862
+
863
+            $code.append("PUSHG ");
864
+            $code.append(String.valueOf(n));
865
+            $code.append("\nPUSHG ");
866
+            $code.append(String.valueOf(n + 1));
867
+            $code.append("\n");
868
+
869
+            n = registre.get("r_" + $b.text);
870
+
871
+            $code.append("PUSHG ");
872
+            $code.append(String.valueOf(n));
873
+            $code.append("\nPUSHG ");
874
+            $code.append(String.valueOf(n + 1));
875
+            $code.append("\n");
876
+
877
+            $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
878
+            $code.append(ppcm("PUSHG 2\n","PUSHG 4\n"));
879
+            $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 2\nDIV\nPUSHG 6\nPUSHG 5\nMUL\nPUSHG 4\nDIV\nINFEQ\n");
880
+
881
+            sp++;
882
+        }
883
+
884
+        if(registre.containsKey("i_" + $a.text) && registre.containsKey("i_" + $b.text))
885
+        {
886
+            int n = registre.get("i_" + $a.text);
887
+
888
+            $code.append("PUSHG ");
889
+            $code.append(String.valueOf(n));
890
+            $code.append("\n");
891
+
892
+            n = registre.get("i_" + $b.text);
893
+
894
+            $code.append("PUSHG ");
895
+            $code.append(String.valueOf(n));
896
+            $code.append("\nINFEQ\n");
897
+
898
+            sp++;
899
+        }
900
+    }
901
+;
902
+
903
+
904
+comparaisonRationnel returns [ StringBuilder code ]
905
+    : e=expressionArithmetiqueRationnelle'>'f=expressionArithmetiqueRationnelle
906
+    {
907
+        $code = $e.code;
908
+        $code.append($f.code.toString());
909
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
910
+        $code.append(ppcm("PUSHG 3\n","PUSHG 5\n"));
911
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 6\nPUSHG 5\nMUL\nSUP\n");
912
+        sp -= 3;
913
+    }
914
+    | e=expressionArithmetiqueRationnelle'<'f=expressionArithmetiqueRationnelle
915
+    {
916
+        $code = $e.code;
917
+        $code.append($f.code.toString());
918
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
919
+        $code.append(ppcm("PUSHG 3\n","PUSHG 5\n"));
920
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 6\nPUSHG 5\nMUL\nINF\n");
921
+        sp -= 3;
922
+    }
923
+    | e=expressionArithmetiqueRationnelle'=='f=expressionArithmetiqueRationnelle
924
+    {
925
+        $code = $e.code;
926
+        $code.append($f.code.toString());
927
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
928
+        $code.append(ppcm("PUSHG 3\n","PUSHG 5\n"));
929
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 6\nPUSHG 5\nMUL\nEQUAL\n");
930
+        sp -= 3;
931
+    }
932
+    | e=expressionArithmetiqueRationnelle'<>'f=expressionArithmetiqueRationnelle
933
+    {
934
+        $code = $e.code;
935
+        $code.append($f.code.toString());
936
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
937
+        $code.append(ppcm("PUSHG 3\n","PUSHG 5\n"));
938
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 6\nPUSHG 5\nMUL\nNEQ\n");
939
+        sp -= 3;
940
+    }
941
+    | e=expressionArithmetiqueRationnelle'>='f=expressionArithmetiqueRationnelle
942
+    {
943
+        $code = $e.code;
944
+        $code.append($f.code.toString());
945
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
946
+        $code.append(ppcm("PUSHG 3\n","PUSHG 5\n"));
947
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 6\nPUSHG 5\nMUL\nSUPEQ\n");
948
+        sp -= 3;
949
+    }
950
+    | e=expressionArithmetiqueRationnelle'<='f=expressionArithmetiqueRationnelle
951
+    {
952
+        $code = $e.code;
953
+        $code.append($f.code.toString());
954
+        $code.append("STOREG 5\nSTOREG 4\nSTOREG 3\nSTOREG 2\n");
955
+        $code.append(ppcm("PUSHG 3\n","PUSHG 5\n"));
956
+        $code.append("STOREG 6\nPUSHG 6\nPUSHG 3\nMUL\nPUSHG 6\nPUSHG 5\nMUL\nINFEQ\n");
957
+        sp -= 3;
958
+    }
959
+;
960
+
961
+
962
+comparaisonEntier returns [ StringBuilder code ]
963
+    : c=expressionArithmetiqueEntiere'>'d=expressionArithmetiqueEntiere
964
+    {
965
+        $code = $c.code;
966
+        $code.append($d.code.toString());
967
+        $code.append("SUP\n");
968
+        sp--;
969
+    }
970
+    | c=expressionArithmetiqueEntiere'<'d=expressionArithmetiqueEntiere
971
+    {
972
+        $code = $c.code;
973
+        $code.append($d.code.toString());
974
+        $code.append("INF\n");
975
+        sp--;
976
+    }
977
+    | c=expressionArithmetiqueEntiere'=='d=expressionArithmetiqueEntiere
978
+    {
979
+        $code = $c.code;
980
+        $code.append($d.code.toString());
981
+        $code.append("EQUAL\n");
982
+        sp--;
983
+    }
984
+    | c=expressionArithmetiqueEntiere'<>'d=expressionArithmetiqueEntiere
985
+    {
986
+        $code = $c.code;
987
+        $code.append($d.code.toString());
988
+        $code.append("NEQ\n");
989
+        sp--;
990
+    }
991
+    | c=expressionArithmetiqueEntiere'>='d=expressionArithmetiqueEntiere
992
+    {
993
+        $code = $c.code;
994
+        $code.append($d.code.toString());
995
+        $code.append("SUPEQ\n");
996
+        sp--;
997
+    }
998
+    | c=expressionArithmetiqueEntiere'<='d=expressionArithmetiqueEntiere
999
+    {
1000
+        $code = $c.code;
1001
+        $code.append($d.code.toString());
1002
+        $code.append("INFEQ\n");
1003
+        sp--;
1004
+    }
1005
+;
1006
+
1007
+
1008
+arrondi returns [ StringBuilder code ]
1009
+    : '['c=expressionArithmetiqueRationnelle']'
1010
+    {
1011
+        $code = $c.code;
1012
+        $code.append("STOREG 3\nSTOREG 2\nPUSHG 3\nITOF\nPUSHG 2\nITOF\nFDIV\nPUSHG 3\nPUSHG 2\nDIV\nITOF\nFSUB\nPUSHF 0.5\nFINF\nJUMPF ");
1013
+        $code.append(String.valueOf(label));
1014
+        $code.append("\nPUSHG 3\nPUSHG 2\nDIV\nJUMP ");
1015
+        $code.append(String.valueOf(label + 1));
1016
+        $code.append("\nLABEL ");
1017
+        $code.append(String.valueOf(label));
1018
+        $code.append("\nPUSHG 3\nPUSHG 2\nDIV\nPUSHI 1\nADD\nLABEL ");
1019
+        $code.append(String.valueOf(label + 1));
1020
+        $code.append("\n");
1021
+        label += 2;
1022
+        sp--;
1023
+    }
1024
+    | '[-'c=expressionArithmetiqueRationnelle'-]'
1025
+    {
1026
+        $code = $c.code;
1027
+        $code.append("STOREG 3\nSTOREG 2\nPUSHG 3\nPUSHG 2\nDIV\n");
1028
+        sp--;
1029
+    }
1030
+    | '[+'c=expressionArithmetiqueRationnelle'+]'
1031
+    {
1032
+        $code = $c.code;
1033
+        $code.append("STOREG 3\nSTOREG 2\nPUSHG 3\nITOF\nPUSHG 2\nITOF\nFDIV\nPUSHG 3\nPUSHG 2\nDIV\nITOF\nFSUB\nPUSHF 0.0\nFEQUAL\nJUMPF ");
1034
+        $code.append(String.valueOf(label));
1035
+        $code.append("\nPUSHG 3\nPUSHG 2\nDIV\nJUMP ");
1036
+        $code.append(String.valueOf(label + 1));
1037
+        $code.append("\nLABEL ");
1038
+        $code.append(String.valueOf(label));
1039
+        $code.append("\nPUSHG 3\nPUSHG 2\nDIV\nPUSHI 1\nADD\nLABEL ");
1040
+        $code.append(String.valueOf(label + 1));
1041
+        $code.append("\n");
1042
+        label += 2;
1043
+        sp--;
1044
+    }
1045
+    | c=expressionArithmetiqueRationnelle'%'
1046
+    {
1047
+        $code = $c.code;
1048
+        $code.append("STOREG 3\nSTOREG 2\nPUSHG 3\nITOF\nPUSHG 2\nITOF\nFDIV\nPUSHF 100.0\nFMUL\nPUSHG 3\nITOF\nPUSHG 2\nITOF\nFDIV\nPUSHF 100.0\nFMUL\nPUSHG 3\nITOF\nPUSHG 2\nITOF\nFDIV\nPUSHF 100.0\nFMUL\nFTOI\nITOF\nFSUB\n");
1049
+        $code.append("PUSHF 0.5\nFSUP\nJUMPF ");
1050
+        $code.append(String.valueOf(label) + "\n");
1051
+        $code.append("FTOI\nPUSHI 1\nADD\nJUMP ");
1052
+        $code.append(String.valueOf(label + 1) + "\n");
1053
+        $code.append("LABEL ");
1054
+        $code.append(String.valueOf(label) + "\n");
1055
+
1056
+        $code.append("FTOI\n");
1057
+        $code.append("LABEL ");
1058
+        $code.append(String.valueOf(label + 1) + "\n");
1059
+        label += 2;
1060
+        sp--;
1061
+    }
1062
+;
1063
+
1064
+//Il a été décider que les affichage n'avait pas d'effet de bord sur la pile
1065
+affichage returns [ StringBuilder code ]
1066
+    : 'afficher('IDENTIFIANT')'
1067
+    {
1068
+        $code = new StringBuilder();
1069
+        String id = $IDENTIFIANT.text;
1070
+
1071
+        if(registre.containsKey("i_" + id))
1072
+        {
1073
+            $code.append("PUSHG ");
1074
+            $code.append(String.valueOf(registre.get("i_" + id)));
1075
+            $code.append("\n");
1076
+            $code.append("WRITE\n");
1077
+            $code.append("POP\n");
1078
+        }
1079
+        else if(registre.containsKey("r_" + id))
1080
+        {
1081
+            $code.append("PUSHG ");
1082
+            $code.append(String.valueOf(registre.get("r_" + id) + 1));
1083
+            $code.append("\nWRITE\n");
1084
+            $code.append("PUSHG ");
1085
+            $code.append(String.valueOf(registre.get("r_" + id)));
1086
+            $code.append("\nWRITE\n");
1087
+            $code.append("POP\nPOP\n");
1088
+        }
1089
+        else if(registre.containsKey("b_" + id))
1090
+        {
1091
+            $code.append("PUSHG ");
1092
+            $code.append(String.valueOf(registre.get("b_" + id)));
1093
+            $code.append("\n");
1094
+            $code.append("WRITE\n");
1095
+            $code.append("POP\n");
1096
+        }
1097
+    }
1098
+    | 'afficher('a=expressionArithmetiqueRationnelle')'
1099
+    {
1100
+        $code = $a.code;
1101
+        $code.append("STOREG 3\nSTOREG 2\nPUSHG 3\nWRITE\nPUSHG 2\nWRITE\nPOP\nPOP\nPUSHG 2\nPUSHG 3\n");
1102
+    }
1103
+    | 'afficher('b=arrondi')'
1104
+    {
1105
+        $code = $b.code;
1106
+        $code.append("WRITE\n");
1107
+    }
1108
+    | 'afficher('c=expressionArithmetiqueEntiere')'
1109
+    {
1110
+        $code = $c.code;
1111
+        $code.append("WRITE\n");
1112
+    }
1113
+;
1114
+
1115
+
1116
+structureConditionnelle returns [ StringBuilder code ]
1117
+    : 'si'a=expressionArithmetiqueBooleene b=bloc'sinon'c=bloc
1118
+    {
1119
+        $code = $a.code;
1120
+        $code.append("JUMPF ");
1121
+        $code.append(String.valueOf(label + 1));
1122
+        $code.append("\nLABEL ");
1123
+        $code.append(String.valueOf(label));
1124
+        $code.append("\n");
1125
+        $code.append($b.code.toString());
1126
+        $code.append("JUMP ");
1127
+        $code.append(String.valueOf(label + 2));
1128
+        $code.append("\nLABEL ");
1129
+        $code.append(String.valueOf(label + 1));
1130
+        $code.append("\n");
1131
+        $code.append($c.code.toString());
1132
+        $code.append("LABEL ");
1133
+        $code.append(String.valueOf(label + 2));
1134
+        $code.append("\n");
1135
+        label += 3;
1136
+        sp--;
1137
+    }
1138
+    | 'si'a=expressionArithmetiqueBooleene b=bloc
1139
+    {
1140
+        $code = $a.code;
1141
+        $code.append("JUMPF ");
1142
+        $code.append(String.valueOf(label + 1));
1143
+        $code.append("\nLABEL ");
1144
+        $code.append(String.valueOf(label));
1145
+        $code.append("\n");
1146
+        $code.append($b.code.toString());
1147
+        $code.append("\nLABEL ");
1148
+        $code.append(String.valueOf(label + 1));
1149
+        $code.append("\n");
1150
+        label += 2;
1151
+        sp--;
1152
+    }
1153
+;
1154
+
1155
+
1156
+bloc returns [ StringBuilder code ]
1157
+    @init
1158
+    {
1159
+        $code = new StringBuilder();
1160
+    }
1161
+    : '{' (instruction { $code.append($instruction.code.toString()); })* '}'
1162
+;
1163
+
1164
+
1165
+boucle returns [ StringBuilder code ]
1166
+    : 'repeter'c=bloc'jusque'b=expressionArithmetiqueBooleene finInstruction
1167
+    {
1168
+        $code = new StringBuilder();
1169
+        $code.append("LABEL ");
1170
+        $code.append(String.valueOf(label));
1171
+        $code.append("\n");
1172
+        $code.append($c.code.toString());
1173
+        $code.append($b.code.toString());
1174
+        $code.append("JUMPF ");
1175
+        $code.append(String.valueOf(label));
1176
+        $code.append("\n");
1177
+        label++;
1178
+        sp--;
1179
+    }
1180
+    | 'tant que'b=expressionArithmetiqueBooleene c=bloc
1181
+    {
1182
+        $code = new StringBuilder();
1183
+        $code.append("LABEL ");
1184
+        $code.append(String.valueOf(label));
1185
+        $code.append("\n");
1186
+        $code.append($b.code.toString());
1187
+        $code.append("JUMPF ");
1188
+        $code.append(String.valueOf(label + 1));
1189
+        $code.append("\n");
1190
+        $code.append($c.code.toString());
1191
+        $code.append("JUMP ");
1192
+        $code.append(String.valueOf(label));
1193
+        $code.append("\nLABEL ");
1194
+        $code.append(String.valueOf(label + 1));
1195
+        $code.append("\n");
1196
+        label += 2;
1197
+        sp--;
1198
+    }
1199
+;
1200
+
1201
+
1202
+TYPE : 'int' | 'rat' | 'bool';
1203
+BOOL : 'true' | 'false';
1204
+NEWLINE : '\r'? '\n' -> skip;
1205
+COMMENT : '//' ~( '\n'|'\r' )* '\r'? '\n' -> skip;
1206
+ENTIER : ('0'..'9')+;
1207
+WS : (' '|'\t')+ -> skip;
1208
+IDENTIFIANT : (('a'..'z' | 'A'..'Z') + ('0'..'9')*)+;
1209
+UNMATCH : . -> skip;
1210
+
1211
+// --| Code MVàP généré |------------------------------------------
1212
+// Le code MVàP généré est enregistré dans un fichier 'a.mvap'.
1213
+// ----------------------------------------------------------------
1214
+//
1215
+// --| Problème de vidage de pile |--------------------------------
1216
+// Le code MVàP généré vide la pile avant l'instruction 'HALT'
1217
+// sauf dans le cas des conditions et boucles où le vidage de pile
1218
+// ne fonctionne pas.
1219
+// Cela vient du fait que l'assembleur MVàP supprime la partie
1220
+// inutile de la condition (i.e. le code du bloc qui ne sera jamais
1221
+// exécuté) ce que le compilateur ne peut pas prévoir.
1222
+// ----------------------------------------------------------------
0 1223