Syntactic analysis fully working implementation.
[TP_AL_C.git] / lexer / main.c
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 wprint_token();
17 token[tokenFound].type = tokenTypestr[tokenType];
18 tokenFound++;
19 } while (tokenType != FIN); // tant que la fin du fichier n'est pas atteinte
20 }
21
22 void do_syntactic_analysis() {
23 c = fgetwc(source); // lecture du premier caractere
24 analyze_AXIOME();
25 }
26
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 }
36
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 }
92 }
93
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) {
98 pr_error("Fail to open file %s\n", in_file);
99 return EXIT_FAILURE;
100 }
101 } else {
102 source = stdin;
103 }
104
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) {
110 pr_error("Fail to open file %s\n", out_file);
111 return EXIT_FAILURE;
112 }
113 } else {
114 target = stdout;
115 }
116
117 if (hflag) {
118 print_usage(argv[0]);
119 } else if (lflag){
120 do_lexical_analysis();
121 } else {
122 do_syntactic_analysis();
123 }
124
125 if (source != NULL) fclose(source); // fermeture du fichier source
126 if (target != NULL) fclose(target); // fermeture du fichier target
127
128 return EXIT_SUCCESS;
129 }