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