[Israel.pm] Regex that matches a C-style function definition.

Gaal Yahas gaal at forum2.org
Sun Nov 11 04:47:36 PST 2007


It's a pretty tough job, and I'm not aware of a module that packages a
solution for it. You might be able to lift some of the grammar in
Inline::C::ParseRecDescent (a peek at the newer Inline::C::ParseRegExp
suggests it may not be as easily persuaded to help you much).

You have to consider things like the C preprocessor and, if you're
going to parse things out in the wild, old-style parameter
declarations.

Now for the hacky tip :-)

If this is on a well-behaved set of source files, you may be able to
get away with something as simple as grabbing everything from the
signature to the nearest closing brace that's flush to the left on a
line of its own.

($body) = /
    void \s+
    f \s+
    \( \s*
    \) \s*                # signature
    \{ \s*                # opening brace
      (.*?) \s+         # body
    ^\}                   # closing brace
  /msx;

On Nov 11, 2007 2:17 PM, Meron Cohen <Meron.Cohen at ecitele.com> wrote:
> Hello,
>
> I'm trying to parse ".c" file and extract a given function's definition.
>
> Example:
>
> void f ()
> {
>     /* definition of f () */
> }
>
> I know that the name of the function is "f" and what I want is the
> definition of f (), inside the "{" and "}".
> Could you tell me if there is a CPAN module that already does it, or
> give me a Regex that does it.
>
> 10x,
> Meron
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/



More information about the Perl mailing list