Fix the syntactic analyzer for real.
[TP_AL_C.git] / lexer / syntactic_analyzer.c
1 /* Syntactic analyzer */
2
3 #include <stdbool.h>
4 #include <stdarg.h>
5 #include <stdlib.h>
6
7 #include "global_vars.h"
8 #include "lexical_analyzer.h"
9 #include "print_helper.h"
10
11 #define PRINT_TOKEN 1
12
13 /* Syntactic analyzer functions implementation */
14
15 enum TokenType tokenType;
16
17 static void analyze_TEXT() {
18 #if DEBUG
19 fprintf(stdout, "entering %s\n", __func__);
20 #endif
21 if (tokenType == MOT) {
22 #if PRINT_TOKEN
23 wprint_ctoken(stdout);
24 #endif
25 if (target != NULL) {
26 fputws((const wchar_t*)tokenValue, target);
27 fputws(L" ", target);
28 }
29 scanner();
30 analyze_TEXT();
31 } else if (tokenType != MOTCLE && tokenType != NPARA && tokenType != SECTION && \
32 tokenType != SSECTION && tokenType != FIN) {
33 fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
34 fflush(stderr);
35 exit(EXIT_FAILURE);
36 }
37 #if DEBUG
38 fprintf(stdout, "leaving %s\n", __func__);
39 #endif
40 }
41
42 static void analyze_P() {
43 #if DEBUG
44 fprintf(stdout, "entering %s\n", __func__);
45 #endif
46 if (tokenType == NPARA) {
47 #if PRINT_TOKEN
48 wprint_ctoken(stdout);
49 #endif
50 scanner();
51 if (target != NULL) {
52 fputws(L"<p>\n", target);
53 }
54 analyze_TEXT();
55 if (target != NULL) {
56 fputws(L"\n</p>\n", target);
57 }
58 analyze_P();
59 } else if (tokenType != SECTION && tokenType != SSECTION && tokenType != FIN) {
60 fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
61 fflush(stderr);
62 exit(EXIT_FAILURE);
63 }
64 #if DEBUG
65 fprintf(stdout, "leaving %s\n", __func__);
66 #endif
67 }
68
69 static void analyze_HEAD() {
70 #if DEBUG
71 fprintf(stdout, "entering %s\n", __func__);
72 #endif
73 if (tokenType == MOTCLE) {
74 //FIXME: Check if the MOTCLE token value is set to >Titre
75 if (target != NULL) {
76 fputws(L"<title>\n", target);
77 fputws(L" ", target);
78 }
79 scanner();
80 analyze_TEXT();
81 if (target != NULL) {
82 fputws(L"\n</title>\n", target);
83 }
84 if (tokenType == MOTCLE) {
85 #if PRINT_TOKEN
86 wprint_ctoken(stdout);
87 #endif /* PRINT_TOKEN */
88 scanner();
89 //The text contain the author
90 fputws(L"<meta name=\"author\" content=\"", target);
91 analyze_TEXT();
92 fputws(L"\">", target);
93 } else if (tokenType != NPARA && tokenType != SECTION && tokenType != FIN) {
94 fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
95 fflush(stderr);
96 exit(EXIT_FAILURE);
97 }
98 }
99 #if DEBUG
100 fprintf(stdout, "leaving %s\n", __func__);
101 #endif
102 }
103
104 static void analyze_H1() {
105 #if DEBUG
106 fprintf(stdout, "entering %s\n", __func__);
107 #endif
108 if (tokenType == SECTION) {
109 #if PRINT_TOKEN
110 wprint_ctoken(stdout);
111 #endif /* PRINT_TOKEN */
112 if (target != NULL) {
113 fputws(L"<h1>\n", target);
114 }
115 scanner();
116 analyze_TEXT();
117 if (target != NULL) {
118 fputws(L"\n</h1>\n", target);
119 }
120 } else {
121 fprintf(stderr, "%s error\n", __func__);
122 fflush(stderr);
123 exit(EXIT_FAILURE);
124 }
125 #if DEBUG
126 fprintf(stdout, "leaving %s\n", __func__);
127 #endif
128 }
129
130 static void analyze_H2() {
131 #if DEBUG
132 fprintf(stdout, "entering %s\n", __func__);
133 #endif
134 if (tokenType == SSECTION) {
135 #if PRINT_TOKEN
136 wprint_ctoken(stdout);
137 #endif /* PRINT_TOKEN */
138 if (target != NULL) {
139 fputws(L"<h2>\n", target);
140 }
141 scanner();
142 analyze_TEXT();
143 if (target != NULL) {
144 fputws(L"\n</h2>\n", target);
145 }
146 }
147 #if DEBUG
148 fprintf(stdout, "leaving %s\n", __func__);
149 #endif
150 }
151
152 static void analyze_S2() {
153 #if DEBUG
154 fprintf(stdout, "entering %s\n", __func__);
155 #endif
156 if (tokenType == SSECTION) {
157 analyze_H2();
158 analyze_P();
159 analyze_S2();
160 } else if (tokenType != SECTION && tokenType != FIN) {
161 fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
162 fflush(stderr);
163 exit(EXIT_FAILURE);
164 }
165 #if DEBUG
166 fprintf(stdout, "leaving %s\n", __func__);
167 #endif
168 }
169
170 static void analyze_S1() {
171 #if DEBUG
172 fprintf(stdout, "entering %s\n", __func__);
173 #endif
174 if (tokenType == SECTION) {
175 analyze_H1();
176 analyze_P();
177 analyze_S2();
178 analyze_S1();
179 } else if (tokenType != FIN) {
180 fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
181 fflush(stderr);
182 exit(EXIT_FAILURE);
183 }
184 #if DEBUG
185 fprintf(stdout, "leaving %s\n", __func__);
186 #endif
187 }
188
189 static void analyze_BODY() {
190 #if DEBUG
191 fprintf(stdout, "entering %s\n", __func__);
192 #endif
193 analyze_P();
194 analyze_S1();
195 #if DEBUG
196 fprintf(stdout, "leaving %s\n", __func__);
197 #endif
198 }
199
200 void analyze_AXIOME() {
201 #if DEBUG
202 fprintf(stdout, "entering %s\n", __func__);
203 #endif
204 /* print the lexical analysis result */
205 #if PRINT_TOKEN
206 wprint_ctoken(stdout);
207 #endif /* PRINT_TOKEN */
208 if (target != NULL) {
209 fputws(L"<head>\n", target);
210 }
211 analyze_HEAD();
212 if (target != NULL) {
213 fputws(L"\n</head>\n", target);
214 }
215 if (target != NULL) {
216 fputws(L"<body>\n", target);
217 }
218 analyze_BODY();
219 if (target != NULL) {
220 fputws(L"\n<body>\n", target);
221 }
222 if (tokenType != FIN) {
223 fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]);
224 fflush(stderr);
225 exit(EXIT_FAILURE);
226 }
227 fprintf(stdout, "successful syntactic analysis\n");
228 #if DEBUG
229 fprintf(stdout, "leaving %s\n", __func__);
230 #endif
231 fflush(stdout);
232 }