Browse Source

Ajout chaines de caractère au compilateur

Faure Paul 2 years ago
parent
commit
012f46f03c
5 changed files with 88 additions and 7 deletions
  1. 3
    1
      Lex_Yacc/al.lex
  2. 80
    4
      Lex_Yacc/as.y
  3. 1
    1
      Makefile
  4. 3
    0
      Tables/Instructions/tab_instruc.c
  5. 1
    1
      Tables/Instructions/tab_instruc.h

+ 3
- 1
Lex_Yacc/al.lex View File

@@ -41,7 +41,7 @@ yyerror (char const *s)
41 41
 "]" 		    { return tCCROCH;   }    // Token crochet ouvrante
42 42
 
43 43
 "get"       { return tGET;      }    // Token fonction get
44
-"printf"    { return tPRINTF;   }    // Token fonction print
44
+"print"     { return tPRINT;    }    // Token fonction print
45 45
 "stop"      { return tSTOP;     }    // Token fonction stop
46 46
 
47 47
 "+"			    { return tADD;      }    // Token addition
@@ -57,6 +57,8 @@ yyerror (char const *s)
57 57
 [0-9]+	        { yylval.nombre = atoi(yytext); return tNB; }    // Token nombre au format classique
58 58
 [0-9]+e[0-9]+ 	{ yylval.nombre = -1; return tNB;        }       // Token nombre au format exponentiel
59 59
 
60
+["][^"]*["]     { strcpy(yylval.str, yytext); return tSTR; }
61
+
60 62
 [a-zA-Z][a-zA-Z0-9_]* { strcpy(yylval.id, yytext); return tID; } // Chaine de caractère (identifiant variable, fonction..)
61 63
 
62 64
 

+ 80
- 4
Lex_Yacc/as.y View File

@@ -10,7 +10,8 @@
10 10
 %union {
11 11
 	int nombre;
12 12
 	struct symbole_t symbole;
13
-  char id[30];
13
+    char id[30];
14
+    char str[300];
14 15
 	struct while_t my_while;
15 16
 }
16 17
 %{
@@ -45,7 +46,8 @@ int first_etoile = 1;
45 46
 %token tMUL tDIV tADD tSUB tEQ
46 47
 %token<nombre> tNB tNBEXP
47 48
 %token<id> tID
48
-%token tPRINTF tGET tSTOP
49
+%token<str> tSTR
50
+%token tPRINT tGET tSTOP
49 51
 %token<nombre> tIF tELSE
50 52
 %token<my_while> tWHILE
51 53
 %token tRETURN
@@ -116,10 +118,84 @@ Get : tGET tOBRACE tCBRACE        {int addr = push("0_TEMPORARY", 0, integer);
116 118
                                   };
117 119
 
118 120
 // Print, une fonction particulière
119
-Print : tPRINTF tOBRACE E tCBRACE {add_operation(PRI,$3,0,0);                                      // On ajoute l'instruction PRI
120
-																	 pop();                                                          // On supprime la variable temporaire
121
+Print : tPRINT tOBRACE E tCBRACE  {add_operation(PRI,$3,0,0);                                      // On ajoute l'instruction PRI
122
+								                   pop();                                                          // On supprime la variable temporaire
121 123
                                   };
122 124
 
125
+// Print, une fonction particulière
126
+Print : tPRINT tOBRACE tSTR tCBRACE  {int i = 0;
127
+                                      int decalage = 0;
128
+                                      char previous = 'a';
129
+                                      while ($3[i] != '\0') {
130
+                                        if (previous == '\\' && $3[i] == 'n') {
131
+                                          $3[i - decalage - 1] = '\n';
132
+                                          previous = 'a';
133
+                                          decalage++;
134
+                                        } else {
135
+                                          $3[i-decalage] = $3[i];
136
+                                          previous = $3[i];
137
+                                        }
138
+                                        i++;
139
+                                      }
140
+                                      $3[i-decalage] = $3[i];
141
+                                      i=0;
142
+                                      int termine = $3[i+1] == '"';
143
+                                      int addr = push("0_TEMPORARY", 0, integer); 
144
+                                      while (!termine) {
145
+                                        if ($3[i+1] != '"') {
146
+                                          add_operation(AFC, addr + i, $3[i+1], 0);
147
+                                        } else {
148
+                                          termine = 1;
149
+                                        }
150
+                                        i++;
151
+                                        if (!termine && $3[i+1] != '"') {
152
+                                          add_operation(AFC, addr + i, $3[i+1], 0);
153
+                                        } else {
154
+                                          termine = 1;
155
+                                        }
156
+                                        i++;
157
+                                        if (!termine && $3[i+1] != '"') {
158
+                                          add_operation(AFC, addr + i, $3[i+1], 0);
159
+                                        } else {
160
+                                          termine = 1;
161
+                                        }
162
+                                        i++;
163
+                                        if (!termine && $3[i+1] != '"') {
164
+                                          add_operation(AFC, addr + i, $3[i+1], 0);
165
+                                        } else {
166
+                                          termine = 1;
167
+                                        }
168
+
169
+                                        i = i - 3;
170
+                                        termine = 0;
171
+                                        if ($3[i+1] != '"') {
172
+                                          add_operation(PRIC, addr + i, 0, 0);
173
+                                        } else {
174
+                                          termine = 1;
175
+                                        }
176
+                                        i++;
177
+                                        if (!termine && $3[i+1] != '"') {
178
+                                          add_operation(PRIC, addr + i, 0, 0);
179
+                                        } else {
180
+                                          termine = 1;
181
+                                        }
182
+                                        i++;
183
+                                        if (!termine && $3[i+1] != '"') {
184
+                                          add_operation(PRIC, addr + i, 0, 0);
185
+                                        } else {
186
+                                          termine = 1;
187
+                                        }
188
+                                        i++;
189
+                                        if (!termine && $3[i+1] != '"') {
190
+                                          add_operation(PRIC, addr + i, 0, 0);
191
+                                        } else {
192
+                                          termine = 1;
193
+                                        }
194
+                                        i++;
195
+                                      }
196
+                                      pop();                                                         // On supprime la variable temporaire
197
+                                     }; 
198
+
123 199
 // Stop, une fonction particulière
124 200
 Stop : tSTOP tOBRACE tNB tCBRACE  {add_operation(STOP,$3,0,0);                                     // On ajoute juste l'instruction stop
125 201
                                   };

+ 1
- 1
Makefile View File

@@ -31,7 +31,7 @@ clean_Lex_Yacc:
31 31
 ###     COMPILATION     ###
32 32
 ###########################
33 33
 build : clean build_Symboles build_Instructions build_Lex_Yacc build_Fonctions
34
-	gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o Tables/Fonctions/tab_fonctions.o -ll -o rondoudou_gcc
34
+	gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o Tables/Fonctions/tab_fonctions.o -ly -o rondoudou_gcc
35 35
 
36 36
 build_Symboles: clean_Symboles
37 37
 	gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o

+ 3
- 0
Tables/Instructions/tab_instruc.c View File

@@ -75,6 +75,9 @@ char * get_asm_line_from_op(struct operation_t op){
75 75
 		case (PRI):
76 76
 			sprintf(buffer,"PRI %d\n",op.arg1);
77 77
 			break;
78
+    case (PRIC):
79
+			sprintf(buffer,"PRIC %d\n",op.arg1);
80
+			break;
78 81
 		case (READ):
79 82
 			sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2);
80 83
 			break;

+ 1
- 1
Tables/Instructions/tab_instruc.h View File

@@ -8,7 +8,7 @@
8 8
 
9 9
 
10 10
 // Liste de codes des instruction 
11
-enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,JMP,JMF,INF,SUP,EQU,PRI,READ,WR,CALL,RET,GET,STOP};
11
+enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,JMP,JMF,INF,SUP,EQU,PRI,PRIC,READ,WR,CALL,RET,GET,STOP};
12 12
 
13 13
 //Ajoute une opération dans la table (à la fin)
14 14
 void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3);

Loading…
Cancel
Save