Syntactic analyser implementation with HTML conversion code
[TP_AL_C.git] / lexer / syntactic_analyzer.c
index e23c130d5bb24c22fa93c966fd9c736c8c4c35db..29524f01f7b39d82d2328f3c773be5565941d30e 100644 (file)
 
 #include <stdbool.h>
 #include <stdarg.h>
+#include <stdlib.h>
 
 #include "global_vars.h"
 #include "lexical_analyzer.h"
+#include "print_helper.h"
+
+#define PRINT_TOKEN 0
 
 /* Syntactic analyzer functions implementation */
 
 enum TokenType tokenType;
 
-static bool analyze_TEXT() {
-    bool rtval = true;
+static void analyze_TEXT() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
     if (tokenType == MOT) {
         scanner();
-        rtval = analyze_TEXT();
+        if (target != NULL) {
+            fputws((const wchar_t*)token[tokenFound].value, target);
+            fputws(L" ", target);
+        }
+        #if PRINT_TOKEN
+        wprint_token_stdout();
+        #endif
+        token[tokenFound].type = tokenTypestr[tokenType];
+        tokenFound++;
+        analyze_TEXT();
     } else if (tokenType != MOTCLE && tokenType != NPARA && tokenType != SECTION && \
                 tokenType != SSECTION && tokenType != FIN) {
-        rtval = false;
+        fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
+        fflush(stderr);
+        exit(EXIT_FAILURE);
     }
-    return rtval;
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-static bool analyze_P() {
-    bool rtval = true;
+static void analyze_P() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
     if (tokenType == NPARA) {
         scanner();
+        #if PRINT_TOKEN
+        wprint_token_stdout();
+        #endif
+        if (target != NULL) {
+            fputws(L"<p>\n", target);
+            fputws((const wchar_t*)token[tokenFound].value, target);
+            fputws(L" ", target);
+        }
+        token[tokenFound].type = tokenTypestr[tokenType];
+        tokenFound++;
         if (tokenType == MOT) {
             scanner();
-            rtval = analyze_TEXT();
-            rtval = analyze_P();
+            #if PRINT_TOKEN
+            wprint_token_stdout();
+            #endif
+            if (target != NULL) {
+                fputws((const wchar_t*)token[tokenFound].value, target);
+                fputws(L" ", target);
+            }
+            token[tokenFound].type = tokenTypestr[tokenType];
+            tokenFound++;
+            analyze_TEXT();
+            if (target != NULL) {
+                fputws(L"\n</p>\n", target);
+            }
+            analyze_P();
+        } else if (tokenType != SECTION && tokenType != SSECTION && tokenType != FIN) {
+            fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
+            fflush(stderr);
+            exit(EXIT_FAILURE);
         }
-    } else if (tokenType != SECTION && tokenType != SSECTION && tokenType != FIN) {
-        rtval = false;
     }
-    return rtval;
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-static bool analyze_HEAD() {
-    bool rtval = true;
+static void analyze_HEAD() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
     if (tokenType == MOTCLE) {
         scanner();
-        rtval = analyze_TEXT();
+        #if PRINT_TOKEN
+        wprint_token_stdout();
+        #endif /* PRINT_TOKEN */
+        token[tokenFound].type = tokenTypestr[tokenType];
+        tokenFound++;
+        //FIXME: Check if the MOTCLE token value is set to >Titre
+        if (target != NULL) {
+            fputws(L"<title>\n", target);
+            fputws((const wchar_t*)token[tokenFound].value, target);
+            fputws(L" ", target);
+        }
+        analyze_TEXT();
+        if (target != NULL) {
+            fputws(L"\n</title>\n", target);
+        }
         if (tokenType == MOTCLE) {
             scanner();
-            rtval = analyze_TEXT();
-        } else {
-            rtval = false;
+            #if PRINT_TOKEN
+            wprint_token_stdout();
+            #endif /* PRINT_TOKEN */
+            token[tokenFound].type = tokenTypestr[tokenType];
+            tokenFound++;
+            //The text contain the author
+            analyze_TEXT();
+        } else if (tokenType != NPARA && tokenType != SECTION && tokenType != FIN) {
+            fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
+            fflush(stderr);
+            exit(EXIT_FAILURE);
         }
-    } else {
-        rtval = false;
     }
-    return rtval;
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-static bool analyze_H1() {
-    bool rtval = true;
+static void analyze_H1() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
     if (tokenType == SECTION) {
         scanner();
-        rtval = analyze_TEXT();
-    } else {
-        rtval = false;
+        #if PRINT_TOKEN
+        wprint_token_stdout();
+        #endif /* PRINT_TOKEN */
+        if (target != NULL) {
+            fputws(L"<h1>\n", target);
+            fputws((const wchar_t*)token[tokenFound].value, target);
+            fputws(L" ", target);
+        }
+        token[tokenFound].type = tokenTypestr[tokenType];
+        tokenFound++;
+        analyze_TEXT();
+        if (target != NULL) {
+            fputws(L"\n</h1>\n", target);
+        }
     }
-    return rtval;
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-static bool analyze_H2() {
-    bool rtval = true;
+static void analyze_H2() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
     if (tokenType == SSECTION) {
         scanner();
-        rtval = analyze_TEXT();
-    } else {
-        rtval = false;
+        #if PRINT_TOKEN
+        wprint_token_stdout();
+        #endif /* PRINT_TOKEN */
+        if (target != NULL) {
+            fputws(L"<h2>\n", target);
+            fputws((const wchar_t*)token[tokenFound].value, target);
+            fputws(L" ", target);
+        }
+        token[tokenFound].type = tokenTypestr[tokenType];
+        tokenFound++;
+        analyze_TEXT();
+        if (target != NULL) {
+            fputws(L"\n</h2>\n", target);
+        }
     }
-    return rtval;
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-static bool analyze_S2() {
-    bool rtval = true;
-    if (analyze_H2()) {
-        rtval = analyze_P();
-        rtval = analyze_S2();
+static void analyze_S2() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
+    //FIXME: This test is probably useless
+    if (tokenType == SSECTION) {
+        analyze_H2();
+        analyze_P();
+        analyze_S2();
     } else if (tokenType != SECTION && tokenType != FIN) {
-        rtval = false;
-    } else {
-        rtval = false;
+        fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
+        fflush(stderr);
+        exit(EXIT_FAILURE);
     }
-    return rtval;
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-static bool analyze_S1() {
-    bool rtval = true;
-    if (analyze_H1()) {
-        rtval = analyze_P();
-        rtval = analyze_S2();
-        rtval = analyze_S1();
+static void analyze_S1() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
+    //FIXME: This test is probably useless
+    if (tokenType == SECTION) {
+        analyze_H1();
+        analyze_P();
+        analyze_S2();
+        analyze_S1();
     } else if (tokenType != FIN) {
-        rtval = false;
-    } else {
-        rtval = false;
+        fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
+        fflush(stderr);
+        exit(EXIT_FAILURE);
     }
-    return rtval;
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-static bool analyze_BODY() {
-    bool rtval = true;
-    rtval = analyze_P();
-    rtval = analyze_S1();
-    return rtval;
+static void analyze_BODY() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
+    analyze_P();
+    analyze_S1();
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
 }
 
-bool analyze_AXIOME() {
-    bool rtval = true;
+void analyze_AXIOME() {
+    #if DEBUG
+    fprintf(stdout, "entering %s\n", __func__);
+    #endif
     scanner();
-    rtval = analyze_HEAD();
-    rtval = analyze_BODY();
+    /* print the lexical analysis result */
+    #if PRINT_TOKEN
+    wprint_token_stdout();
+    #endif /* PRINT_TOKEN */
+    token[tokenFound].type = tokenTypestr[tokenType];
+    tokenFound++;
+    if (target != NULL) {
+        fputws(L"<head>\n", target);
+    }
+    analyze_HEAD();
+    if (target != NULL) {
+        fputws(L"\n</head>\n", target);
+    }
+    if (target != NULL) {
+        fputws(L"<body>\n", target);
+    }
+    analyze_BODY();
+    if (target != NULL) {
+        fputws(L"\n<body>\n", target);
+    }
     if (tokenType != FIN) {
-        rtval = false;
+        fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
+        fflush(stderr);
+        exit(EXIT_FAILURE);
     }
-    return rtval;
+    fprintf(stdout, "successful syntactic analysis\n");
+    #if DEBUG
+    fprintf(stdout, "leaving %s\n", __func__);
+    #endif
+    fflush(stdout);
 }