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