aboutsummaryrefslogtreecommitdiff
path: root/iburg/briggs/icg-grammars/shardgrammar.cc
blob: d69d840e076b2a232ec199f84cf8a99d16fb11fb (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
// {([
//
// Copyright (c) 2008 Google Inc. All rights reserved.
//
// rrh@google.com 08/08/2008
//
// $Header: $
//

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>

int string_distance(const char *a, const char *b)
{
  const size_t n = strlen(a);
  const size_t m = strlen(b);
  int *c = new int[(n+1)*(m+1)];
#define access(i, j) ((i)*(m+1)+(j))
  for (size_t i = 0; i <= n; i++) {
    c[access(i,0)] = i;
  }
  for (size_t j = 0; j <= m; j++) {
    c[access(0,j)] = j;
  }
  for (size_t i = 1; i <= n; i++) {
    for (size_t j = 1; j <= m; j++) {
      const int x = c[access(i-1,j-0)] + 1;
      const int y = c[access(i-0,j-1)] + 1;
	    int z = c[access(i-1,j-1)] + 0;
      if (a[i-1] != b[j-1]) {
	z += 1;
      }
      if (x <= y && x <= z) {
	c[access(i,j)] = x;
      } else if (y <= x && y <= z) {
	c[access(i,j)] = y;
      } else {
	c[access(i,j)] = z;
      }
    }
  }
  const int back = c[access(n, m)];
  delete [] c;
  return back;
}

double string_similarity (const char *a, const char *b, int &edit_distance)
{
  const size_t totalLength = strlen(a) + strlen(b);
  edit_distance = string_distance(a, b);
  double m1 = (1.0*totalLength - 2.0*edit_distance)/totalLength;
  if (m1 < 0) {
    m1 = 0.0;
  }
  return m1;
}

//
// Test jig
//
void string_distance_test(const char *a, const char *b)
{
  fprintf(stdout, "string_distance %10s %10s ==> %2d\n",
    a, b, string_distance(a, b)
  );
}

void string_distance_unittest(void)
{
  string_distance_test("abc", "abc");
  string_distance_test("ab", "abc");
  string_distance_test("bc", "abc");
  string_distance_test("Abc", "abc");
  string_distance_test("abC", "abc");
  string_distance_test("AbC", "abc");
}

void append(const char *&dst, const char *s1, const char *s2)
{
  const size_t nlg = 0
    + (dst ? ::strlen(dst) : 0)
    + (s1 ? ::strlen(s1) : 0)
    + (s2 ? ::strlen(s2) : 0)
  ;
  char *back = new char[nlg+1];
  ::snprintf(back, nlg+1, "%s%s%s",
    dst ? dst : "",
    s1 ? s1 : "",
    s2 ? s2 : ""
  );
  delete [] dst; dst = 0;
  dst = back;
}

const char *stringtrim(const char *src)
{
  if (src == 0) {
    return 0;
  }
  const char *first = src;
  while (*first && isspace(*first)) {
    first += 1;
  }
  const char *last = src + strlen(src) - 1;
  while (last >= first && isspace(*last)) {
    last -= 1;
  }
  const size_t lg = last - first + 1;
  char *back = new char[lg+1];
  ::strncpy(back, first, lg);
  back[lg] = 0;
  return back;
}

//
// Read the grammar files enumerated on the argument line.
// Output one file for each block of rules.
// The output files end up in the directory "./tmp"
//

int global_block_number = 0;

class Key {
public:
  const char *ifname;
  int lineno_s;
  int lineno_e;
};

class KeyRecord {
public:
  Key key;
  const char *ofname;		// output file name

  const char *rules;		// rules (grammar)
  const char *trimrules;	// string trimmed rules (grammar)
  size_t lgrules;		// length of rules, bytes

  const char *sems;		// semantic actions (block-labeled C code)
  const char *trimsems;		// string trimmed semantic actions
  size_t lgsems;		// length of semantic actions, bytes

  KeyRecord *best_other;	//              best other KeyRecord
  double best_similarity;	// similarty of best other KeyRecord
  int best_edit_distance;	// edit dist of best other KeyRecord

public:
  KeyRecord(void);
};

KeyRecord::KeyRecord(void) {
  ofname = 0;
  rules = 0;
  trimrules = 0;
  sems = 0;
  trimsems = 0;
  lgrules = 0;
  lgsems = 0;
  best_other = 0;
  best_similarity = 0.0;
  best_edit_distance = -1;
}

void print_singleton(FILE *fp, const KeyRecord *rp)
{
  const Key &k = rp->key;
  fprintf(fp, " {%s %4d %4d}", k.ifname, k.lineno_s, k.lineno_e);
}

void print_pairs(FILE *fp, const KeyRecord *ri, const KeyRecord *rj)
{
  if (ri && rj) {
    fprintf(fp, " tkdiff %4s %4s ", ri->ofname, rj->ofname);
  }
  if (ri) {
    print_singleton(fp, ri);
  }
  fprintf(fp, " vs");
  if (rj) {
    print_singleton(fp, rj);
  }
}

//
// Obsolete: this was pre-pythonization of the plug spec files
//
int read_grammar_file(const char *ifname)
{
  int lineno = 0;
  extern int global_block_number;
  global_block_number += 1;
  FILE *ifd = ::fopen(ifname, "r");
  if (ifd == NULL) {
    return -1;
  }

  enum State {
    State_none,
    State_rules
  };
  State state = State_none;

  int lineno_s = -1;
  int lineno_e = -1;

  const char *rules = 0;	// all syntactic rules in this block
  const char *trimrules = 0;	// all syntactic rules in this block, trimmed
  int lgrules = 0;		// number of lines in rules

  const char *sems = 0;		// all semantic actions in this block
  const char *trimsems = 0;	// all semantic actions in this block, trimmed
  int lgsems  = 0;

  enum {
    NRULES=600
  };
  KeyRecord *ruleinfo[NRULES];

  for (int i = 0; i < NRULES; i++) {
    ruleinfo[i] = 0;
  }

  int nruleinfo = 0;
  int in_shard_region = 0;

  char line[8*BUFSIZ];
  while (::fgets(line, sizeof(line), ifd) == line) {
    lineno += 1;
    if (!in_shard_region) {
      if (::strncmp(line, "<SHARD>", 7) == 0) {
        in_shard_region = 1;
      }
      continue;
    }
    //
    // look for some kind of syntactic rule
    //
    if (isalpha(line[0]) && ::index(line, ':') > line+0) {
      if (state != State_rules) {
        lineno_s = lineno;
	state = State_rules;
	rules = 0;
	trimrules = 0;
	sems = 0;
	trimsems = 0;
      }
      append(trimrules, stringtrim(line), "\n");
      append(rules, line, 0);
      lgrules += 1;
      continue;
    }

    //
    // some intermediate line, between rule definitions
    // and the closing syntax of the rule block
    //
    if (state == State_rules) {
      append(trimsems, stringtrim(line), "\n");
      append(sems, line, 0);
      lgsems +=1;
    }

    //
    // end of block
    //
    if (::strstr(line, "\175;")) {
      lineno_e = lineno;
      char ofname[BUFSIZ];
      ::snprintf(ofname, sizeof(ofname), "tmp/%03d", global_block_number);
      FILE *ofd = ::fopen(ofname, "w");
      if (ofd == NULL) {
        continue;
      }
      fprintf(ofd, \
	"// file %4s contains %3d+%3d lines from %5d .. %5d of file %s\n",
	ofname,
	lgrules,
	lgsems,
	lineno_s, lineno_e,
	ifname
      );
      fprintf(ofd, "%s", rules);
      fprintf(ofd, "%s", sems);
      ::fclose(ofd);

      KeyRecord *p = new KeyRecord();
      ruleinfo[nruleinfo] = p;
      nruleinfo += 1;

      if (nruleinfo >= NRULES) {
        fprintf(stderr, "too many rules, max of %d\n", NRULES);
	exit(1);
      }
      p->ofname = strdup(ofname);

      p->key.ifname = ifname;
      p->key.lineno_s = lineno_s;
      p->key.lineno_e = lineno_e;

      p->trimsems = trimsems;
      p->sems = sems;
      p->lgsems = lgsems;

      p->trimrules = trimrules;
      p->rules = rules;
      p->lgrules = lgrules;

      rules = 0;
      trimrules = 0;
      lgrules = 0;

      sems = 0;
      trimsems = 0;
      lgsems = 0;

      global_block_number += 1;
      state = State_none;
    }
  }
  ::fclose(ifd); ifd = 0;

  const int nkey = nruleinfo;

  //
  // Compare 2 blocks of semantic actions that
  // are within $offset line numbers of each other.
  // use the Levenshtein distance (minimal number of edits)
  //
  for (int i = 0; i < nkey; i++) {
    KeyRecord *ri = ruleinfo[i];
    if (ri->best_other) continue;
    fprintf(stdout, "\n");		// block spacing
    for (int j = 0; j < nkey; j++) {
      KeyRecord *rj = ruleinfo[j];
      if (rj->best_other) continue;

      int edit_distance = 0;
      const double similarity =
	string_similarity(ri->trimsems, rj->trimsems, edit_distance);
      if (edit_distance < 30) {
        rj->best_other = ri;
        rj->best_edit_distance = edit_distance;
        rj->best_similarity = similarity;
	if (ri != rj) {
	  fprintf(stdout, "edit= %4d sim= %8.4f",
	    edit_distance, similarity
	  );
	  print_pairs(stdout, ri, rj);
	  fprintf(stdout, "\n");
	}
      }

    }
  }

  return 0;
}

struct simcarrier {
  double similarity;
  int i;
  int j;
};

int simcarriersorter(const void *vp1, const void *vp2)
{
  const simcarrier *p1 = (const simcarrier *)vp1;
  const simcarrier *p2 = (const simcarrier *)vp2;
  const int reverse = -1;
  if (p1->similarity < p2->similarity) {
    return reverse * -1;
  }
  if (p1->similarity > p2->similarity) {
    return  reverse * 1;
  }
  return 0;
}

int abs(const int x)
{
  return x < 0 ? -x : x;
}
void print_info(FILE *fp, const simcarrier *info, int index,
  size_t *lengths, const char **names)
{
  const int i = info[index].i;
  const int j = info[index].j;
  fprintf(stdout, "%5.3f  %5d %5d    tkdiff -w %30s %30s\n",
    info[index].similarity,
    lengths[i], lengths[j],
    names[i], names[j]
  );
}

int main(int argc, const char *argv[])
{
  if (0) {
    string_distance_unittest();
  }
  if (0) {
    ::mkdir("./tmp", 0777);
    for (int i = 1; i < argc; i++) {
      read_grammar_file(argv[i]);
    }
  }
  if (1) {
    const char **contents = new const char *[argc];
    size_t *lengths = new size_t[argc];
    for (int i = 1; i < argc; i++) {
      contents[i] = 0;
      lengths[i] = 0;
    }
    for (int i = 1; i < argc; i++) {
      FILE *ifd = ::fopen(argv[i], "r");
      if (ifd) {
	struct stat sbuf;
	::stat(argv[i], &sbuf);
	lengths[i] = sbuf.st_size;
	contents[i] = new char[lengths[i]];
	char *last = (char *)contents[i];
        char line[8*BUFSIZ];
	while (::fgets(line, sizeof(line), ifd) == line) {
	  const char *trimmed = stringtrim(line);
	  strcat(last, trimmed);
	  last += strlen(last);
	}
      }
      ::fclose(ifd);
    }

    simcarrier *info = new simcarrier[argc*argc];
    for (int i = 1; i < argc; i++) {
      for (int j = 1; j < argc; j++) {
	const int index = i*argc+j;
        info[index].i = i;
        info[index].j = j;
        info[index].similarity = 0.0;
      }
    }
    for (int i = 1; i < argc; i++) {
      // fprintf(stdout, "progress: %4d/%-4d\n", i, argc);
      if (contents[i] == 0) continue;
      const bool isdivi = strstr(contents[i], "MOD") || strstr(contents[i], "DIV");
      int nhit = 0;
      for (int j = i+1; j < argc; j++) {
	if (contents[j] == 0) continue;
	const bool isdivj = strstr(contents[j], "MOD") || strstr(contents[j], "DIV");
	if (abs(lengths[i] - lengths[j]) > 0.20*lengths[j]) {
	  continue;
	}
	const int index = i*argc+j;
	int edit_distance;
	info[index].similarity =
	  string_similarity(contents[i], contents[j], edit_distance);
	if (0
	|| (isdivi && isdivj)
	|| info[index].similarity > 0.80) {
	  print_info(stdout, info, index, lengths, argv);
	  nhit += 1;
	}
      }
      if (nhit) {
        fprintf(stdout, "\n");
	fflush(stdout);
      }
    }

    if (0) {
      fprintf(stdout, "# --- Sorted Similarity Summary ---\n");
      ::qsort(info, argc*argc, sizeof(info[0]), simcarriersorter);
      int nhit = 0;
      for (int index = 0; index < argc*argc; index++) {
	if (info[index].similarity > 0) {
	  if (info[index].similarity < 1.00) {
	    print_info(stdout, info, index, lengths, argv);
	    nhit += 1;
	    if (nhit > 20) {
	      break;
	    }
	  }
	}
      }
    }

  }

  exit(0);
}

// })]