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