Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

extract comments from source code.

by veerubiji (Sexton)
on Sep 26, 2011 at 12:05 UTC ( #927844=perlquestion: print w/replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to extract xml comments from my c code. Actvally i added some xml comments to my c code. Now I need to extract xml comments as it is by using perl.

. c code .... .......... /*xml comments*/ ......... .........ccode </code>

I need to extract only xml comments he above code not entire c code.As show in the above code i just posted small c code before and after xml comments, more number of c code lines is there in my code like same as above c code.but can any one help me how to extract xml comments as it is as shown in the above.I tried to extract xml comments using perl regexp but i am unable to extract xml comments. can any one help me. thanks in advance. sorry for deleting code.

Replies are listed 'Best First'.
Re: extract comments from source code.
by Marshall (Abbot) on Sep 26, 2011 at 12:34 UTC
    To extract the lines between and including ones that start with <DTC> and end with </DTC>:

    #!/usr/bin/perl -w use strict; while (<DATA>) { if (m|^\s*<DTC>| ... m|^\s*</DTC>|) { print; } } __DATA__ your data consisting of the C program...
    See: tutorial on Flip/Flop operator: Flipin good, or a total flop?

    Lines extracted:

    <DTC> <TroubleCode>1101</TroubleCode> <ShortDescription>The reductant pump needs to run slower than expect +ed to keep the pressure.</ShortDescription> <Classification>FAULT</Classification> <SelfHealing>No selfhealing</SelfHealing> <Erasability>erasable</Erasability> <RedetectedDegradation>No Degradation</RedetectedDegradation> <WarningLamp>No Warning Lamp</WarningLamp> <DirectDegradation>No Action</DirectDegradation> <Order>PRIMARY</Order> </DTC>
    There are a number of Perl XML parsers that can deal with these lines after they have been extracted from the C code.

    I looked again at the post and I think that the "C" code will not compile because the <DTC>...</DTC> stuff does not look like a comment block to the C compiler. I guess the blocks could be like:

    /*XML doc start <DTC> <TroubleCode>1101</TroubleCode> <Classification>FAULT</Classification> <SelfHealing>No selfhealing</SelfHealing> <Erasability>erasable</Erasability> </DTC> XML doc end */
    So that the C compiler will see the whole XML doc as a single comment block.

    An alternate that is worth considering is Perl POD (Perl Plain Old Documentation) Perl POD. If you organize the "comments" in the C code right, the C compiler will be happy. And the Perl documentation generator will also be happy processing the raw C input file (a "=" as the first character on a line has special meaning). The Perl POD generators will make HTML web pages or man pages directly from the C source file, if the comments /*....*/ are done right.

    /*Perl doc start =some heading in Perl POD ... lines =some other heading tag in Perl POD ..... lines =cut Perl doc end */
      Hi Marshall, Thank you very much for your reply.I tried your suggested regex its working. I am very happy with that and i am at learning stage so I got some understanding with your code that extract xml comments. also thank you very much for your suggetion in c code also i rectified that error also. now i am very happy because my problem solved. If incase i have more question on perl programming i will ask you.,because i am working with that. thank you , regards, veerubiji

        Hi marshall, i have one more problem i have xml file like below. but i nedd to create hash table using perl script.

Re: extract comments from source code.
by Anonymous Monk on Sep 26, 2011 at 12:12 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://927844]
Approved by sundialsvc4
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2017-03-25 19:58 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (313 votes). Check out past polls.