#!/usr/bin/perl -w

use strict;

my %rules;
my %subrules;
my %final;

while (<>) {
	chomp;
	last if /^$/;
	my ($id, $rest) = /\A(\d+): (.*)\z/;
	if ($rest =~ /"(.)"/) {
		$final{$id} = $1;
		next;
	}
	my @alts;
	my @subalts;
	for my $seq (split /\|/, $rest) {
		push @alts, [ $seq =~ /(\d+)/g ];
		push @subalts, ($seq =~ /(\d+)/g);
	}
	$rules{$id} = \@alts;
	$subrules{$id} = \@subalts;
}

while (keys %rules) {
	RULE:
	for my $id (keys %rules) {
		for my $subr (@{ $subrules{$id} }) {
			next RULE if !defined $final{$subr};
		}
		$final{$id} = '(?:(?:' . join(')|(?:', map {
			join('', map { $final{$_} } @$_)
			} @{ $rules{$id} }) .'))';
		print "\$final{$id} = $final{$id}\n";
		delete $rules{$id};
	}
}

my $re = '\A'.$final{0}.'\z';

my $count = 0;
while (<>) {
	chomp;
	$count++ if ($_ =~ /$re/);
}

print "$count matched\n";
