#!/bin/perl -n -w use strict; # global, but could be attached to the parser or passed to the handlers use vars qw( @results); # we need those to hold info about the parsing # @in_element is a stack of open elements, # the current element is $in_element[-1] use vars qw( @in_element $elt_id $child_seen $child_text); if(m/^\((.*)$/) # element start tag (tag { push @in_element, $1; if( $1 eq 'elt') # elt start tag { $child_seen= 0; } # reset the flag elsif( $1 eq 'child') # child start tag { $child_seen= 1; # set the flag $child_text= ''; # reset the text } } elsif( m/^A([^\s]*) (.*)$/) # attribute Aatt value { # store the id for elt elements $elt_id= $2 if( ($in_element[-1] eq 'elt') && ($1 eq 'id')); } elsif( m/^-(.*)\n/) # text -text { $child_text.= $1 if( $in_element[-1] eq 'child'); } elsif( m/\)(.*)$/) # end tag )tag { if( $1 eq 'elt') { if( $child_seen) { push @results, $child_text; } else { push @results, "missing child for elt $elt_id"; } } } END { print join "\n", @results; print "\n"; }