Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: regex 2 match C function

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


in reply to regex 2 match C function

If you don't want the whole file to be slurped in at once
you could, since lines of code are seperated by semi-colons in C, just use a semi-colon as an end-of-line indicator: local $/ = ';';.
Updated code that should not match ifs and such:
#!perl -w use strict; $/ = ';'; my ($header, $type, $arguments); for my $data (<DATA>) { $data =~ s/(((?:static\s+)?\w+)\s+(\w+)\s*\(([^)]*)\)\s*\{)/($3 eq 'if' or $3 eq 'do' or $3 eq 'while' o +r $3 eq 'for') ? +$1 : "\/* Function: $3()\n" . " Input:\n" . do { $header = $1; $type = $2; $arguments = $4 . ','; '' +} . (join "\n", map { sprintf " %-8s %8 +s : ", split } $arguments =~ m|\s*(\w+\**\s+\**\w+)\s*,|g) +. "\n\n" . " Output:\n" . (sprintf " %-8s : \n", +$type) . "\n Author :\n Date :\ +n*\/" . "\n\n$header"/ge; print $data; } __DATA__ int function(int a, char *b) { ... return 0; }
(only if it is not HW)
Hope this helped (and I hope your question was not HW, but for work, documentation or something similar as I suppose it to be).

Replies are listed 'Best First'.
Re^2: regex 2 match C function
by Anonymous Monk on Jan 24, 2014 at 09:49 UTC
    Hi, the above code will treat static as part of the return type. It would be nice if I get an algorithm for extracting return type with any number of words. For example " static inline unsigned int".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found