aboutsummaryrefslogtreecommitdiff
path: root/src/gram.y
blob: 93484a78b64edf4be90e75572ba900d649d823c6 (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
%{
/*
   This file is part of Ellinika project.
   Copyright (C) 2004 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 2 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 Ellinika; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#include "trans.h"
#define obstack_chunk_alloc malloc
#define obstack_chunk_free free
#include <obstack.h>	

struct obstack stk;

struct descr *make_descr(enum descr_type type, u_char *value);

static int _register_topic(void *item, void *data);
static int convert_pos(char *text, int *pos);

%}

%token NODE POS END MEANING ALIAS ANT TOPIC FORMS XREF
%token <string> STRING

%type <num> pos
%type <string> string forms
%type <header> nodehdr alias
%type <descr> descr
%type <list> list aliases descrlist header
%type <item> item

%union {
	int num;
	u_char *string;
	struct header *header;
	struct descr *descr;
	RAD_LIST *list;
	struct gram_item item;
};


%%

input    : list
           {
		   node_list = $1;
	   }
         ;

list     : item
           {
		   $$ = list_create();
		   switch ($1.type) {
		   case item_node:
			   list_append($$, $1.v.node);
			   break;

		   case item_list:
			   list_concat($$, $1.v.list);
			   list_destroy(&$1.v.list, NULL, NULL);
		   }
	   }
         | list item
           {
		   switch ($2.type) {
		   case item_node:
			   list_append($1, $2.v.node);
			   break;

		   case item_list:
			   list_concat($1, $2.v.list);
			   list_destroy(&$2.v.list, NULL, NULL);
		   }
		   $$ = $1;
	   }
         ;

item     : header descrlist end
           {
		   $$.type = item_node;
		   $$.v.node = create_node($1, $2);
	   }
         | TOPIC string list end
           {
		   $$.type = item_list;
		   $$.v.list = $3;
		   list_iterate($3, _register_topic,
				make_descr(descr_topic, $2));
	   }
         ; 

end      : END
         ;

header   : nodehdr
           {
		   $$ = list_create();
		   list_append($$, $1);
	   }
         | nodehdr aliases
           {
		   list_prepend($2, $1);
		   $$ = $2;
	   }
         ;

nodehdr  : NODE string pos forms
           {
		   $$ = emalloc(sizeof(*$$));
		   $$->key = $2;
		   $$->pos = $3;
		   $$->forms = $4;
	   }
         ;

pos      : /* empty */
           {
		   $$ = -1;
	   }
         | POS string
           {
		   if (convert_pos($2, &$$))
			   YYERROR;
	   }
         ;

forms    : /* empty */
           {
		   $$ = NULL;
	   }
         | FORMS string
           {
		   $$ = $2;
	   }
         ;

aliases  : alias
           {
		   $$ = list_create();
		   list_append($$, $1);
	   }
         | aliases alias
           {
		   list_append($1, $2);
		   $$ = $1;
	   }
         ;

alias    : ALIAS string pos forms
           {
		   $$ = emalloc(sizeof(*$$));
		   $$->key = $2;
		   $$->pos = $3;
		   $$->forms = $4;
	   }
         ;

descrlist: descr
           {
		   $$ = list_create();
		   list_append($$, $1);
	   }
         | descrlist descr
           {
		   list_append($1, $2);
		   $$ = $1;
	   }
         ;

descr    : TOPIC string
           {
		   $$ = make_descr(descr_topic, $2);
	   }
         | MEANING string
           {
		   $$ = make_descr(descr_meaning, $2);
	   }
         | ANT string
           {
		   $$ = make_descr(descr_antonym, $2);
	   }
         | XREF string
           {
		   $$ = make_descr(descr_xref, $2);
	   }
         ;

string   : mstring
           {
		   obstack_1grow(&stk, 0);
		   $$ = obstack_finish(&stk);
	   }
         ;

mstring  : STRING
           {
		   obstack_grow(&stk, $1, strlen($1));
	   }
         | mstring STRING
           {
		   obstack_1grow(&stk, '\n');
		   obstack_grow(&stk, $2, strlen($2));
	   }
         ;

%%

yyerror(char *s)
{
	fprintf (stderr, "%s:%d: %s\n",
		 file_name ? file_name : "<stdin>", input_line, s);
}

int
parse(int argc, char **argv)
{
	if (argc)
		open_input(argc, argv);
	obstack_init(&stk);
	return yyparse();
}

struct descr *
make_descr(enum descr_type type, u_char *value)
{
	struct descr *p = emalloc(sizeof(*p));
	p->type = type;
	p->value = value;
	return p;
}

static int
_register_topic(void *item, void *data)
{
	struct node *node = item;
	list_append(node->descr, data);
	return 0;
}


static int
convert_pos(char *text, int *pos)
{
	unsigned long n;
	if (sql_query_n(&n,
			"SELECT id FROM pos WHERE abbr=\"%s\"",
			text)
	    && sql_query_n(&n,
			   "SELECT id FROM pos WHERE abbr_lat=\"%s\"",
			   text)
	    && sql_query_n(&n,
			   "SELECT id FROM pos WHERE name=\"%s\"",
			   text)) {
		yyerror("unknown or misspelled part of speech");
		return 1;
	}
	*pos = n;
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.