summaryrefslogtreecommitdiff
path: root/extools/sg_lexer.l
blob: 9051ba514e0f028b3fb8ed5463ab5cdd76e70885 (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
%{
#include "rule_lexer.h"

#include <string>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cassert>
#include "tdict.h"
#include "fdict.h"
#include "trule.h"
#include "striped_grammar.h"

int lex_line = 0;
int read_contexts = 0;
std::istream* sglex_stream = NULL;
StripedGrammarLexer::GrammarCallback grammar_callback = NULL;
StripedGrammarLexer::ContextCallback context_callback = NULL;
void* grammar_callback_extra = NULL;
void* context_callback_extra = NULL;

#undef YY_INPUT
#define YY_INPUT(buf, result, max_size) (result = sglex_stream->read(buf, max_size).gcount())

#define YY_SKIP_YYWRAP 1
int num_rules = 0;
int yywrap() { return 1; }
bool fl = true;
#define MAX_TOKEN_SIZE 255
std::string sglex_tmp_token(MAX_TOKEN_SIZE, '\0');

#define MAX_RULE_SIZE 48
WordID sglex_src_rhs[MAX_RULE_SIZE];
WordID sglex_trg_rhs[MAX_RULE_SIZE];
int sglex_src_rhs_size;
int sglex_trg_rhs_size;
WordID sglex_lhs;
int sglex_src_arity;
int sglex_trg_arity;

#define MAX_FEATS 100
int sglex_feat_ids[MAX_FEATS];
double sglex_feat_vals[MAX_FEATS];
int sglex_num_feats;

#define MAX_ARITY 20
int sglex_nt_sanity[MAX_ARITY];
int sglex_src_nts[MAX_ARITY];
float sglex_nt_size_means[MAX_ARITY];
float sglex_nt_size_vars[MAX_ARITY];

std::vector<WordID> cur_src_rhs;
std::vector<WordID> cur_trg_rhs;
ID2RuleStatistics cur_options;
RuleStatistics* cur_stats = NULL;
int sglex_cur_fid = 0;

static void sanity_check_trg_index(int index) {
  if (index > sglex_src_arity) {
    std::cerr << "Target index " << index << " exceeds source arity " << sglex_src_arity << std::endl;
    abort();
  }
  int& flag = sglex_nt_sanity[index - 1];
  if (flag) {
    std::cerr << "Target index " << index << " used multiple times!" << std::endl;
    abort();
  }
  flag = 1;
}

static void sglex_reset() {
  sglex_src_arity = 0;
  sglex_trg_arity = 0;
  sglex_num_feats = 0;
  sglex_src_rhs_size = 0;
  sglex_trg_rhs_size = 0;
}

%}

REAL [\-+]?[0-9]+(\.[0-9]*([eE][-+]*[0-9]+)?)?|inf|[\-+]inf
NT [\-#$A-Z_:=.",\\][\-#$".A-Z+/=_0-9!:@\\]*
ALIGN [0-9]+-[0-9]+

%x LHS_END SRC TRG FEATS FEATVAL ALIGNS
%%

<INITIAL>[ ]	;
<INITIAL>[\t]	{
		if (read_contexts) {
			cur_options.clear();
			BEGIN(TRG);
		} else {
			std::cerr << "Unexpected tab while reading striped grammar\n";
			exit(1);
		}
		}

<INITIAL>\[{NT}\]   {
		if (read_contexts) {
			sglex_tmp_token.assign(yytext, yyleng);
			sglex_src_rhs[sglex_src_rhs_size] = TD::Convert(sglex_tmp_token);
			++sglex_src_rhs_size;
		} else {
			sglex_tmp_token.assign(yytext + 1, yyleng - 2);
			sglex_lhs = -TD::Convert(sglex_tmp_token);
			// std::cerr << sglex_tmp_token << "\n";
  			BEGIN(LHS_END);
			}
		}

<INITIAL>[^ \t]+ {
		if (read_contexts) {
			// std::cerr << "Context: " << yytext << std::endl;
			sglex_tmp_token.assign(yytext, yyleng);
			sglex_src_rhs[sglex_src_rhs_size] = TD::Convert(sglex_tmp_token);
			++sglex_src_rhs_size;
		} else {
			std::cerr << "Unexpected input: " << yytext << " when NT expected\n";
			exit(1);
		}
		}

<SRC>\[{NT}\]   {
		sglex_tmp_token.assign(yytext + 1, yyleng - 2);
		sglex_src_nts[sglex_src_arity] = sglex_src_rhs[sglex_src_rhs_size] = -TD::Convert(sglex_tmp_token);
		++sglex_src_arity;
		++sglex_src_rhs_size;
		}

<LHS_END>[ ] { ; }
<LHS_END>\|\|\|	{
		sglex_reset();
		BEGIN(SRC);
		}

<LHS_END>.	{
		std::cerr << "Line " << lex_line << ": unexpected input in LHS: " << yytext << std::endl;
		exit(1);
		}


<SRC>\[{NT},[1-9][0-9]?\]   {
		int index = yytext[yyleng - 2] - '0';
		if (yytext[yyleng - 3] == ',') {
		  sglex_tmp_token.assign(yytext + 1, yyleng - 4);
		} else {
		  sglex_tmp_token.assign(yytext + 1, yyleng - 5);
		  index += 10 * (yytext[yyleng - 3] - '0');
		}
		if ((sglex_src_arity+1) != index) {
			std::cerr << "Src indices must go in order: expected " << sglex_src_arity << " but got " << index << std::endl;
			abort();
		}
		sglex_src_nts[sglex_src_arity] = sglex_src_rhs[sglex_src_rhs_size] = -TD::Convert(sglex_tmp_token);
		++sglex_src_rhs_size;
		++sglex_src_arity;
		}

<SRC>[^ \t]+	{ 
		sglex_tmp_token.assign(yytext, yyleng);
		sglex_src_rhs[sglex_src_rhs_size] = TD::Convert(sglex_tmp_token);
		++sglex_src_rhs_size;
		}
<SRC>[ ]	{ ; }
<SRC>\t		{
		//std::cerr << "LHS=" << TD::Convert(-sglex_lhs) << " ";
		//std::cerr << "  src_size: " << sglex_src_rhs_size << std::endl;
		//std::cerr << "  src_arity: " << sglex_src_arity << std::endl;
		cur_options.clear();
		memset(sglex_nt_sanity, 0, sglex_src_arity * sizeof(int));
		sglex_trg_rhs_size = 0;
		BEGIN(TRG);
		}

<TRG>\[[1-9][0-9]?\]   {
		if (read_contexts) {
			sglex_tmp_token.assign(yytext, yyleng);
			sglex_trg_rhs[sglex_trg_rhs_size] = TD::Convert(sglex_tmp_token);
			++sglex_trg_rhs_size;
		} else {
			int index = yytext[yyleng - 2] - '0';
			if (yyleng == 4) {
			  index += 10 * (yytext[yyleng - 3] - '0');
			}
			++sglex_trg_arity;
			sanity_check_trg_index(index);
			sglex_trg_rhs[sglex_trg_rhs_size] = 1 - index;
			++sglex_trg_rhs_size;
		}
}

<TRG>\|\|\|	{
		assert(sglex_trg_rhs_size > 0);
		cur_trg_rhs.resize(sglex_trg_rhs_size);
		for (int i = 0; i < sglex_trg_rhs_size; ++i)
			cur_trg_rhs[i] = sglex_trg_rhs[i];
		cur_stats = &cur_options[cur_trg_rhs];
		BEGIN(FEATS);
		}

<TRG>[^ ]+	{
		sglex_tmp_token.assign(yytext, yyleng);
		sglex_trg_rhs[sglex_trg_rhs_size] = TD::Convert(sglex_tmp_token);
		
		++sglex_trg_rhs_size;
		}
<TRG>[ ]+	{ ; }

<FEATS>\n	{
		assert(sglex_src_rhs_size > 0);
		cur_src_rhs.resize(sglex_src_rhs_size);
		for (int i = 0; i < sglex_src_rhs_size; ++i)
			cur_src_rhs[i] = sglex_src_rhs[i];
		if (read_contexts) {
			context_callback(cur_src_rhs, cur_options, context_callback_extra);
		} else {
			assert(sglex_lhs < 0);
			grammar_callback(sglex_lhs, cur_src_rhs, cur_options, grammar_callback_extra);
		}
		cur_options.clear();
		sglex_reset();
		BEGIN(INITIAL);
		}
<FEATS>[ ]+	{ ; }
<FEATS>\|\|\|	{
		memset(sglex_nt_sanity, 0, sglex_src_arity * sizeof(int));
		sglex_trg_rhs_size = 0;
		BEGIN(TRG);
		}
<FEATS>[A-Z][A-Z_0-9]*=	{
		// std::cerr << "FV: " << yytext << std::endl;
		sglex_tmp_token.assign(yytext, yyleng - 1);
		sglex_cur_fid = FD::Convert(sglex_tmp_token);
		static const int Afid = FD::Convert("A");
		if (sglex_cur_fid == Afid) {
			BEGIN(ALIGNS);
		} else {
			BEGIN(FEATVAL);
		}
		}
<FEATVAL>{REAL}	{
		// std::cerr << "Feature val input: " << yytext << std::endl;
		cur_stats->counts[sglex_cur_fid] += strtod(yytext, NULL);
		BEGIN(FEATS);
		}
<FEATVAL>.	{
		std::cerr << "Feature val unexpected input: " << yytext << std::endl;
		exit(1);
		}
<FEATS>.	{
		std::cerr << "Features unexpected input: " << yytext << std::endl;
		exit(1);
		}
<ALIGNS>{ALIGN}(,{ALIGN})*	{
		assert(cur_stats->aligns.empty());
		int i = 0;
		while(i < yyleng) {
			short a = 0;
			short b = 0;
			while (yytext[i] != '-') { a *= 10; a += yytext[i] - '0'; ++i; }
			++i;
			while (yytext[i] != ',' && i < yyleng) { b *= 10; b += yytext[i] - '0'; ++i; }
			++i;
			cur_stats->aligns.push_back(std::make_pair(a,b));
		}
		BEGIN(FEATS);
		}
<ALIGNS>.	{
		std::cerr << "Aligns unexpected input: " << yytext << std::endl;
		exit(1);
		}
%%

#include "filelib.h"

void StripedGrammarLexer::ReadStripedGrammar(std::istream* in, GrammarCallback func, void* extra) {
  read_contexts = 0;
  lex_line = 1;
  sglex_stream = in;
  grammar_callback_extra = extra;
  grammar_callback = func;
  yylex();
}

void StripedGrammarLexer::ReadContexts(std::istream* in, ContextCallback func, void* extra) {
  read_contexts = 1;
  lex_line = 1;
  sglex_stream = in;
  context_callback_extra = extra;
  context_callback = func;
  yylex();
}