Deleting comments in C or Java code by awk scripts

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.