CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
regex 2 match C functions

 



jai
New User

Aug 13, 2003, 1:21 AM

Post #1 of 4 (3379 views)
regex 2 match C functions Can't Post

hi,
can anyone give me the regex to match C-style functions.

given a C source file, the regex should match all the C functions and add a header b4 the function.

the functions may be of the form

int
function(int a, char *b) {
...
return 0;
}

the regex should match:

int
function(int a, char *b)

and print:
------------------------------------------------------
/* Function : function()
Input:
int a :
char *b:

Output:
int :

Author :
Date :
*/

int
function(int a, char *b)
-----------------------------------------------------------

thanks
-jai


KevinR
Veteran


Aug 13, 2003, 3:09 PM

Post #2 of 4 (3372 views)
Re: [jai] regex 2 match C functions [In reply to] Can't Post

I am not entirely sure I understand your question.

but lets see, maybe something along these lines will help you:


Code
  
$target = 'function(int a, char *b)';

open (FILE,"path/to/your/file") or die;
while (<FILE>) {
next if $_ !~ /$target/;
if (/$target/) {
&print_code;
close(FILE);
exit;
}
}

sub print_code {
print qq~Content-type: text/html\n\n~;#only if you have not already printed a header
print qq~
/* Function : function()
Input:
int a :
char *b:

Output:
int :

Author :
Date :
*/

int
function(int a, char *b)
~;
}


I left out the first part of your pattern (int) thinking that only the second part will be sufficient to return a true match. Maybe someone will have a better suggestion
-------------------------------------------------


jai
New User

Aug 13, 2003, 11:37 PM

Post #3 of 4 (3367 views)
Re: [KevinR] regex 2 match C functions [In reply to] Can't Post

hi,
i know u didnt understand my question. Actually the script should get the C source filename as input and print add headers to the functions.
heres what i came up with, I'd welcome comments on this.

Code
foreach (@ARGV) { 

# Read file
open(FF,"<$_") || die ("Cant open $_ : $!\n");
my $t=<FF>;
close FF;

$t=~s/((\**\s*\w+\s*)+\s*)(\((\s*\**\s*\w+,?)*\)?\s*{)/${\&do($1,$3)}/g;

# Write file
open (FF, ">$_.j") || die ("Cant open $_.j : $!\n");
print FF $t;
close FF;
}

sub do($$) {

my ($a,$b)=@_;
my ($t1, $t2, @t3);
$t1=$a,$t2=$b;
$a=~s/\n/ /gm;
$b=~s/\n//g;
$b=~s/\s+/ /gm;
$b=~s/[(\{)]/ /g;
@t3=split /,/,$b;
#@t3=map {"\n *$_\t : " } @t3;
@t3=map{sprintf "\n *%-13s : ", $_} @t3;
my $t=<<E;


/*
* Function : $a
*
* Inputs : \n *@t3
*
* Author :
* Date :
*/
$t1$t2
E
}


/jai


jryan
User

Sep 6, 2003, 9:12 PM

Post #4 of 4 (3340 views)
Re: [jai] regex 2 match C functions [In reply to] Can't Post

I'll tell you what; I'll do the hard part for you. Here's the regex:


Code
#!/usr/bin/perl -w 
use strict;
use re 'eval';

my $single = qr/'(?:[^'\\]|\\.)'/x;
my $double = qr/"(?:[^"\\]|\\.)*"/x;
my $quoted = qr/$single|$double/x;
my $comment = qr!/\* (?:[^/](?!\*)|.)* /\*!x;
my $typename = qr/\w+\s* \**\s* \w+\s*/x; # loose, to account for structs

my $paramlist = qr/
\(
(?:
(?: $typename \s*,\s* )*
$typename
)?
\) \s*
/x;

my $block = qr!
\{
(?:
[^{}'"/]
| $comment
| $quoted
| (??{ balblock })
)*
\}
!x;

my $cfunction = qr/^$typename $paramlist $block/x;

my $data = ...;
my @matches = $data =~ /($cfunction)/gm;


I didn't test it very thoroughly, but it should be ok. Capture what you need.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives