in reply to (La)TeX brace counter
#!/usr/bin/perl -w use strict; my %x = (')'=>'(','}'=>'{',']'=>'['); my @braces = (); while (<>) { s/\\(\(|\)|\{|\}|\[|\])//g; # yank out escaped braces for (split //, $_) { # split each line into chars push(@braces, $_), next if (/\(|\{|\[/); # push if left brace /(\)|\}|\])/ and # if right brace (@braces > 0 # and left braces on stack ? $braces[-1] eq $x{$1} # and right brace matches left ? pop @braces # pop the pair : (print("unmatched '$braces[-1]' before '$_' on line $."), e +xit) # saw a left without correct right : (print("no match for '$_' on line $."), exit) # saw a right + with no stack ); } } print "passed!\n";
This is barely tested, so nobody go using it anywhere. Granted, this doesn't take quoted braces into account, but that's a whole different story.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: (La)TeX brace counter (athomason)
by Strahinja (Initiate) on Jul 24, 2000 at 21:41 UTC |