#! /usr/bin/perl -w use autodie; use strict; sub parse_chain (\@); sub parse_chain (\@) { my ($words) = @_; my $chain = ""; my $sep = ""; WORD: for (;;) { last WORD unless @$words; my $word = shift @$words; if ($word eq "}" || $word eq "|") { unshift @$words, $word; last WORD; } elsif ($word ne "{") { $chain .= $sep . $word; $sep = ","; } else { my @alts; ALT: for (;;) { push @alts, parse_chain(@$words); my $tok = shift @$words; last ALT if $tok eq "}"; die "bad syntax" unless $tok eq "|"; } $chain .= "(" . join("|", sort { $a cmp $b } @alts) . ")"; } } return $chain; } sub parse_list ($) { my ($path) = @_; open my $f, "<", $path; my @chains; while (<$f>) { my @words = split; push @chains, parse_chain(@words); } $f->close; return join("|", sort { $a cmp $b } @chains); } die "usage: $0 A B" unless @ARGV == 2; my $achain = parse_list $ARGV[0]; my $bchain = parse_list $ARGV[1]; die "$achain /= $bchain" unless $achain eq $bchain;