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