Comment Conversion?? --c++ comments to standard C language annotations

Source: Internet
Author: User

Write the annotation conversion program to implement the conversion function of annotations in a C + + language program source file
1.c++ style comments//notes converted to standard C-style */*/comments
2./* * * Style comments remain the same
3. All conversions are subject to grammatical rules
4. Annotation transformations need to support comment nesting
Note 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.
1, single-line comments or no nesting, comment lines directly converted, such as:
①//123/* 123 */
②/* 123 // 123 */unchanged
③/*123
* * Remain as is
2. There are nested annotations (there are also nested other annotation symbols in one comment//,/* */) The excess of each comment symbol in the nesting is replaced with two spaces.
such as single line:
①//123/456 //123 456/
②//123//456/123 456/
③//123*//456/*123 456/
such as cross-line
/...........
......... ..........
........ ..........
/ /
Attention:
1, in addition to the following two cases of modification, the source file after conversion can not have any other changes:
① extra annotations with spaces instead of
②//at the beginning of the note with/* and the end of the line */
2, the following 3 situations do not need to convert
①/* 123 // 456 */
②/* 123 // 456
*/
③/* 123
// 456
*/
3. You do not need to consider comments in the input file that do not conform to the grammar rules
Here is the main function

#include <iostream>using namespace STD;extern intCommentconvert (file *inputfile, file *outputfile);intMain () {FILE *fpin = NULL;//inputfileFILE *fpout = NULL;//outputfileFpin = fopen ("/users/caolu/desktop/program/own authoring/annotation/input.c","R");if(NULL = = Fpin) {cout<<"Open input file fail!"<<endl;return-1; } fpout = fopen ("/users/caolu/desktop/program/own authoring/annotation/output.c","W");if(NULL = = fpout) {cout<<"Open output file fail!"<<endl;return-1; } commentconvert (Fpin,fpout);//Fclose (Fpin); Fclose (fpout);return 0;}

Implementation of State functions

#include <iostream>usingnamespace Std;#Define UL unsigned longextern intCommentconvert (file *inputfile, file *outputfile); typedefenum{no_comment_state, c_comment_state, Cpp_comment_state, Str_state, end_state} State_enum;typedefstruct{FILE *inputfile;    FILE *outputfile; State_enum Ulstate;} State_machine;//State_machine g_state = {0};/////////////////////////// /// /// /// /// /// /// /// ///voidEventpro (CharCH);voidEventproatno (CharCH);voidEVENTPROATC (CharCH);voidEventproatcpp (CharCH);voidEVENTPROATSTR (CharCH);/////////////////////////// /// /// /// /// /// /// /// ////intCommentconvert (file *inputfile, file *outputfile) {if(Inputfile==null | | outputfile==null) {cout<<"input argument invalid!"<<endl;return-1;    } g_state.inputfile = Inputfile;    G_state.outputfile = outputfile; G_state.ulstate = no_comment_state;CharCh while(g_state.ulstate! = end_state) {ch = fgetc (g_state.inputfile);//Eventpro (CH); }return 0;}voidEventpro (CharCH) {Switch(g_state.ulstate) { CaseNo_comment_state:eventproatno (CH); Break; CaseC_COMMENT_STATE:EVENTPROATC (CH); Break; CaseCpp_comment_state:eventproatcpp (CH); Break; CaseSTR_STATE:EVENTPROATSTR (CH); Break; CaseEnd_state: Break; }}voidEventproatno (CharCH) {CharNextch;Switch(CH) { Case '/':// // /* Nextch = fgetc (g_state.inputfile);if(Nextch = ='/')//C + +{FPUTC ('/', g_state.outputfile); FPUTC (' * ', g_state.outputfile);        G_state.ulstate = cpp_comment_state; }Else if(Nextch = =' * ')//c{FPUTC (ch,g_state.outputfile);            FPUTC (Nextch,g_state.outputfile);        G_state.ulstate = c_comment_state; }Else{        } Break; CaseEOF:g_state.ulstate = end_state; Break; Case ' "': G_state.ulstate = str_state;default: FPUTC (Ch,g_state.outputfile); Break; }}voidEVENTPROATC (CharCH) {CharNextch;Switch(CH) { Case '/': Nextch = fgetc (g_state.inputfile);if(Nextch = ='/') {FPUTC ("', g_state.outputfile); FPUTC ("', g_state.outputfile); } Break; Case ' * ': Nextch = fgetc (g_state.inputfile);if(Nextch = ='/') {FPUTC (ch,g_state.outputfile);            FPUTC (Nextch,g_state.outputfile);        G_state.ulstate = no_comment_state; } Break;default: FPUTC (Ch,g_state.outputfile); Break; }}voidEventproatcpp (CharCH) {//123/*123    CharNextch;Switch(CH) { Case'/': Nextch =fgetc (G_state.inputfile);if(Nextch = ='/') {FPUTC ("', g_state.outputfile); FPUTC ("', g_state.outputfile); }Else  if(Nextch = =' * ') {FPUTC ("', g_state.outputfile); FPUTC ("', g_state.outputfile); }ElseFPUTC (Ch,g_state.outputfile); Break; Case' * ': Nextch =fgetc (G_state.inputfile);if(Nextch = ='/') {FPUTC ("', g_state.outputfile); FPUTC ("', g_state.outputfile); }ElseFPUTC (Ch,g_state.outputfile); Break; Case ' \ n ': FPUTC (' * ', g_state.outputfile); FPUTC ('/', g_state.outputfile); FPUTC (' \ n ', g_state.outputfile); G_state.ulstate = no_comment_state; Break; CaseEOF:FPUTC (' * ', g_state.outputfile); FPUTC ('/', g_state.outputfile); G_state.ulstate = end_state; Break;default: FPUTC (Ch,g_state.outputfile); Break; }}voidEVENTPROATSTR (CharCH) {CharNextch;Switch(CH) { Case' + ': Fgetc (G_state.inputfile);if(Nextch = =' "') g_state.ulstate = no_comment_state; Break; CaseEOF:g_state.ulstate = end_state; Break;default: FPUTC (Ch,g_state.outputfile); Break; }}

Input.c

//Each zone consists of several memory blocks//TRTE//DFGD//aaaaa*///*//*dfnmhj;///**/XFHG;/*dfg*//**//*jjjjjj//*/intj =0;/*//ggg//gg//gg*//**//**/Qqqqqqqqq/*mmmmmm*//*KKKKKKKK * */////////////////////////// /// /// /// /// /// /// ////2 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"//aaaaaaaa "aaaaa///* * *""aaaaaaaaaaaaaa///* * *"

Output.c

/ * Each zone consists of several memory blocks * //*trte dfgd*//*AAAAA * */ * dfnmhj;*// * xfhg;*//*dfg*//**//*JJJJJJ * *intj =0;/ * GGG GG gg*//**//**/Qqqqqqqqq/*mmmmmm*//*KKKKKKKK * */ * 2*/"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"//aaaaaaaa "aaaaa///* * *""aaaaaaaaaaaaaa///* * *"

Comment Conversion?? --c++ comments to standard C language annotations

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.