Avoid filling twice token values.
[TP_AL_C.git] / lexer / main.c
CommitLineData
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
12void do_lexical_analysis() {
13 c = fgetwc(source); // lecture du premier caractere
14 do {
15 scanner();
f196dae5 16 wprint_token(target);
9ed84d89 17 token[tokenFound].type = tokenTypestr[tokenType];
dfbc1df9 18 wcscpy((wchar_t*)tokenValue, (wchar_t*)token[tokenFound].value);
9ed84d89
JB
19 tokenFound++;
20 } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte
4b580abd
JB
21}
22
9ed84d89 23void do_syntactic_analysis() {
48e4b4ad
JB
24 fputws(L"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr_FR\" lang=\"fr_FR\">\n",
25 target);
9ed84d89 26 c = fgetwc(source); // lecture du premier caractere
15ad4b5a 27 scanner();
e70feb8c 28 analyze_AXIOME();
48e4b4ad 29 fputws(L"</html>\n", target);
4b580abd
JB
30}
31
9ed84d89
JB
32void print_usage(const char* name) {
33 fprintf(stdout,"Usage: %s [options]\n"
34 "Where [options] are:\n"
f491c07a 35 " -h, --help: display this help message\n"
9ed84d89
JB
36 " -l, --lexical-only: do only the lexical analysis\n"
37 " -i, --input<filename>: use <filename> as input file instead of standard input\n"
38 " -o, --output<filename>: use <filename> as output file instead of standard output\n",
39 name);
48e4b4ad 40 fflush(stdout);
9ed84d89 41}
4b580abd 42
9ed84d89
JB
43int 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",
25696723 89 argv[0], optopt);
9ed84d89
JB
90 break;
91 case '?':
92 default:
93 /* invalid option */
94 pr_error("%s: option '-%c' is invalid: ignored\n",
25696723 95 argv[0], optopt);
5c1346dd
JB
96 /* print the help message for invalid options */
97 hflag = 1;
9ed84d89
JB
98 break;
99 }
d3eb30ef 100 }
4b580abd 101
9ed84d89
JB
102 if (in_file != NULL) {
103 // Ouvre le fichier source en lecture seulement (le fichier doit exister) :
104 source = fopen(in_file, "r+");
105 if (source == NULL) {
224a9916 106 pr_error("Fail to open file %s\n", in_file);
9ed84d89
JB
107 return EXIT_FAILURE;
108 }
109 } else {
110 source = stdin;
4b580abd
JB
111 }
112
9ed84d89
JB
113 if (out_file != NULL) {
114 // Cree et ouvre le fichier cible en lecture/ecriture
115 // avec suppression du contenu au prealable :
116 target = fopen(out_file, "w+");
117 if (target == NULL) {
224a9916 118 pr_error("Fail to open file %s\n", out_file);
9ed84d89
JB
119 return EXIT_FAILURE;
120 }
121 } else {
122 target = stdout;
96964f3e
JB
123 }
124
9ed84d89
JB
125 if (hflag) {
126 print_usage(argv[0]);
f8c1adc1 127 } else if (lflag) {
9ed84d89
JB
128 do_lexical_analysis();
129 } else {
130 do_syntactic_analysis();
96964f3e
JB
131 }
132
96964f3e
JB
133 if (source != NULL) fclose(source); // fermeture du fichier source
134 if (target != NULL) fclose(target); // fermeture du fichier target
135
b5cd8f86 136 return EXIT_SUCCESS;
96964f3e 137}