Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Newbien: Extract a C function from a file
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
jose.luis.fdez.d...@gmail.com  
View profile  
 More options Sep 29 2012, 5:56 am
Newsgroups: comp.lang.perl.misc
From: jose.luis.fdez.d...@gmail.com
Date: Sat, 29 Sep 2012 02:56:34 -0700 (PDT)
Local: Sat, Sep 29 2012 5:56 am
Subject: Newbien: Extract a C function from a file
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Josef Moellers  
View profile  
 More options Sep 29 2012, 10:30 am
Newsgroups: comp.lang.perl.misc
From: Josef Moellers <josef.moell...@invalid.invalid>
Date: Sat, 29 Sep 2012 16:30:23 +0200
Local: Sat, Sep 29 2012 10:30 am
Subject: Re: Newbien: Extract a C function from a file
On 09/29/2012 11:56 AM, jose.luis.fdez.d...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
jose.luis.fdez.d...@gmail.com  
View profile  
 More options Sep 29 2012, 3:27 pm
Newsgroups: comp.lang.perl.misc
From: jose.luis.fdez.d...@gmail.com
Date: Sat, 29 Sep 2012 12:27:15 -0700 (PDT)
Local: Sat, Sep 29 2012 3:27 pm
Subject: Re: Newbien: Extract a C function from a file
El sábado, 29 de septiembre de 2012 16:30:24 UTC+2, Josef Moellers  escribió:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Morrow  
View profile  
 More options Sep 29 2012, 6:03 pm
Newsgroups: comp.lang.perl.misc
From: Ben Morrow <b...@morrow.me.uk>
Date: Sat, 29 Sep 2012 23:00:29 +0100
Local: Sat, Sep 29 2012 6:00 pm
Subject: Re: Newbien: Extract a C function from a file

Quoth jose.luis.fdez.d...@gmail.com:

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Gibson  
View profile  
 More options Oct 1 2012, 1:00 pm
Newsgroups: comp.lang.perl.misc
From: Jim Gibson <jimsgib...@gmail.com>
Date: Mon, 01 Oct 2012 10:00:37 -0700
Local: Mon, Oct 1 2012 1:00 pm
Subject: Re: Newbien: Extract a C function from a file
In article <1b62de6b-931a-481d-bce1-2acbd1cf16f5@googlegroups.com>,

<jose.luis.fdez.d...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eli the Bearded  
View profile  
 More options Oct 1 2012, 6:13 pm
Newsgroups: comp.lang.perl.misc
From: Eli the Bearded <*...@eli.users.panix.com>
Date: Mon, 1 Oct 2012 22:13:08 +0000 (UTC)
Local: Mon, Oct 1 2012 6:13 pm
Subject: Re: Newbien: Extract a C function from a file
In comp.lang.perl.misc,  <jose.luis.fdez.d...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Oct 1 2012, 8:02 pm
Newsgroups: comp.lang.perl.misc
From: Kaz Kylheku <k...@kylheku.com>
Date: Tue, 2 Oct 2012 00:02:35 +0000 (UTC)
Local: Mon, Oct 1 2012 8:02 pm
Subject: Re: Newbien: Extract a C function from a file
On 2012-10-01, Eli the Bearded <*...@eli.users.panix.com> wrote:

> In comp.lang.perl.misc,  <jose.luis.fdez.d...@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 )


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Morrow  
View profile  
 More options Oct 1 2012, 8:03 pm
Newsgroups: comp.lang.perl.misc
From: Ben Morrow <b...@morrow.me.uk>
Date: Tue, 2 Oct 2012 00:52:33 +0100
Local: Mon, Oct 1 2012 7:52 pm
Subject: Re: Newbien: Extract a C function from a file

Quoth Eli the Bearded <*...@eli.users.panix.com>:

> In comp.lang.perl.misc,  <jose.luis.fdez.d...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Morrow  
View profile  
 More options Oct 1 2012, 11:03 pm
Newsgroups: comp.lang.perl.misc
From: Ben Morrow <b...@morrow.me.uk>
Date: Tue, 2 Oct 2012 03:56:49 +0100
Local: Mon, Oct 1 2012 10:56 pm
Subject: Re: Newbien: Extract a C function from a file

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »