[C ++] C ++ annotations are converted into C annotations.

Source: Internet
Author: User

[C ++] C ++ annotations are converted into C annotations.

In a large project, due to the diversity of code personnel, the annotation style of the Code is different. However, in a released version, the annotation style should be consistent so that other personnel can use and maintain it later. As a result, the following requirements are generated:


Implements the Conversion Function of annotation in a C/C ++ program source file (Here we implement the conversion of C ++ annotation to a standard C language annotation)









Annotation conversion requirements:





Other considerations:







Specific analysis:


When reading source files, you may encounter the following situations: C-style comments (including the nesting of comments), C ++ style comments (including the nesting of comments), and nested comments in characters, there are no comments, end, and other statuses.


Introduce"State MachineConcept:

State MachineShort for FSM (Finite State Machine), mainly divided into two categories: the first class, if the output is only related to the State and has nothing to do with the input, it is called the Moore State Machine; the second class, output is called the Mealy state machine.

The state machine can be summarized into four elements: current state, condition, action, and substate. This induction is mainly based on the internal causal relationship of the state machine. "Current state" and "condition" are factors, and "action" and "secondary State" are results. The details are as follows:

① Current state: the current state.

② Condition: Also known as "Event". When a condition is met, an action is triggered or a State migration is executed.

③ Action: The action executed after the condition is met. After the action is executed, You can migrate it to a new State or maintain the original state. The action is not required. When the condition is met, You can directly migrate to the new State without performing any action.

④ State: the new state to be migrated after the condition is met. "Substate" is relative to "current state". Once "substate" is activated, it is changed to a new "current state.


The abstract state is often converted into a state transition diagram:




Read characters from the input file, interpret and modify comments, and generate a new file. (File operations are not outlined here)


Code implementation:



Main Program

#include<iostream>using namespace std;extern int CommentConvert(FILE *inputfile, FILE *outputfile);int main(){FILE *fpIn = NULL;  //inputfileFILE *fpOut = NULL; //outputfileerrno_t err;err = fopen_s(&fpIn, "input.c", "r");if ((err = fopen_s(&fpIn, "input.c", "r")) != 0){cout << "Open input file fail!" << endl;//return -1;}if ((err = fopen_s(&fpOut, "output.c", "w")) != 0){cout << "Open output file fail!" << endl;//return -1;}CommentConvert(fpIn, fpOut); //fclose(fpIn);fclose(fpOut);return 0;}


Annotation Conversion Program:

# Include <iostream> using namespace std; # define STACKSIZE 1024 # define UL unsigned longextern int CommentConvert (FILE * inputfile, FILE * outputfile); typedef enum {delimiter, C_COMMENT_STATE, CPP_COMMENT_STATE, STR_STATE, END_STATE} STATE_ENUM; // Status List typedef struct {FILE * inputfile; FILE * outputfile; STATE_ENUM ulstate;} STATE_MACHINE; // state machine // STATE_MACHINE g_state = {0 }; /////////////////////////////////// ///////////// Void EventPro (char ch); // event-driven void EventProAtNo (char ch ); void EventProAtC (char ch); void EventProAtCpp (char ch); void EventProAtStr (char ch ); //////////////////////////////////////// ///// // int CommentConvert (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; // The initial state is the uncommented state char ch; while (g_state.ulstate! = END_STATE) {ch = fgetc (g_state.inputfile); // EventPro (ch);} return 0;} void EventPro (char ch) // event-driven model {switch (g_state.ulstate) // different event states use different state functions {case NO_COMMENT_STATE: EventProAtNo (ch); break; case C_COMMENT_STATE: EventProAtC (ch); break; case CPP_COMMENT_STATE: EventProAtCpp (ch ); break; case STR_STATE: EventProAtStr (ch); break; case END_STATE: break;} void EventProAtNo (char ch) {char nextch; switch (ch) {case '/': //// * nextch = fgetc (g_state.inputfile); if (nextch = '/') // C ++ {fputc ('/', g_state.outputfile ); fputc ('*', g_state.outputfile); // convert CPP's // to/* g_state.ulstate = CPP_COMMENT_STATE; // convert to CPP status} else if (nextch = '*') // C {fputc (ch, g_state.outputfile); fputc (nextch, g_state.outputfile); g_state.ulstate = C_COMMENT_STATE; // convert to C status} else {} break; case EOF: g_state.ulstate = END_STATE; break; case '"': g_state.ulstate = STR_STATE; default: fputc (ch, g_state.outputfile ); break ;}} void EventProAtC (char ch) {char nextch; switch (ch) {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; case '/': nextch = fgetc (g_state.inputfile ); if (nextch = '/') {fputc ('', g_state.outputfile); fputc ('', g_state.outputfile); // nested comments are replaced by two spaces} break; default: fputc (ch, g_state.outputfile); break ;}} void EventProAtCpp (char ch) {// 123/* 123 char nextch; switch (ch) {case '\ N ': // process multiple lines of fputc ('*', g_state.outputfile); fputc ('/', g_state.outputfile); fputc ('\ n', g_state.outputfile); g_state.ulstate = NO_COMMENT_STATE; break; case EOF: fputc ('*', g_state.outputfile); fputc ('/', g_state.outputfile); g_state.ulstate = END_STATE; break; case '/': nextch = fgetc (g_state.inputfile ); if (nextch = '/') // (nested //) {fputc ('', g_state.outputfile); fputc ('', g_state.outputfile );} else if (nextch = '*') // (nested/*) {fputc ('', g_state.outputfile); fputc ('', g_state.outputfile );} else {fputc (ch, g_state.outputfile);} break; case '*': nextch = fgetc (g_state.inputfile); if (nextch = '/') // nesting // {fputc ('', g_state.outputfile); fputc ('', g_state.outputfile);} else {fputc (ch, g_state.outputfile);} break; case '"': g_state.ulstate = STR_STATE; default: fputc (ch, g_state.outputfile); break ;}} void EventProAtStr (char ch) {char nextch; switch (ch) {case '\ 0 ': nextch = fgetc (g_state.inputfile); if (nextch = '"') // read to \ 0 and" indicates that the string ends g_state.ulstate = NO_COMMENT_STATE; // The status switches to break; case EOF: g_state.ulstate = END_STATE; break; default: fputc (ch, g_state.outputfile); break ;}}


Test Case (input file ):


// Each zone is composed of several memory blocks. // each zone is composed of several memory blocks. // each block is 4096 bytes. // int I = 0; * // * int I = 0; // **/int I = 0;/* int I = 0; * // ** // * int I = 0; // */int j = 0; // each zone consists of several memory blocks, each block contains 4096 bytes. // The 0th Integers of each block point to the next partition. // Therefore, it is a single-chain table structure. // Therefore, each partition contains only 4092 bytes to store real data *// * int I = 0; * // * int j = 0; * // **/int I = 0; //////////////////////////////////////// /// // 5 "abcdefghijklmn ~~~~!!!!! !!! "// Dsgf sdfg" fs sdfsg // **/"" fs sdfsg ///**/"



Output file ):


/* Each zone consists of several memory blocks * // * Each zone consists of several memory blocks, each of which is 4096 bytes * // * int I = 0; * // * int I = 0; * // * int I = 0; * // * int I = 0; * // ** // * int I = 0; */int j = 0;/* Each zone consists of several memory blocks, each block is 4096 bytes. The 0th Integers of each block point to the next zone. Therefore, it is a single-chain table structure. Therefore, each zone contains only 4092 bytes to store real data. * // * int I = 0; * // * int j = 0; * // **/int I = 0; /* 5 */"abcdefghijklmn ~~~~!!!!!!!! "// Dsgf sdfg" fs sdfsg // **/"" fs sdfsg ///**/"





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.