Commit | Line | Data |
---|---|---|
9ed84d89 JB |
1 | /* Syntactic analyzer */ |
2 | ||
3 | #include <stdbool.h> | |
e70feb8c | 4 | #include <stdlib.h> |
9ed84d89 JB |
5 | |
6 | #include "global_vars.h" | |
e70feb8c | 7 | #include "print_helper.h" |
2d2a5978 JB |
8 | #include "syntactic_analyzer.h" |
9 | #include "lexical_analyzer.h" | |
e70feb8c | 10 | |
15ad4b5a | 11 | #define PRINT_TOKEN 1 |
9ed84d89 JB |
12 | |
13 | /* Syntactic analyzer functions implementation */ | |
14 | ||
15 | enum TokenType tokenType; | |
16 | ||
e70feb8c JB |
17 | static 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 |
42 | static 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); |
4a12843d | 49 | #endif /* PRINT_TOKEN */ |
48e4b4ad JB |
50 | if (target != NULL) { |
51 | fputws(L"<p>\n", target); | |
48e4b4ad | 52 | } |
152ec152 | 53 | scanner(); |
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 |
69 | static void analyze_HEAD() { |
70 | #if DEBUG | |
71 | fprintf(stdout, "entering %s\n", __func__); | |
72 | #endif | |
4a12843d | 73 | if (tokenType == MOTCLE && wcscmp(L">Titre", (const wchar_t*)tokenValue) == 0) { |
48e4b4ad JB |
74 | if (target != NULL) { |
75 | fputws(L"<title>\n", target); | |
48e4b4ad JB |
76 | fputws(L" ", target); |
77 | } | |
15ad4b5a | 78 | scanner(); |
e70feb8c | 79 | analyze_TEXT(); |
48e4b4ad JB |
80 | if (target != NULL) { |
81 | fputws(L"\n</title>\n", target); | |
82 | } | |
4a12843d | 83 | if (tokenType == MOTCLE && wcscmp(L">Auteur", (const wchar_t*)tokenValue) == 0) { |
e70feb8c | 84 | #if PRINT_TOKEN |
15ad4b5a | 85 | wprint_ctoken(stdout); |
e70feb8c | 86 | #endif /* PRINT_TOKEN */ |
15ad4b5a | 87 | fputws(L"<meta name=\"author\" content=\"", target); |
152ec152 | 88 | scanner(); |
e70feb8c | 89 | analyze_TEXT(); |
15ad4b5a | 90 | fputws(L"\">", target); |
e70feb8c JB |
91 | } else if (tokenType != NPARA && tokenType != SECTION && tokenType != FIN) { |
92 | fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]); | |
6c47be32 | 93 | fflush(stderr); |
e70feb8c | 94 | exit(EXIT_FAILURE); |
9ed84d89 | 95 | } |
9ed84d89 | 96 | } |
e70feb8c JB |
97 | #if DEBUG |
98 | fprintf(stdout, "leaving %s\n", __func__); | |
99 | #endif | |
9ed84d89 JB |
100 | } |
101 | ||
e70feb8c JB |
102 | static void analyze_H1() { |
103 | #if DEBUG | |
104 | fprintf(stdout, "entering %s\n", __func__); | |
105 | #endif | |
9ed84d89 | 106 | if (tokenType == SECTION) { |
e70feb8c | 107 | #if PRINT_TOKEN |
15ad4b5a | 108 | wprint_ctoken(stdout); |
e70feb8c | 109 | #endif /* PRINT_TOKEN */ |
48e4b4ad JB |
110 | if (target != NULL) { |
111 | fputws(L"<h1>\n", target); | |
48e4b4ad | 112 | } |
15ad4b5a | 113 | scanner(); |
e70feb8c | 114 | analyze_TEXT(); |
48e4b4ad JB |
115 | if (target != NULL) { |
116 | fputws(L"\n</h1>\n", target); | |
117 | } | |
15ad4b5a JB |
118 | } else { |
119 | fprintf(stderr, "%s error\n", __func__); | |
120 | fflush(stderr); | |
121 | exit(EXIT_FAILURE); | |
9ed84d89 | 122 | } |
e70feb8c JB |
123 | #if DEBUG |
124 | fprintf(stdout, "leaving %s\n", __func__); | |
125 | #endif | |
9ed84d89 JB |
126 | } |
127 | ||
e70feb8c JB |
128 | static void analyze_H2() { |
129 | #if DEBUG | |
130 | fprintf(stdout, "entering %s\n", __func__); | |
131 | #endif | |
9ed84d89 | 132 | if (tokenType == SSECTION) { |
e70feb8c | 133 | #if PRINT_TOKEN |
15ad4b5a | 134 | wprint_ctoken(stdout); |
e70feb8c | 135 | #endif /* PRINT_TOKEN */ |
48e4b4ad JB |
136 | if (target != NULL) { |
137 | fputws(L"<h2>\n", target); | |
48e4b4ad | 138 | } |
15ad4b5a | 139 | scanner(); |
e70feb8c | 140 | analyze_TEXT(); |
48e4b4ad JB |
141 | if (target != NULL) { |
142 | fputws(L"\n</h2>\n", target); | |
143 | } | |
9ed84d89 | 144 | } |
e70feb8c JB |
145 | #if DEBUG |
146 | fprintf(stdout, "leaving %s\n", __func__); | |
147 | #endif | |
9ed84d89 JB |
148 | } |
149 | ||
e70feb8c JB |
150 | static void analyze_S2() { |
151 | #if DEBUG | |
152 | fprintf(stdout, "entering %s\n", __func__); | |
153 | #endif | |
154 | if (tokenType == SSECTION) { | |
155 | analyze_H2(); | |
156 | analyze_P(); | |
157 | analyze_S2(); | |
9ed84d89 | 158 | } else if (tokenType != SECTION && tokenType != FIN) { |
e70feb8c | 159 | fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]); |
6c47be32 | 160 | fflush(stderr); |
e70feb8c | 161 | exit(EXIT_FAILURE); |
9ed84d89 | 162 | } |
e70feb8c JB |
163 | #if DEBUG |
164 | fprintf(stdout, "leaving %s\n", __func__); | |
165 | #endif | |
9ed84d89 JB |
166 | } |
167 | ||
e70feb8c JB |
168 | static void analyze_S1() { |
169 | #if DEBUG | |
170 | fprintf(stdout, "entering %s\n", __func__); | |
171 | #endif | |
172 | if (tokenType == SECTION) { | |
173 | analyze_H1(); | |
174 | analyze_P(); | |
175 | analyze_S2(); | |
176 | analyze_S1(); | |
9ed84d89 | 177 | } else if (tokenType != FIN) { |
e70feb8c | 178 | fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]); |
6c47be32 | 179 | fflush(stderr); |
e70feb8c | 180 | exit(EXIT_FAILURE); |
9ed84d89 | 181 | } |
e70feb8c JB |
182 | #if DEBUG |
183 | fprintf(stdout, "leaving %s\n", __func__); | |
184 | #endif | |
9ed84d89 JB |
185 | } |
186 | ||
e70feb8c JB |
187 | static void analyze_BODY() { |
188 | #if DEBUG | |
189 | fprintf(stdout, "entering %s\n", __func__); | |
190 | #endif | |
191 | analyze_P(); | |
192 | analyze_S1(); | |
193 | #if DEBUG | |
194 | fprintf(stdout, "leaving %s\n", __func__); | |
195 | #endif | |
9ed84d89 JB |
196 | } |
197 | ||
5aa93c8d | 198 | void analyze_AXIOME(void) { |
e70feb8c JB |
199 | #if DEBUG |
200 | fprintf(stdout, "entering %s\n", __func__); | |
201 | #endif | |
e70feb8c JB |
202 | /* print the lexical analysis result */ |
203 | #if PRINT_TOKEN | |
15ad4b5a | 204 | wprint_ctoken(stdout); |
e70feb8c | 205 | #endif /* PRINT_TOKEN */ |
48e4b4ad JB |
206 | if (target != NULL) { |
207 | fputws(L"<head>\n", target); | |
208 | } | |
e70feb8c | 209 | analyze_HEAD(); |
48e4b4ad JB |
210 | if (target != NULL) { |
211 | fputws(L"\n</head>\n", target); | |
212 | } | |
213 | if (target != NULL) { | |
214 | fputws(L"<body>\n", target); | |
215 | } | |
e70feb8c | 216 | analyze_BODY(); |
48e4b4ad JB |
217 | if (target != NULL) { |
218 | fputws(L"\n<body>\n", target); | |
219 | } | |
dfbc1df9 JB |
220 | #if PRINT_TOKEN |
221 | wprint_ctoken(stdout); | |
222 | #endif /* PRINT_TOKEN */ | |
9ed84d89 | 223 | if (tokenType != FIN) { |
e70feb8c | 224 | fprintf(stderr, "%s follows error on %s\n", __func__, tokenTypestr[tokenType]); |
6c47be32 | 225 | fflush(stderr); |
e70feb8c | 226 | exit(EXIT_FAILURE); |
9ed84d89 | 227 | } |
e70feb8c JB |
228 | fprintf(stdout, "successful syntactic analysis\n"); |
229 | #if DEBUG | |
230 | fprintf(stdout, "leaving %s\n", __func__); | |
231 | #endif | |
6c47be32 | 232 | fflush(stdout); |
9ed84d89 | 233 | } |