Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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.

Comment on extract comments from source code.
Re: extract comments from source code.
by Anonymous Monk on Sep 26, 2011 at 12:12 UTC
Re: extract comments from source code.
by Marshall (Prior) 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__ ...is 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.

    Update:
    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.

        <code>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-08-22 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls