| 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | #include <stdbool.h> |
| 5 | #include <getopt.h> |
| 6 | |
| 7 | #include "global_vars.h" |
| 8 | #include "lexical_analyzer.h" |
| 9 | #include "syntactic_analyzer.h" |
| 10 | #include "print_helper.h" |
| 11 | |
| 12 | void do_lexical_analysis() { |
| 13 | c = fgetwc(source); // lecture du premier caractere |
| 14 | do { |
| 15 | scanner(); |
| 16 | if (tokenType == MOT || tokenType == MOTCLE) { |
| 17 | fwprintf(target, L"%20s: %ls\n", tokenTypestr[tokenType], token[tokenFound].value); |
| 18 | } else { |
| 19 | fwprintf(target, L"%20s\n", tokenTypestr[tokenType]); |
| 20 | } |
| 21 | token[tokenFound].type = tokenTypestr[tokenType]; |
| 22 | tokenFound++; |
| 23 | } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte |
| 24 | } |
| 25 | |
| 26 | void do_syntactic_analysis() { |
| 27 | c = fgetwc(source); // lecture du premier caractere |
| 28 | do { |
| 29 | analyze_AXIOME(); |
| 30 | } while (tokenType != FIN); |
| 31 | } |
| 32 | |
| 33 | void print_usage(const char* name) { |
| 34 | fprintf(stdout,"Usage: %s [options]\n" |
| 35 | "Where [options] are:\n" |
| 36 | " -h, --help: display this help text\n" |
| 37 | " -l, --lexical-only: do only the lexical analysis\n" |
| 38 | " -i, --input<filename>: use <filename> as input file instead of standard input\n" |
| 39 | " -o, --output<filename>: use <filename> as output file instead of standard output\n", |
| 40 | name); |
| 41 | } |
| 42 | |
| 43 | int main(int argc, char **argv) { |
| 44 | /* In and out files name */ |
| 45 | const char* in_file = NULL; |
| 46 | const char* out_file = NULL; |
| 47 | static int hflag = 0; |
| 48 | static int lflag = 0; |
| 49 | |
| 50 | /* getopt_long stores the option index here. */ |
| 51 | int option_index = 0; |
| 52 | |
| 53 | static struct option long_options[] = |
| 54 | { |
| 55 | {"help", no_argument, &hflag, 1}, |
| 56 | {"input", optional_argument, NULL, 'i'}, |
| 57 | {"lexical-only", no_argument, &lflag, 1}, |
| 58 | {"output", optional_argument, NULL, 'o'}, |
| 59 | {0, 0, 0, 0} |
| 60 | }; |
| 61 | |
| 62 | int c_in; |
| 63 | |
| 64 | while ((c_in = getopt_long(argc, argv, "hi::lo::", long_options, |
| 65 | &option_index)) != -1) { |
| 66 | switch (c_in) { |
| 67 | case 'h': |
| 68 | hflag = 1; |
| 69 | break; |
| 70 | case 'i': |
| 71 | if (optarg != NULL) { |
| 72 | in_file = optarg; |
| 73 | } |
| 74 | break; |
| 75 | case 'l': |
| 76 | lflag = 1; |
| 77 | break; |
| 78 | case 'o': |
| 79 | if (optarg != NULL) { |
| 80 | out_file = optarg; |
| 81 | } |
| 82 | break; |
| 83 | case 0: |
| 84 | /* getopt_long() set a variable, just keep going */ |
| 85 | break; |
| 86 | case ':': |
| 87 | /* missing option argument */ |
| 88 | pr_error("%s: option '-%c' requires an argument\n", |
| 89 | argv[0], optopt); |
| 90 | break; |
| 91 | case '?': |
| 92 | default: |
| 93 | /* invalid option */ |
| 94 | pr_error("%s: option '-%c' is invalid: ignored\n", |
| 95 | argv[0], optopt); |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (in_file != NULL) { |
| 101 | // Ouvre le fichier source en lecture seulement (le fichier doit exister) : |
| 102 | source = fopen(in_file, "r+"); |
| 103 | if (source == NULL) { |
| 104 | pr_error("Impossible d'ouvrir le fichier %s\n", in_file); |
| 105 | return EXIT_FAILURE; |
| 106 | } |
| 107 | } else { |
| 108 | source = stdin; |
| 109 | } |
| 110 | |
| 111 | if (out_file != NULL) { |
| 112 | // Cree et ouvre le fichier cible en lecture/ecriture |
| 113 | // avec suppression du contenu au prealable : |
| 114 | target = fopen(out_file, "w+"); |
| 115 | if (target == NULL) { |
| 116 | pr_error("Impossible d'ouvrir le fichier %s\n", out_file); |
| 117 | return EXIT_FAILURE; |
| 118 | } |
| 119 | } else { |
| 120 | target = stdout; |
| 121 | } |
| 122 | |
| 123 | if (hflag) { |
| 124 | print_usage(argv[0]); |
| 125 | } else if (lflag){ |
| 126 | do_lexical_analysis(); |
| 127 | } else { |
| 128 | do_syntactic_analysis(); |
| 129 | } |
| 130 | |
| 131 | if (source != NULL) fclose(source); // fermeture du fichier source |
| 132 | if (target != NULL) fclose(target); // fermeture du fichier target |
| 133 | |
| 134 | return EXIT_SUCCESS; |
| 135 | } |