Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

need regex to parse c++ functions

by Chief of Chaos (Friar)
on Nov 27, 2002 at 13:51 UTC ( [id://216083]=perlquestion: print w/replies, xml ) Need Help??

Chief of Chaos has asked for the wisdom of the Perl Monks concerning the following question:

Greetings honoured monks,
I'm working on a regex that returns me a list of function names of a c++ file.
(Returns a array of strings or a string )
But there are so many possibilities of function definitions.
E.g. :
are ok :
void CLASS1::Function0 (void){ } const void CLASS1::Function1 (void) { } const CLASS2::Type CLASS1::Function3 (void){ } std::vector<std::string>& CLASS1::Function4 (void){ } const CLASS2::Type<Type2>* CLASS1::Function5 (void){ }
are not ok :
const std::string Class1::Member1 = ""; foo = Class2::function(FOO,BAA);
etc
All functions (that are ok) should be in the array.
To simplify it, I have set the function parameters to void.
Does there a module exists for parsing c++ source files?
Is there a better way to get the function names without a regex?
my code so far :
#!/usr/local/bin/perl -w use strict; use warnings; while (<DATA>) { chomp(); my $reg=""; $reg.="^[a-zA-Z0-9]+"; $reg.="\\s*"; $reg.="[^:]*::\\s*"; $reg.="([^ ]*\\s)?"; $reg.="([^(]*)"; $reg.="\\([^)]*\\)[{]?\$"; print "$2\n" if (/$reg/); } __DATA__ void CLASS1::Function0 (void){ } const void CLASS1::Function1 (void) { } const CLASS2::Type CLASS1::Function3 (void){ } std::vector<std::string>& CLASS1::Function4 (void){ } const CLASS2::Type<Type2>* CLASS1::Function5 (void){ } const std::string Class1::Member1 = "" " more data "; std::vector<std::string>& CLASS1::Function4 (void){ foo = Class2::function(FOO,BAA); }
I hope the great spirit of monk wisdom can help a unworthy little apprentice like me.

Replies are listed 'Best First'.
Re: need regex to parse c++ functions
by broquaint (Abbot) on Nov 27, 2002 at 15:41 UTC
    Might I recommend the cdecl tool for your prototype parsing? Or you might want to look at C::DynaLib and Inline::CPP and see how they do it.
    HTH

    _________
    broquaint

      Thanks,

      Inline::CPP::Grammar holds the gramma for the cpp function parsing, this can be used in Parse::RecDescent.

      Great help.
Re: need regex to parse c++ functions
by Zaxo (Archbishop) on Nov 27, 2002 at 17:36 UTC

    You may do better with a grammar. There are BNF grammars floating around - in the draft standard for one source - and Parse::RecDescent ought to be able to handle the subset for declarations.

    After Compline,
    Zaxo

      Thanks,
      i will try Parse::RecDescent which seems a little bit simpler as a regex.

      And if I don't get it I will set a flag for help.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found