jeniffer wrote:
>I am writing a perl code to find out the location (line number) of
>ending bracket of a function given its name and start line number .Do i
>just scan character by character (from start line number ) and
>increment a counter when I encounter { decrement the counter by 1 on
>getting } ,and when the counter = 0 ,I take the line number .This would
>free me from parsing the code .Is this method correct?
It's close, but not quite. You'll have to be able to recognize comments,
and strings. You have to skip braces inside those. For the rest, it's
just a matter of balancing the braces.
>or can i do it
>in a better way by regular expression ?
Yeah, doing it character by character is how one would do it in C, but
it most definitely isn't the fastest way in Perl. Scanning for the next
brace with a regex would be lots faster. But likely not better,
resultwise.
At first I'm thinking of using Text::Balanced, it's more or less
designed for things like that. But I remember reading an article on
perl.com, on lexing your data (thus recognizing comments and strings).
That might be of use to you:
Lexing Your Data
<http://www.perl.com/pub/a/2006/01/05/parsing.html>
--
Bart.
|