Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Newbien: Extract a C function from a file

58 views
Skip to first unread message

jose.luis...@gmail.com

unread,
Sep 29, 2012, 5:56:34 AM9/29/12
to
Hi,

This is the pattern from a C function:

<<snip>>
void foo(....)
{
....
}
<<snip>>

Given the function name, is there an easy way to extract the function text from a C file in Perl?

Thanks in advance,
Jose Luis


Josef Moellers

unread,
Sep 29, 2012, 10:30:23 AM9/29/12
to
On 09/29/2012 11:56 AM, jose.luis...@gmail.com wrote:
> Hi,
>
> This is the pattern from a C function:
>
> <<snip>>
> void foo(....)
> {
> ....
> }
> <<snip>>
>
> Given the function name, is there an easy way to extract the function text from a C file in Perl?

In general: No.
You need a /scanner/parser for C for that.

Josef

jose.luis...@gmail.com

unread,
Sep 29, 2012, 3:27:15 PM9/29/12
to
El sábado, 29 de septiembre de 2012 16:30:24 UTC+2, Josef Moellers escribió:
> On 09/29/2012 11:56 AM, jose.luis.fdez.diaz at ..... gmail.com wrote:
>
> > Hi,
>
> >
>
> > This is the pattern from a C function:
>
> >
>
> > <<snip>>
>
> > void foo(....)
>
> > {
>
> > ....
>
> > }
>
> > <<snip>>
>
> >
>
> > Given the function name, is there an easy way to extract the function text from a C file in Perl?
>
>
>
> In general: No.
>
> You need a /scanner/parser for C for that.
>
>
>
> Josef

Thanks for your answer Josef.

Perhaps this is possible. Given this pattern:

<<snip>>
{
{
....
}

{
....

{
...
}

....

}
}
<<snip>>

Is there and easy way to get the text between the external braces?


Regards,
Jose Luis

Ben Morrow

unread,
Sep 29, 2012, 6:00:29 PM9/29/12
to

Quoth jose.luis...@gmail.com:
>
> Perhaps this is possible. Given this pattern:
>
> <<snip>>
> {
> {
> ....
> }
>
> {
> ....
>
> {
> ...
> }
>
> ....
>
> }
> }
> <<snip>>
>
> Is there and easy way to get the text between the external braces?

You can match matched braces with the (?R) construct introduced in perl
5.10, but you have to be careful about braces inside quotes and
comments. (You can probably ignore the possibility of unmatched braces
inside macro definitions and expansions.)

If you can assume the C is 'properly' formatted, of course, it becomes
much easier: the only time you get an open brace in the first column is
when it is starting a function, and the end of the function is the next
close brace which is also in the first column. Depending on what you're
trying to do you could consider running the C through indent(1) with
some appropriate set of options, since that has something approximating
a C parser.

Ben

Jim Gibson

unread,
Oct 1, 2012, 1:00:37 PM10/1/12
to
In article <1b62de6b-931a-481d...@googlegroups.com>,
<jose.luis...@gmail.com> wrote:

> Hi,
>
> This is the pattern from a C function:
>
> <<snip>>
> void foo(....)
> {
> ....
> }
> <<snip>>
>
> Given the function name, is there an easy way to extract the function text
> from a C file in Perl?

I have used the Text::Balanced module for this kind of thing in the
past.

--
Jim Gibson

Eli the Bearded

unread,
Oct 1, 2012, 6:13:08 PM10/1/12
to
In comp.lang.perl.misc, <jose.luis...@gmail.com> wrote:
> Given the function name, is there an easy way to extract the function
> text from a C file in Perl?

You are in a world of hurt if the programmer hates you.

:r macro.c
#include <stdio.h>

#define PROGRAMMER_HATES_YOU }

void
function_name
( int param )
{
char *haha = " } ";
if (param) {
puts(haha);
}
puts("Feel the love.\n");
PROGRAMMER_HATES_YOU

int main ( int a, char**v) {
function_name( a%2 ); return /* } done { */ (0); }

:r! gcc -Wall -o macro macro.c && ./macro
}
Feel the love.


Elijah
------
preprocessor makes everything much nastier

Kaz Kylheku

unread,
Oct 1, 2012, 8:02:35 PM10/1/12
to
On 2012-10-01, Eli the Bearded <*@eli.users.panix.com> wrote:
> In comp.lang.perl.misc, <jose.luis...@gmail.com> wrote:
>> Given the function name, is there an easy way to extract the function
>> text from a C file in Perl?
>
> You are in a world of hurt if the programmer hates you.
>
>:r macro.c
> #include <stdio.h>
>
> #define PROGRAMMER_HATES_YOU }

Note that such buffoonery in the source will also likely defeat numerous
popular programming tools like cscope, mkid and ctags. Oh, not to mention
syntax highlighting and automatic code indentation in your text editor, and the
reporting of function names in context diff hunks.

So that should really be:

#define PROGRAMMER_HATED_BY_TEAM_CUSTOMERS_WIFE_KIDS_DOG )

Ben Morrow

unread,
Oct 1, 2012, 7:52:33 PM10/1/12
to

Quoth Eli the Bearded <*@eli.users.panix.com>:
> In comp.lang.perl.misc, <jose.luis...@gmail.com> wrote:
> > Given the function name, is there an easy way to extract the function
> > text from a C file in Perl?
>
> You are in a world of hurt if the programmer hates you.
>
> :r macro.c
> #include <stdio.h>
>
> #define PROGRAMMER_HATES_YOU }

So run it through cpp before trying to parse it... Whether that's useful
or not depends on what exactly the OP is trying to do with these
function bodies, which he hasn't said.

Ben

Ben Morrow

unread,
Oct 1, 2012, 10:56:49 PM10/1/12
to

Quoth Kaz Kylheku <k...@kylheku.com>:
The perl source contains (in some configurations) all of

#define END_EXTERN_C }
#define STMT_END )
#define PERL_FPU_PRE_EXEC {

though in all cases when these macros are used correctly the visible
braces and brackets end up balancing. (The STMT_END definition is
particularly evil...)

Ben

0 new messages