#!/usr/bin/env tclsh while {[gets stdin line] >= 0} { if {$line eq ""} break if {[scan $line {%d: "%[a-z]"} n c] == 2} { set char($n) $c continue } scan $line %d%n: n length set line [string range $line $length+1 end] ;# +1 for the colon set tmp {} while {[string length $line]} { if {[scan $line %d%n i length] == 2} { lappend tmp $i set line [string range $line $length end] } else { # must be the pipe scan $line %s%n i length set line [string range $line $length end] lappend rule($n) $tmp set tmp {} } } if {[llength $tmp]} {lappend rule($n) $tmp} } proc make_regex {n} { global rule char if {[info exists char($n)]} {return $char($n)} foreach list $rule($n) { set tmp "" foreach i $list { append tmp [make_regex $i] } lappend tmplist $tmp } return "([join $tmplist |])" } set r "^[make_regex 0]$" set n 0 while {[gets stdin line] >= 0} { if {$line eq ""} continue if {[regexp $r $line]} {incr n} } puts $n