"C Language" annotation conversion--c comments to standard C + + language annotations

Source: Internet
Author: User

first, the specific requirements:

1:C-style comments/*/comments converted to standard C + + style//annotations

2://style annotations remain the same

3: All conversions are subject to grammatical rules

4: Note Translation requires support for comment nesting


Second, the conversion requirements:

There are a lot of nesting cases for annotations, and here's just an example, you need to follow the code of the C + + language annotation rules, and I won't just test the example here.                                                               



Three, the attention matters:

1, except for the following two cases of modification, the source file can not be changed after the conversion of any other changes:
 a. Extra annotations with spaces instead of
 b./* */At the beginning of the note is replaced by//, the end of the line is removed */
2, note the conversion of the following situation
a./******/

b.///*......*/
3. You do not need to consider comments in the input file that do not conform to the grammar rules


Four, the code:

Main.c

#include <stdio.h> #include <errno.h> #include <assert.h>typedef enum state{success,//successful File_erroe, File error no_match,//mismatch other,//other error}state;typedef enum tag{tag_begin,//tag_end,//c comment in C comment paragraph}tag; #pragma warning ( disable:4996) State Annotationconvert (file* inFile, file* outFile) {tag tag = Tag_end;char Firstch, Secondch;assert ( InFile), assert (outFile);d O{firstch = fgetc (inFile), switch (firstch) {//1. General case '/': SECONDCH = fgetc (inFile); if ( SECONDCH = = ' * ' && tag = = tag_end) {FPUTC ('/', outFile), FPUTC ('/', outFile);//3. Match question tag = tag_begin;} else if (secondch = = '/') {char next;fputc (firstch, outFile); FPUTC (SECONDCH, outFile);//7.c++ comment problem do {next = fgetc (InFile) ; Fputc (Next, outFile);} while (next! = ' \ n ' && next! = EOF);//When the end of the file is read, Mark Firstchif (next = EOF) {firstch = EOF;}} ELSE{FPUTC (Firstch, outFile); FPUTC (SECONDCH, outFile);} Break;case ' \ n ': fputc (' \ n ', outFile);//4. Multi-line Comment question if (tag = = Tag_begin) {FPUTC ('/', outFile); FPUTC ('/', outFile);} Break;case ' * ': SECONDCH = FGETC (InFile), if (secondch = = '/') {//2. Line break//5. Continuous comment question Char next = fgetc (InFile); if (next! = ' \ n ' && next! = EOF) {FSE Ek (InFile,-1, seek_cur);} FPUTC (' \ n ', outFile); tag = Tag_end;} else if (secondch = = ' * ') {//6. Continuous **/problem 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 ("Conversion succeeded \ n");} else if (ret = = No_match) {printf ("does not match \ n");} else if (ret = = File_erroe) {printf ("File Error:%d\n", errno);} else{printf ("Other error:%d\n ", errno);} return 0;}

Input.c

1. General conditions/* int i = 0; *///2. Line break/* int i = 0; */Int J = 0;//3. Match problem/*int i = 0;/*xxxxx*///4. Multiline comment Problem/*int i=0;int j = 0;int k = 0;*/int k = 0;//5. Continuous comment problem/**//**///6. Continuous **/problem/***///7.c++ Comment question///*xxxxxxxxxxxx*/

V. Examples of use of the environment

     When it comes to writing programs under Windows with the VC2005 environment, there are programs written in the C + + language, but with the comments of C, you can compile the connection to run successfully. But there are also many compilers that do not support a single-line comment for C. And don't want to manually change all the code. A program will be used to automatically replace C's comments with the C + + language annotation format.


"C Language" annotation conversion--c comments to standard C + + language annotations

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.