Merge branch 'master' of git.piment-noir.org:TP_AL_C
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Feb 2018 14:16:07 +0000 (15:16 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 13 Feb 2018 14:16:07 +0000 (15:16 +0100)
lexer/lexical_analyzer.c
lexer/main.c
lexer/print_helper.c
lexer/print_helper.h
lexer/syntactic_analyzer.c

index 6f995ba1c414b209d7603d8296a0b8e5821a8a8e..56415a01a914f73c8ee3355fada66416d6f10ead 100644 (file)
@@ -201,13 +201,13 @@ FIN:
 error:
     if (tokenType == MOT || tokenType == MOTCLE) {
         fwprintf(stderr, L"%s error with token type: %s and value: %ls\n",
-                  __func__,
-                  tokenTypestr[tokenType],
-                  token[tokenFound].value);
+                 __func__,
+                 tokenTypestr[tokenType],
+                 token[tokenFound].value);
     } else {
         fwprintf(stderr, L"%s error with token type: %s\n",
-                  __func__,
-                  tokenTypestr[tokenType]);
+                 __func__,
+                 tokenTypestr[tokenType]);
     }
     fflush(stderr);
     tokenType = FIN;
index f8622174c4b2c8cb52f5a84f4ce0aff74a87a6dc..7c31ee37aa388dd52629cc95f06359a560b56f9e 100644 (file)
@@ -13,7 +13,7 @@ void do_lexical_analysis() {
     c = fgetwc(source); // lecture du premier caractere
     do {
         scanner();
-        wprint_token_target();
+        wprint_token(target);
         token[tokenFound].type = tokenTypestr[tokenType];
         tokenFound++;
     } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte
@@ -84,13 +84,15 @@ int main(int argc, char **argv) {
             case ':':
                 /* missing option argument */
                 pr_error("%s: option '-%c' requires an argument\n",
-                        argv[0], optopt);
+                         argv[0], optopt);
                 break;
             case '?':
             default:
                 /* invalid option */
                 pr_error("%s: option '-%c' is invalid: ignored\n",
-                        argv[0], optopt);
+                         argv[0], optopt);
+                /* print the help message for invalid options */
+                hflag = 1;
                 break;
         }
     }
@@ -120,7 +122,7 @@ int main(int argc, char **argv) {
 
     if (hflag) {
         print_usage(argv[0]);
-    } else if (lflag){
+    } else if (lflag) {
         do_lexical_analysis();
     } else {
         do_syntactic_analysis();
index f7ab04ffb9887b2e2fbdf5d96ea06b6016ca509c..630985be4a8331b5f5147b21e084b03d10aab720 100644 (file)
@@ -1,7 +1,7 @@
-#include <stdarg.h>
 #include <stdio.h>
 #include <wchar.h>
 
+#include "print_helper.h"
 #include "global_vars.h"
 
 void pr_warning(const char* format, ...) {
@@ -53,19 +53,11 @@ void wpr_error(const wchar_t *format, ...) {
     fflush(stderr);
 }
 
-void wprint_token_stdout() {
+void wprint_token(FILE* out_file) {
     if (tokenType == MOT || tokenType == MOTCLE) {
-        fwprintf(stdout, L"%20s: %ls\n", tokenTypestr[tokenType], token[tokenFound].value);
+        fwprintf(out_file, L"%20s: %ls\n", tokenTypestr[tokenType], token[tokenFound].value);
     } else {
-        fwprintf(stdout, L"%20s\n", tokenTypestr[tokenType]);
-    }
-    fflush(stdout);
-}
-
-void wprint_token_target() {
-    if (tokenType == MOT || tokenType == MOTCLE) {
-        fwprintf(target, L"%20s: %ls\n", tokenTypestr[tokenType], token[tokenFound].value);
-    } else {
-        fwprintf(target, L"%20s\n", tokenTypestr[tokenType]);
+        fwprintf(out_file, L"%20s\n", tokenTypestr[tokenType]);
     }
+    fflush(out_file);
 }
index 396afd448be74a6fe7a3886dbf8f3ed2929a3aff..080ea9ddec4ff018eb21770d0434d3700076a5e3 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef PRINT_HELPER_H_
 #define PRINT_HELPER_H_
 
+#include <stdio.h>
+
 void pr_warning(const char *format, ...);
 void pr_error(const char *format, ...);
 
@@ -9,7 +11,6 @@ void pr_debug(const char *format, ...);
 void wpr_warning(const wchar_t *format, ...);
 void wpr_error(const wchar_t *format, ...);
 
-void wprint_token_stdout();
-void wprint_token_target();
+void wprint_token(FILE* out_file);
 
 #endif /* PRINT_HELPER_H_ */
index 29524f01f7b39d82d2328f3c773be5565941d30e..ddef90b12707dc5f09009e2d62f182a46899cd0c 100644 (file)
@@ -25,7 +25,7 @@ static void analyze_TEXT() {
             fputws(L" ", target);
         }
         #if PRINT_TOKEN
-        wprint_token_stdout();
+        wprint_token(stdout);
         #endif
         token[tokenFound].type = tokenTypestr[tokenType];
         tokenFound++;
@@ -48,7 +48,7 @@ static void analyze_P() {
     if (tokenType == NPARA) {
         scanner();
         #if PRINT_TOKEN
-        wprint_token_stdout();
+        wprint_token(stdout);
         #endif
         if (target != NULL) {
             fputws(L"<p>\n", target);
@@ -60,7 +60,7 @@ static void analyze_P() {
         if (tokenType == MOT) {
             scanner();
             #if PRINT_TOKEN
-            wprint_token_stdout();
+            wprint_token(stdout);
             #endif
             if (target != NULL) {
                 fputws((const wchar_t*)token[tokenFound].value, target);
@@ -91,7 +91,7 @@ static void analyze_HEAD() {
     if (tokenType == MOTCLE) {
         scanner();
         #if PRINT_TOKEN
-        wprint_token_stdout();
+        wprint_token(stdout);
         #endif /* PRINT_TOKEN */
         token[tokenFound].type = tokenTypestr[tokenType];
         tokenFound++;
@@ -108,7 +108,7 @@ static void analyze_HEAD() {
         if (tokenType == MOTCLE) {
             scanner();
             #if PRINT_TOKEN
-            wprint_token_stdout();
+            wprint_token(stdout);
             #endif /* PRINT_TOKEN */
             token[tokenFound].type = tokenTypestr[tokenType];
             tokenFound++;
@@ -132,7 +132,7 @@ static void analyze_H1() {
     if (tokenType == SECTION) {
         scanner();
         #if PRINT_TOKEN
-        wprint_token_stdout();
+        wprint_token(stdout);
         #endif /* PRINT_TOKEN */
         if (target != NULL) {
             fputws(L"<h1>\n", target);
@@ -158,7 +158,7 @@ static void analyze_H2() {
     if (tokenType == SSECTION) {
         scanner();
         #if PRINT_TOKEN
-        wprint_token_stdout();
+        wprint_token(stdout);
         #endif /* PRINT_TOKEN */
         if (target != NULL) {
             fputws(L"<h2>\n", target);
@@ -234,7 +234,7 @@ void analyze_AXIOME() {
     scanner();
     /* print the lexical analysis result */
     #if PRINT_TOKEN
-    wprint_token_stdout();
+    wprint_token(stdout);
     #endif /* PRINT_TOKEN */
     token[tokenFound].type = tokenTypestr[tokenType];
     tokenFound++;