Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: regex 2 match C function

by CombatSquirrel (Hermit)
on Aug 13, 2003 at 11:27 UTC ( [id://283507]=note: print w/replies, xml ) Need Help??


in reply to regex 2 match C function

Try this:
#!perl -w use strict; undef $/; my ($header, $type, $arguments); my $file = <DATA>; $file =~ s/((\w+)\s+(\w+)\s*\(([^)]*)\)\s*\{)/"\/* Function: $3()\n" . " Input:\n" . do { $header = + $1; $type = $2; $arguments = $4 . ','; ''} . (join "\n", map { sprintf " + %-5s %8s : ", split } $arguments =~ m|\s*(\w+\**\s+\**\w+) +\s*,|g) . "\n\n" . " Output:\n" . (sprintf " %-5s : \n", $ty +pe) . "\n Author :\n Date :\n*\ +/" . "\n\n$header"/ge; print $file; __DATA__ int function(int a, char *b) { ... return 0; }

I am just a little afraid that it will also catch if-blocks and such.
Hope this helped.

Replies are listed 'Best First'.
Re: Re: regex 2 match C function
by jai (Sexton) on Aug 14, 2003 at 06:33 UTC
    hi, thanx for the code. actually i came up with the following.
    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 }
    I know urs is far better. as u said, it will match try, catch blocks and also constructs of type
    #define add(x,y) {\
    I'd like the monks to give suggestions on optimizations,..

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://283507]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found