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

Any suggestions for how to parse a header file that contains the interface for a class that must be generated using PERL(or any thing else, I dont know hence I dont suggest anything other than PERL at this juncture:(). I guess this means being able to extract information like: -Function Declarations
-Arguments
And then be able to use this information for creating a C++ class that implements these methods

Any suggestions on how to do this or where to look for appropriate information are welcome. :)

Thanks.

PS.Posting this as a seperate question (in addition to the earlier one related to the same) because I think the earlier post doesnt explain well enough what Im trying to achieve in the first place, ;):P

Edited by castaway: added some HTML. PS: Text Wrapping is probably meant.

Replies are listed 'Best First'.
Re: Parsing a .h header file
by jmcnamara (Monsignor) on Oct 22, 2003 at 08:19 UTC

    There is a Parse::RecDescent based C++ grammar as part of the Inline::CPP module.

    However, you might find it difficult to modify if you aren't familiar with Perl and Parse::RecDescent.

    Depending on how straight-forward and standardised your class declarations are it may be possible to implement a simple parser in Perl, although I can envisage several class constructs that make this task tricky.

    --
    John.

      All I want to do is to use a header file to generate some C++ code from it namely a class declaration and function definitions complete with arguments etc and some defunct code that I could then use to generate more code, namely, this

      1) Have a header file blah.h that contains the interface

      2) Generate a .cpp file from it that has the class declaration and function definitions with defunct/default code and some placeholders/tags(layman language used here owing to lack of specuialized knowldge of PERL or scripting in general) that are to be replaced in Step 3

      3) Use the file from Step 2 and replace the placeholders with some information that is provided (by means of an input config file/command line/whatever other means) and must be replaced uniformally in the entire file(from Step 2)

      Requirements that MUST be met from the FINAL PROCESSED OUTPUT FILE from Step 3 are:

      1) The length of each line of code (that can be compiled by a standard C++ compiler) must NOT exceed 78 characters

      2) Indentation and other standard formatting features must be retained and no user intervention must be needed in order to beautify or standardize the code to something that is normally acceptable as code that can be accpeted in a code repository.

      3) Last, stating the obvious of course: the code SHOULD be capable of being compiled by a C++ compiler. :)

      I hope I have come up with a better description of the problem at hand.

      Thanks very much for the response.:) Much appreciated.
      You hit the nail on its head, as I already admitted in my post(that appeared as an anyonymous monk) and is related to the problem I describe here, I am NOT a perl Guru and my knowldge of PERL can be best described as elementary. I have tried to give a better description of the problem in a reply earlier. I hope that might enable you to suggest something that I can do to tackle this problem. Thanks very much.
      With reference to my problem that I have described : here:

      A possible solution to the problem *could* be :

      1) I use the same method I use now to create a tokenzied sorta intermediate file from the header file complete with all the information that I need to be able to furnish a class declaration and a class definition.Call this File A

      Use File A as an input file to further process the tokens etc and produce another file that has tags/tokens whatever else that I need for stuff like:

      -function names

      -function params

      return values

      and so on WITHOUT any formatting.CAll it File B

      3)Use File B and parse each token to get File C. While priting out File C, I check for the length of the line being printed (to the file) and if the length exceeds 78 characters, I insert a newline to move to the next line.

      Possible problems that I encounter or envisage I will encouter are:

      1) Within functions etc, one normally indents the code, so also in if/for code blocks, so when Im processing a string of tokens that I know is ised for a function definition, after I move to a newline, i SHOULD also print out a tab to preserve the formatting

      This is all I can think of at the moment and I am rather confused by all this. I thought it was an easy problem to begin with , but.....:(

        I did a quick google search for 'parse C++ header file with perl' and came up with quite a few good results. My suggestion is that you:
        • find a script that already generates a data structure from a header file ( like this one: http://starship.python.net/crew/tbryan/PERCEPS/ ) ( or http://aips2.nrao.edu/RELEASED/cxx2html/ )
        • use Data::Dumper to view the data structure that is created.
        • use a templating system such as Template toolkit to layout your functions and classes.

        This way you will be able to take an iterative approach to the development of your tool and it is a good method to build your perl skills as you go.

        --
        Clayton
        Would appreciate if you comment on the misgivings of this possible solution as I have suggested here. Thanks
Re: Parsing a .h header file
by dragonchild (Archbishop) on Oct 22, 2003 at 14:28 UTC
    Your best solution is to contract this out to someone (like me or any number of other monks) to do this for you on a project basis. You're wanting to do something that's relatively complicated, with serious potential for exceptions, and you've already admitted your Perl strengths are limited.

    I'm not attempting to solicit your business (though I wouldn't complain if you gave it to me). I'm telling you that unless you're willing to put some serious time into this project, you're going to end up with a mess that won't do what you want it to do. At that point, you're either going to give up or contract someone anyways.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    ... strings and arrays will suffice. As they are easily available as native data types in any sane language, ... - blokhead, speaking on evolutionary algorithms

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.