7 #include "global_vars.h"
8 #include "lexical_analyzer.h"
9 #include "syntactic_analyzer.h"
10 #include "print_helper.h"
12 void do_lexical_analysis() {
13 c
= fgetwc(source
); // lecture du premier caractere
16 if (tokenType
== MOT
|| tokenType
== MOTCLE
) {
17 fwprintf(target
, L
"%20s: %ls\n", tokenTypestr
[tokenType
], token
[tokenFound
].value
);
19 fwprintf(target
, L
"%20s\n", tokenTypestr
[tokenType
]);
21 token
[tokenFound
].type
= tokenTypestr
[tokenType
];
23 } while (tokenType
!= FIN
); // tant que la fin du fichier n'est pas atteinte
26 void do_syntactic_analysis() {
27 c
= fgetwc(source
); // lecture du premier caractere
30 } while (tokenType
!= FIN
);
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",
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
;
50 /* getopt_long stores the option index here. */
53 static struct option long_options
[] =
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'},
64 while ((c_in
= getopt_long(argc
, argv
, "hi::lo::", long_options
,
65 &option_index
)) != -1) {
84 /* getopt_long() set a variable, just keep going */
87 /* missing option argument */
88 pr_error("%s: option '-%c' requires an argument\n",
94 pr_error("%s: option '-%c' is invalid: ignored\n",
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("Fail to open file %s\n", in_file
);
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("Fail to open file %s\n", out_file
);
124 print_usage(argv
[0]);
126 do_lexical_analysis();
128 do_syntactic_analysis();
131 if (source
!= NULL
) fclose(source
); // fermeture du fichier source
132 if (target
!= NULL
) fclose(target
); // fermeture du fichier target