#!/usr/bin/perl -wT #-*-cperl-*- use strict; my ( $laststate, $state, $nextstate ); open(SOURCE, "< $ARGV[0]") or die "Couldn't open $ARGV[0] for reading: $!\n"; # There are three possible line beginnings (ignoring whitespace): '(', ':', and ')' # There are three possible line endings (ignoring whitespace): '(', ')', and neither one while () { SWITCH: { # the first line in the file always starts with '(' # no other line will match this /^\(/ && do { $state = $laststate = 0; $nextstate = 1; # elevate the state last SWITCH; }; # lines beginning with ':' always have whitespace before it # these lines either maintain or elevate the state, never lower the state /^\s+:/ && do { $laststate = $state; $state = $nextstate; # here is where we analyse the line endings STATE: { # if the line ends with '(', elevate the state /\($/ && do { $nextstate++; last STATE; }; # if the line ends with ')', maintain the state? /\)$/ && do { last STATE; }; # other line endings elevate the state $nextstate++; } last SWITCH; }; # if the line contains only whitespace and ')', lower the state /^\s+\)$/ && do { $laststate = $state; $state--; $nextstate--; last SWITCH; }; } # analyse our state if ( $state != $laststate ) { # we changed state print "State Change!\t\t"; } print "$laststate\t$state\t$nextstate\n"; } close SOURCE;