chiark / gitweb /
Makefile, chain.c: Use Xyla found using `pkg-config'.
[wordchain] / chkref
1 #! /usr/bin/perl -w
2
3 use autodie;
4 use strict;
5
6 sub parse_chain (\@);
7 sub parse_chain (\@) {
8   my ($words) = @_;
9
10   my $chain = ""; my $sep = "";
11   WORD: for (;;) {
12     last WORD unless @$words;
13     my $word = shift @$words;
14     if ($word eq "}" || $word eq "|") {
15       unshift @$words, $word; last WORD;
16     } elsif ($word ne "{") {
17       $chain .= $sep . $word; $sep = ",";
18     } else {
19       my @alts;
20       ALT: for (;;) {
21         push @alts, parse_chain(@$words);
22         my $tok = shift @$words;
23         last ALT if $tok eq "}";
24         die "bad syntax" unless $tok eq "|";
25       }
26       $chain .= "(" . join("|", sort { $a cmp $b } @alts) . ")";
27     }
28   }
29   return $chain;
30 }
31
32 sub parse_list ($) {
33   my ($path) = @_;
34
35   open my $f, "<", $path;
36   my @chains;
37   while (<$f>) {
38     my @words = split;
39     push @chains, parse_chain(@words);
40   }
41   $f->close;
42   return join("|", sort { $a cmp $b } @chains);
43 }
44
45 die "usage: $0 A B" unless @ARGV == 2;
46
47 my $achain = parse_list $ARGV[0];
48 my $bchain = parse_list $ARGV[1];
49 die "$achain /= $bchain" unless $achain eq $bchain;