aboutsummaryrefslogtreecommitdiff
path: root/src/ellinika/phoneme.y
blob: 4d9afa28df378e8a0207619df0962e516715d502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* This file is part of Ellinika project.
   Copyright (C) 2011 Sergey Poznyakoff

   Ellinika is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   Ellinika is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
%{
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>	
#include "utf8.h"
#include "elmorph.h"

static struct phoneme *phoneme_base;
static size_t phoneme_max;
static size_t phoneme_count;
static int error_state;

#define PHONEME_MAP_INITIAL_ALLOC 16

static void
phoneme_append(struct phoneme *phoneme)
{
	if (error_state)
		return;
	
	if (phoneme_max == phoneme_count) {
		struct phoneme *np;
		size_t nsize;

		if (!phoneme_max) 
			nsize = PHONEME_MAP_INITIAL_ALLOC;
		else {
			nsize = 2 * phoneme_max;
			if (nsize < phoneme_max) {
				error_state = ENOMEM;
				return;
			}
		}
		np = realloc(phoneme_base, nsize * sizeof(phoneme_base[0]));
		if (!np) {
			error_state = ENOMEM;
			return;
		}
		phoneme_max = nsize;
		phoneme_base = np;
	}
	phoneme_base[phoneme_count++] = *phoneme;
}
	
#define DIPHTHONG(a,b,pc,fl) do {					\
		(a).count = 2;						\
                (a).code = pc;						\
		(a).flags = (fl) | CHF_DIPHTHONG |			\
			(((a.flags) | (b).flags) & CHF_ACCENT_MASK);	\
	} while (0)

%}
%union {
	struct phoneme phoneme;
};

%token <phoneme> LETTER_A            1
%token <phoneme> LETTER_A_ACC        2
%token <phoneme> LETTER_B            3
%token <phoneme> LETTER_G            4
%token <phoneme> LETTER_D            5
%token <phoneme> LETTER_E            6
%token <phoneme> LETTER_E_ACC        7
%token <phoneme> LETTER_Z            8
%token <phoneme> LETTER_H            9
%token <phoneme> LETTER_H_ACC       10
%token <phoneme> LETTER_TH          11
%token <phoneme> LETTER_I           12 
%token <phoneme> LETTER_I_ACC       13
%token <phoneme> LETTER_I_TREMA     14
%token <phoneme> LETTER_I_TREMA_ACC 15
%token <phoneme> LETTER_K           16
%token <phoneme> LETTER_L           17
%token <phoneme> LETTER_M           18
%token <phoneme> LETTER_N           19
%token <phoneme> LETTER_KS          20
%token <phoneme> LETTER_OMICRON     21
%token <phoneme> LETTER_OMICRON_ACC 22
%token <phoneme> LETTER_P           23
%token <phoneme> LETTER_R           24
%token <phoneme> LETTER_S           25
%token <phoneme> LETTER_T           26
%token <phoneme> LETTER_Y           27
%token <phoneme> LETTER_Y_ACC       28
%token <phoneme> LETTER_Y_TREMA     29
%token <phoneme> LETTER_Y_TREMA_ACC 30
%token <phoneme> LETTER_F           31
%token <phoneme> LETTER_X           32 
%token <phoneme> LETTER_PS          33
%token <phoneme> LETTER_OMEGA       34
%token <phoneme> LETTER_OMEGA_ACC   35

%type <phoneme> monophthong diphthong phoneme

%%
input   : phoneme
          {
		  phoneme_append(&$1);
	  }
        | input phoneme
          {
		  phoneme_append(&$2);
	  }
        ;

phoneme : monophthong
        | diphthong
        ;

monophthong:
          LETTER_A
	| LETTER_A_ACC
        | LETTER_B
        | LETTER_G
        | LETTER_D
        | LETTER_E
	| LETTER_E_ACC
        | LETTER_Z
        | LETTER_H
	| LETTER_H_ACC
        | LETTER_TH
        | LETTER_I
	| LETTER_I_ACC
	| LETTER_I_TREMA
	| LETTER_I_TREMA_ACC
        | LETTER_K
        | LETTER_L
        | LETTER_M
        | LETTER_N
        | LETTER_KS
        | LETTER_OMICRON
	| LETTER_OMICRON_ACC
        | LETTER_P
        | LETTER_R
        | LETTER_S
        | LETTER_T
        | LETTER_Y
	| LETTER_Y_ACC
	| LETTER_Y_TREMA
	| LETTER_Y_TREMA_ACC
        | LETTER_F
        | LETTER_X
        | LETTER_PS
        | LETTER_OMEGA
        | LETTER_OMEGA_ACC
	;
	
diphthong:
          LETTER_A LETTER_I
          {
		  DIPHTHONG($1, $2, PHON_E, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_A LETTER_I_ACC
          {
		  DIPHTHONG($1, $2, PHON_E, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_E LETTER_I
          {
		  DIPHTHONG($1, $2, PHON_I, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_E LETTER_I_ACC
          {
		  DIPHTHONG($1, $2, PHON_I, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_OMICRON LETTER_I
          {
		  DIPHTHONG($1, $2, PHON_I, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_OMICRON LETTER_I_ACC
          {
		  DIPHTHONG($1, $2, PHON_I, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_Y LETTER_I
          {
		  DIPHTHONG($1, $2, PHON_I, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_Y LETTER_I_ACC
          {
		  DIPHTHONG($1, $2, PHON_I, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_OMICRON LETTER_Y
          {
		  DIPHTHONG($1, $2, PHON_U, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_OMICRON LETTER_Y_ACC
          {
		  DIPHTHONG($1, $2, PHON_U, CHF_VOWEL);
		  $$ = $1;
	  }
	| LETTER_M LETTER_P
          {
		  DIPHTHONG($1, $2, PHON_B, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_N LETTER_T
          {
		  DIPHTHONG($1, $2, PHON_D, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_G LETTER_G
          {
		  DIPHTHONG($1, $2, PHON_G, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_G LETTER_K
          {
		  DIPHTHONG($1, $2, PHON_G, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_G LETTER_X
          {
		  DIPHTHONG($1, $2, PHON_G, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_S LETTER_M
          {
		  DIPHTHONG($1, $2, PHON_ZM, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_T LETTER_S
          {
		  DIPHTHONG($1, $2, PHON_TS, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_T LETTER_Z
          {
		  DIPHTHONG($1, $2, PHON_DZ, CHF_CONSONANT);
		  $$ = $1;
	  }
	| LETTER_G LETTER_KS
          {
		  DIPHTHONG($1, $2, PHON_NGZ, CHF_CONSONANT);
		  $$ = $1;
	  }
        | LETTER_A LETTER_Y
          {
		  DIPHTHONG($1, $2, PHON_AV, 0);
		  $$ = $1;
	  }		  
        | LETTER_A LETTER_Y_ACC
          {
		  DIPHTHONG($1, $2, PHON_AV, 0);
		  $$ = $1;
	  }		  
        | LETTER_E LETTER_Y
          {
		  DIPHTHONG($1, $2, PHON_EV, 0);
		  $$ = $1;
	  }		  
        | LETTER_E LETTER_Y_ACC
          {
		  DIPHTHONG($1, $2, PHON_EV, 0);
		  $$ = $1;
	  }		  
        ;

%%

static unsigned *input_base;
static size_t input_len;
static size_t input_pos;

#define ISALPHA(ci) ((ci) && ci->letter)

#define PHONEME_FLAG_MASK \
	(CHF_ACCENT_MASK|CHF_VOWEL|CHF_CONSONANT)

int
yylex()
{
	unsigned c;
	struct char_info_st const *ci;

	do {
		if (input_pos == input_len)
			return 0;
		c = input_base[input_pos++];
		ci = elchr_info(c);
	} while (!ISALPHA(ci));

	yylval.phoneme.code = ci->phoneme;
	yylval.phoneme.start = input_pos - 1;
	yylval.phoneme.count = 1;
	yylval.phoneme.flags = ci->flags & PHONEME_FLAG_MASK;
	return ci->letter;
}

int
yyerror(const char *s)
{
	fprintf(stderr, "\n%s:%d: INTERNAL ERROR: %s\n", __FILE__, __LINE__, s);
	abort();
}

int
phoneme_map(struct phoneme **pph, size_t *plen, unsigned *word, size_t len)
{
	int rc;
	
	input_base = word;
	input_len = len;
	input_pos = 0;
	phoneme_base = NULL;
	phoneme_max = 0;
	phoneme_count = 0;
	error_state = 0;
	rc = yyparse();
	if (rc) {
		free(phoneme_base);
		errno = EINVAL;
		return errno;
	}
	if (error_state) {
		free(phoneme_base);
		errno = error_state;
		return errno;
	}
	if (phoneme_count < phoneme_max)
		phoneme_base =
			realloc(phoneme_base,
				phoneme_count * sizeof(phoneme_base[0]));
	*pph = phoneme_base;
	*plen = phoneme_count;
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.