in reply to Parsing a .h header file


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.

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

    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.
Re: Re: Parsing a .h header file
by hehenoobhehe (Novice) on Oct 22, 2003 at 08:55 UTC
    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.
Re: Re: Parsing a .h header file
by hehenoobhehe (Novice) on Oct 22, 2003 at 09:19 UTC
    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

        I would generate the code with no indenting and potential overlength lines then run it through astyle to make it presentable. Link to astyle on the previous node.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print