[C Language] annotation Conversion -- C annotation is converted into a standard C ++ language annotation, Annotation
I. Specific requirements:
1: C-style comments/**/comments are converted to Standard C ++ styles // comments
2: // The style comment remains unchanged
3: All conversions must comply with syntax rules.
4: Support for annotation nesting for annotation Conversion
Ii. Conversion requirements:
There are many situations where annotations are nested. Here is just an example. You need to follow the C/C ++ annotation rules to write code. I will not just test the examples here.
Iii. Notes:
1. You cannot modify the source file after conversion except in the following two cases:
A. Extra annotators are replaced by spaces.
B./**/Replace the comment with // and remove the end of the line */
2. Pay attention to the conversion in the following situations.
A ./******/
B .///*...... */
3. Do not consider comments that do not comply with the syntax rules in the input file.
4. Code:
Main. c
# Include <stdio. h> # include <errno. h> # include <assert. h> typedef enum STATE {SUCCESS, // FILE_ERROE is successful, // file error NO_MATCH, // does not match OTHER, // OTHER error} STATE; typedef enum TAG {TAG_BEGIN, // TAG_END in the C Comment Segment, // C comment end} TAG; # pragma warning (disable: 4996) STATE AnnotationConvert (FILE * inFile, FILE * outFile) {TAG tag = TAG_END; char firstCh, secondCh; assert (inFile); assert (outFile); do {firstCh = fgetc (inFile); switch (firstCh) {// 1. I Case '/': secondCh = fgetc (inFile); if (secondCh = '*' & tag = TAG_END) {fputc ('/', outFile ); fputc ('/', outFile); // 3. matching Problem tag = TAG_BEGIN;} else if (secondCh = '/') {char next; fputc (firstCh, outFile); fputc (secondCh, outFile ); // 7.c++ annotation problem do {next = fgetc (inFile); fputc (next, outFile);} while (next! = '\ N' & next! = EOF); // when reading the end of a file, Mark firstChif (next = EOF) {firstCh = EOF ;}} else {fputc (firstCh, outFile); fputc (secondCh, outFile);} break; case '\ N': fputc (' \ n', outFile); // 4. multi-line annotation problem if (tag = TAG_BEGIN) {fputc ('/', outFile); fputc ('/', outFile);} break; case '*': secondCh = fgetc (inFile); if (secondCh = '/') {// 2. line feed problem // 5. continuous annotation problem char next = fgetc (inFile); if (next! = '\ N' & next! = EOF) {fseek (inFile,-1, SEEK_CUR);} fputc ('\ n', outFile); tag = TAG_END;} else if (secondCh = '*') {// 6. consecutive **/issue fseek (inFile,-1, SEEK_CUR); fputc (firstCh, outFile);} else {fputc (firstCh, outFile); fputc (secondCh, outFile);} break; default: fputc (firstCh, outFile); break;} while (firstCh! = EOF); if (tag = TAG_END) {return SUCCESS;} else {return NO_MATCH;} int StartConvert () {STATE s; const char * inFileName = "input. c "; const char * outFileName =" output. c "; FILE * inFile = fopen (inFileName," r "); FILE * outFile = fopen (outFileName," w "); if (inFile = NULL) {return FILE_ERROE;} if (outFile = NULL) {fclose (inFile); return FILE_ERROE;} s = AnnotationConvert (inFile, outFile); fclose (inFile ); fclose (outFile); return s;} int main () {STATE ret = StartConvert (); if (ret = SUCCESS) {printf ("converted successfully \ n ");} else if (ret = NO_MATCH) {printf ("unmatched \ n");} else if (ret = FILE_ERROE) {printf ("file error: % d \ n ", errno);} else {printf (" other errors: % d \ n ", errno);} return 0 ;}
Input. c
// 1. generally/* int I = 0; * // 2. line feed problem/* int I = 0; */int j = 0; // 3. matching Problem/* int I = 0;/* xxxxx * // 4. multi-line comment/* int I = 0; int j = 0; int k = 0; */int k = 0; // 5. comments continuously/** // 6. consecutive **/problem/*** // 7.c++ comments // * xxxxxxxxxx */
V. Environment example
When a program is written in the VC2005 environment in Windows, a program written in C ++ can be compiled and run successfully with C annotations. however, many compilers do not support single-line comments of C. I don't want to modify all the code manually. this program will be used to automatically replace C comments with the comments in C ++.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.