#!/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};
		}
		if ($id == 8) {
			$final{$id} = $final{42} . '+';
			delete $rules{$id};
			next RULE;
		}

		if ($id == 11) {
			$final{$id} = '(?:' . join('|',
				$final{42} . $final{31},
				$final{42} x 2 . $final{31} x 2,
				$final{42} x 3 . $final{31} x 3,
				$final{42} x 4 . $final{31} x 4,
				$final{42} x 5 . $final{31} x 5,
				$final{42} x 6 . $final{31} x 6,
				$final{42} x 7 . $final{31} x 7,
				$final{42} x 8 . $final{31} x 8,
				$final{42} x 9 . $final{31} x 9,
				$final{42} x 10 . $final{31} x 10,
				$final{42} x 11 . $final{31} x 11,
				$final{42} x 12 . $final{31} x 12,
				) . ')';
			delete $rules{$id};
			next RULE;
		}
		
		$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";
