Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: extract comments from source code.

by Marshall (Prior)
on Sep 26, 2011 at 12:34 UTC ( #927848=note: print w/ replies, xml ) Need Help??


in reply to extract comments from source code.

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 */


Comment on Re: extract comments from source code.
Select or Download Code
Re^2: extract comments from source code.
by veerubiji (Sexton) on Sep 26, 2011 at 15:53 UTC
    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: note [id://927848]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (16)
As of 2015-07-06 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (75 votes), past polls