Delete all comments in C language programs, code implementation, and language program comments
Link: http://lixing123.com/archives/310
At the end of chapter 1st, I learned "C programming language". I have a question:
Write a comment statement to delete all C language programs. Correct Processing of string and character constants with quotation marks. In C, annotations cannot be nested. Exercise 1-23. Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments don't nest.
At the beginning, I used a brute-force method to traverse each character and then judge whether the comment was entered.
In this case, there is a very troublesome problem: only when "//" or "/*" is detected consecutively can the comment State be determined; if only one "/" is detected, and other strings are followed, We have to print this separate.
In addition, there are many branch statuses. if you use if/else, it is hard to understand and error-prone.
I searched the internet and found a very good solution: the state machine. Jump between various States, with clear logic, easy to make mistakes, and easy to debug if errors occur.
Paste the code below:
# Include <stdio. h> int state; int c1, c2; void change_state (int c); int main (int argc, const char * argv []) {int c; state = 0; c1 = 0; c2 = 0; while (c = getchar ())! = EOF) {c1 = c2; c2 = c; change_state (c);} if (/* disables code */(0) = 1) {printf ("just test: // abcd "); printf (" just test:/* hello */");}/* state machine function */void change_state (int c) {if (state = 0) {// normal state if (c = '/') {state = 1;} else if (c = '"') {state = 5; putchar (c) ;}else if (c = '\ '') {state = 6; putchar (c) ;}else {state = 0; putchar (c) ;}} else if (state = 1) {// 1 '/' if (c = '/') {state = 2 ;} else if (c = '*') {state = 3;} else {state = 0; putchar (c1); putchar (c );}} else if (state = 2) {// "//" comment status if (c = '\ n') {state = 0; putchar (c );} else {state = 2 ;}} else if (state = 3) {// "/*" comment status if (c = '*') {state = 4 ;} else {state = 3 ;}} else if (state = 4) {if (c = '/') {state = 0 ;}else {state = 3 ;}} else if (state = 5) {// In the "string if (c = '"') {state = 0; putchar (c );} else if (c = '\') {state = 7; putchar (c) ;}else {state = 5; putchar (c );}} else if (state = 6) {// In the 'character, if (c =' \ '') {state = 0; putchar (c );} else if (c = '\') {state = 8; putchar (c) ;}else {state = 6; putchar (c );}} else if (state = 7) {// In the "string" \ "state = 5; putchar (c);} else if (state = 8) {// "\" state = 6; putchar (c) ;}} in the 'string );}}
Use this code as the input. The result is as follows:
#include <stdio.h>int state;int c1,c2;void change_state(int c);int main(int argc, const char * argv[]) { int c; state = 0; c1 = 0; c2 = 0; while ((c=getchar())!=EOF) { c1 = c2; c2 = c; change_state(c); } if ( (0)==1) { printf("just test://abcd"); printf("just test:/*hello*/"); }}void change_state(int c){ if (state==0) { if (c=='/') { state = 1; }else if (c=='"'){ state = 5; putchar(c); }else if (c=='\''){ state = 6; putchar(c); } else{ state = 0; putchar(c); } }else if (state==1) { if (c=='/') { state = 2; }else if (c=='*'){ state = 3; }else{ state = 0; putchar(c1); putchar(c); } }else if (state==2) { if (c=='\n') { state = 0; putchar(c); }else{ state = 2; } }else if (state==3) { if (c=='*') { state = 4; }else{ state = 3; } }else if (state==4) { if (c=='/') { state = 0; }else{ state = 3; } }else if (state==5){ if (c=='"') { state = 0; putchar(c); }else if(c=='\\'){ state = 7; putchar(c); }else{ state = 5; putchar(c); } }else if (state==6){ if (c=='\'') { state = 0; putchar(c); }else if(c=='\\'){ state = 8; putchar(c); }else{ state = 6; putchar(c); } }else if (state==7){ state = 5; putchar(c); }else if (state==8){ state = 6; putchar(c); }Perfect!
Thanks @ roma823 and its article: http://blog.csdn.net/roma823/article/details/6364849