aboutsummaryrefslogtreecommitdiff
path: root/src/gram.y
blob: c065bcfea7e20f56094719381507520306493e71 (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
%{
#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);
 
%}

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

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

%union {
	u_char *string;
	struct header *header;
	struct descr *descr;
	RAD_LIST *list;
};


%%

input    : list
         ;

list     : node
         | list node
         ;

node     : header descrlist end
           {
		   register_node($1, $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 */
           {
		   $$ = NULL;
	   }
         | POS string
           {
		   $$ = $2;
	   }
         ;

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, input_line, s);
}

int
parse(char *name)
{
	if (name)
		open_input(name);
	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;
}

Return to:

Send suggestions and report system problems to the System administrator.