Last night, I attended writting examination for Bai Du,there are a problem, ask us implement a state machine to deleting t He comments in C code,i don ' t know what to implement. So I Writte This awk script to handle this problem.
I test my scripts by about lines C code, it's reliable to deleting all comments,i also confirm it isn ' t delete c Code but I just test it by a little code.
My classmate tell me, my scripts has some bugs, but he just tell me there has some bugs. If you find there is some bugs, please tell me the details. I'll appreciate it.
Following commants, my script can delete it, I had many testing.
1. int tag = 0; Something About comments
2.//something about comments
3./* Something about comments */
4. int tag = 0,/* someting about Comments */mytag = 0;
5./* Something
About
Comments * *
6/* Something
* About
* Comments
*/
My awk script is following:
{
# #The befor line don ' t contains/*
if (Deletetag = = 0) {
MATHC ($0,/\/\/)
# #This line contains//
if (rlength = = 2)
{
Code = 0
# #There is some C code befor//
if (Rstart > 0) {
Print substr ($0,0,rstart-1)
}
} else {
Match ($0,/\/\*/)
Tmpstart = Rstart
# #This line contains/*
if (rlength = = 2) {
code=2
Match ($0,/\*\//)
# #This Line contains * *
if (rlength = = 2) {
Print substr ($0,0,tmpstart-1) substr ($0,rstart + rlength + 1)
} else {
Deletetag=1
}
}
}
} else if (Deletetag = = 1) {# #Befor line contains/*
Code=0
Match ($0,/\*\//)
# #This Line Contians * *
if (rlength = = 2) {
Deletetag=0
}
}
# #This line don ' t contians//and/* and the befor line don ' t contians/*, so print this line
if (code ==1) {
Print $
}
Code=1
}
If My awk scripts has some bugs, please mail me [email protected] or [email protected], I'll appreciate it. Thanks.
Deleting comments in C or Java code by awk scripts