Store all tokens found during the lexical analysis in an array.
[TP_AL_C.git] / lexer / main.c
index a53d793e827c26326f216db73dfe3e48e71328f5..db41495086b75c03aa4a24683d07a188dabbc429 100644 (file)
@@ -3,10 +3,13 @@
 #include <string.h>
 #include <stdbool.h>
 
-FILE *source, *target = NULL;
+#define TOKEN_MAX_LENGTH 50
+#define TOKEN_LIST_MAX 500
+
+FILE *source = NULL, *target = NULL;
 char c;
 unsigned int i = 0;
-char tokenValue[50];
+char tokenValue[TOKEN_MAX_LENGTH];
 enum TokenType {
     MOTCLE,
     SECTION,
@@ -16,8 +19,9 @@ enum TokenType {
     FIN
 } tokenType;
 const char* tokenTypestr[] = { "MOTCLE", "SECTION", "SSECTION", "NPARA", "MOT", "FIN" };
+const char* tokenList[TOKEN_LIST_MAX];
 
-/* This looks silly to check for each characters but for debugging, it's just the way to go */
+/* It looks silly to check for each characters but for debugging, it's just the way to go */
 bool istAlpha() {
     if (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'g' || \
         c == 'h' || c == 'i' || c == 'j' || c == 'k' || c == 'l' || c == 'm' || c == 'n' || \
@@ -44,9 +48,9 @@ bool isSeparator() {
 }
 
 int scanner() {
-const char* Titre = "Titre";
-const char* Auteur = "Auteur";
-unsigned int j = 0;
+    const char* Titre = "Titre";
+    const char* Auteur = "Auteur";
+    unsigned int j = 0;
 
 // The main loop get the next character
 init:
@@ -87,6 +91,7 @@ init:
     }
 
 MC1:
+    // FIXME: Partial match need a rewind in the characters extraction from the file
     if (c == Titre[j] && j < strlen(Titre) - 1) {
         c = fgetc(source);
         tokenValue[i] = c;
@@ -247,6 +252,8 @@ int main (int argc, char const *argv[]) {
         return -1;
     }
 
+    int tokenFound = 0;
+
     do {
         c = fgetc(source); // lecture du caractere suivant du fichier source
         tokenValue[i] = c;
@@ -261,6 +268,8 @@ int main (int argc, char const *argv[]) {
         } else {
             printf ("Token type found: %s\n", tokenTypestr[tokenType]);
         }
+        tokenFound++;
+        tokenList[tokenFound] = tokenTypestr[tokenType];
         // reinit tokenValue
         i = 0;
         memset(tokenValue, 0, sizeof(tokenValue));