8122023c63a6a6ded748ea99eda9cfba89d1e840
[TP_AL_C.git] / lexer / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdbool.h>
5
6 FILE *source, *target = NULL;
7 char c;
8 unsigned int i = 0;
9 char tokenValue[50];
10 enum TokenType {
11 MOTCLE,
12 SECTION,
13 SSECTION,
14 NPARA,
15 MOT,
16 FIN
17 } tokenType;
18 const char* tokenTypestr[] = { "MOTCLE", "SECTION", "SSECTION", "NPARA", "MOT", "FIN" };
19
20 /* It looks silly to check for each characters but for debugging, it's just the way to go */
21 bool istAlpha() {
22 if (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'g' || \
23 c == 'h' || c == 'i' || c == 'j' || c == 'k' || c == 'l' || c == 'm' || c == 'n' || \
24 c == 'o' || c == 'p' || c == 'q' || c == 'r' || c == 's' || c == 't' || c == 'u' || \
25 c == 'v' || c == 'w' || c == 'x' || c == 'y' || c == 'z' || \
26 c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F' || c == 'G' || \
27 c == 'H' || c == 'I' || c == 'J' || c == 'K' || c == 'L' || c == 'M' || c == 'N' || \
28 c == 'O' || c == 'P' || c == 'Q' || c == 'R' || c == 'S' || c == 'T' || c == 'U' || \
29 c == 'V' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z' || \
30 c == '.' || c == '?' || c == '!' || c == ',' || c == ';' || c == ':' || c == '-' || \
31 c == '\''|| c == '#' || \
32 c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || \
33 c == '7' || c == '8' || c == '9') {
34 return true;
35 }
36 return false;
37 }
38
39 bool isSeparator() {
40 if (c == '\t' || c == ' ' || c == '\n') {
41 return true;
42 }
43 return false;
44 }
45
46 int scanner() {
47 const char* Titre = "Titre";
48 const char* Auteur = "Auteur";
49 unsigned int j = 0;
50
51 // The main loop get the next character
52 init:
53 if (c == ' ' || c == '\t') {
54 c = fgetc(source);
55 tokenValue[i] = c;
56 i++;
57 goto init;
58 }
59 if (c == '\n') {
60 c = fgetc(source);
61 tokenValue[i] = c;
62 i++;
63 goto initLV1;
64 }
65 if (c == '>') {
66 c = fgetc(source);
67 tokenValue[i] = c;
68 i++;
69 goto MC1;
70 }
71 if (c == '=') {
72 c = fgetc(source);
73 tokenValue[i] = c;
74 i++;
75 goto S1SS1;
76 }
77 if (istAlpha()) {
78 c = fgetc(source);
79 tokenValue[i] = c;
80 i++;
81 goto M1;
82 }
83 if (c == EOF) {
84 goto FIN;
85 } else {
86 goto error;
87 }
88
89 MC1:
90 // FIXME: Partial match need a rewind in the characters extraction from the file
91 if (c == Titre[j] && j < strlen(Titre) - 1) {
92 c = fgetc(source);
93 tokenValue[i] = c;
94 i++;
95 j++;
96 goto MC1;
97 }
98 if (c == Auteur[j] && j < strlen(Auteur) - 1) {
99 c = fgetc(source);
100 tokenValue[i] = c;
101 i++;
102 j++;
103 goto MC1;
104 } else {
105 c = fgetc(source);
106 tokenValue[i] = c;
107 i++;
108 goto MC2;
109 }
110
111 S1SS1:
112 if (c == '=') {
113 c = fgetc(source);
114 tokenValue[i] = c;
115 i++;
116 goto SS2;
117 }
118 if (isSeparator() || c == EOF) {
119 goto SECTION;
120 }
121
122 SS2:
123 if (isSeparator() || c == EOF) {
124 goto SSECTION;
125 }
126
127 SECTION:
128 tokenType = SECTION;
129 return 1;
130
131 SSECTION:
132 tokenType = SSECTION;
133 return 1;
134
135 M1:
136 if (istAlpha()) {
137 c = fgetc(source);
138 tokenValue[i] = c;
139 i++;
140 goto M1;
141 }
142 if (isSeparator() || c == EOF) {
143 goto MOT;
144 }
145
146 initLV1:
147 if (c == ' ' || c == '\t') {
148 c = fgetc(source);
149 tokenValue[i] = c;
150 i++;
151 goto initLV1;
152 }
153 if (c == '\n') {
154 c = fgetc(source);
155 tokenValue[i] = c;
156 i++;
157 goto initLV1LV2;
158 }
159 if (istAlpha()) {
160 c = fgetc(source);
161 tokenValue[i] = c;
162 i++;
163 goto M1;
164 }
165 if (c == '=') {
166 c = fgetc(source);
167 tokenValue[i] = c;
168 i++;
169 goto S1SS1;
170 }
171 if (c == '>') {
172 c = fgetc(source);
173 tokenValue[i] = c;
174 i++;
175 goto MC1;
176 }
177 if (c == EOF) {
178 goto FIN;
179 }
180
181 initLV1LV2:
182 if (isSeparator()) {
183 c = fgetc(source);
184 tokenValue[i] = c;
185 i++;
186 goto initLV1LV2;
187 }
188 if (istAlpha()) {
189 goto NPARA;
190 }
191 if (c == '>') {
192 c = fgetc(source);
193 tokenValue[i] = c;
194 i++;
195 goto MC1;
196 }
197 if (c == '=') {
198 c = fgetc(source);
199 tokenValue[i] = c;
200 i++;
201 goto S1SS1;
202 }
203 if (c == EOF) {
204 goto FIN;
205 }
206
207 NPARA:
208 tokenType = NPARA;
209 return 1;
210
211 MOT:
212 tokenType = MOT;
213 return 1;
214
215 MC2:
216 if (isSeparator() || c == EOF) {
217 goto MOTCLE;
218 }
219
220 MOTCLE:
221 tokenType = MOTCLE;
222 return 1;
223
224 FIN:
225 tokenType = FIN;
226 return 1;
227
228 error:
229 tokenType = FIN;
230 return -1;
231 }
232
233 int main (int argc, char const *argv[]) {
234
235 // Ouvre le fichier test.txt en lecture seulement (le fichier doit exister) :
236 source = fopen("test.txt", "r");
237 // Cree et ouvre un fichier target.html en lecture/ecriture
238 // avec suppression du contenu au prealable :
239 target = fopen("target.html", "w+");
240
241 if (source == NULL) {
242 printf("Impossible d'ouvrir le fichier source\n");
243 return -1;
244 }
245
246 if (target == NULL) {
247 printf("Impossible d'ouvrir le fichier target\n");
248 return -1;
249 }
250
251 do {
252 c = fgetc(source); // lecture du caractere suivant du fichier source
253 tokenValue[i] = c;
254 i++;
255 int scanrt = scanner();
256 if (scanrt == -1) {
257 printf ("Scanner error with token value: %s\n", tokenValue);
258 exit(EXIT_FAILURE);
259 }
260 if (c != EOF) {
261 printf ("Token type found: %s with value: %s\n", tokenTypestr[tokenType], tokenValue);
262 } else {
263 printf ("Token type found: %s\n", tokenTypestr[tokenType]);
264 }
265 // reinit tokenValue
266 i = 0;
267 memset(tokenValue, 0, sizeof(tokenValue));
268 } while (c != EOF); // tant que la fin du fichier n'est pas atteinte
269
270 if (source != NULL) fclose(source); // fermeture du fichier source
271 if (target != NULL) fclose(target); // fermeture du fichier target
272
273 return 0;
274 }