注释转换

利用状态机机制将C注释转换为C++注释;

1 #include"utili.h"
  2
  3 extern "C" int ConvertComment(FILE *inputfile, FILE *outputfile);
  4
  5 typedef enum
  6 {
  7     NO_COMMENT_STATE,
  8     C_COMMENT_STATE,
  9     CPP_COMMENT_STATE,
 10     QUOTATION_STATE,
 11     END_STATE
 12 }ENUM_STATE;
 13
 14 typedef struct StateMachine
 15 {
 16     FILE *inputfile;
 17     FILE *outputfile;
 18     ENUM_STATE state;
 19     ENUM_STATE pre_state;
 20 }StateMachine;

23 StateMachine g_state;
 24
 25 void EventPro(char ch);
 26 void EventProAtNo(char ch);
 27 void EventProAtC(char ch);
 28 void EventProAtCpp(char ch);
 29 void EventProAtQuotation(char ch);
 30 /
 31
 32 int ConvertComment(FILE *inputfile, FILE *outputfile)
 33 {
 34     if(inputfile==NULL || outputfile==NULL)
 35     {
 36         cout<<"argument is invalid."<<endl;
 37         return -1;
 38     }

 g_state.inputfile = inputfile;
 42     g_state.outputfile = outputfile;
 43     g_state.state = NO_COMMENT_STATE;
 44
 45     char ch;
 46     while(g_state.state != END_STATE)
 47     {
 48         ch = fgetc(g_state.inputfile);
 49         EventPro(ch);
 50     }
 51
 52     return 0;
 53 }
 54
 55 void EventPro(char ch)
 56 {
 57     switch(g_state.state)
 58     {
 59     case NO_COMMENT_STATE:
 60         EventProAtNo(ch);
 61         break;
 62     case C_COMMENT_STATE:
 63         EventProAtC(ch);
 64         break;
 65     case CPP_COMMENT_STATE:
 66         EventProAtCpp(ch);
 67         break;
 68     case QUOTATION_STATE:
 69         EventProAtQuotation(ch);
 70         break;
 71     //case END_STATE:
 72     //  break;
 73     }
 74 }
 75 void EventProAtNo(char ch)
 76 {
77     char nextch;
 78     switch(ch)
 79     {
 80     case '/':
 81         nextch = fgetc(g_state.inputfile);
 82         if(nextch == '/') // C++
 83         {
 84             fputc('/',g_state.outputfile);
 85             fputc('*',g_state.outputfile);
 86             g_state.state = CPP_COMMENT_STATE;
 87         }
 88         else if(nextch == '*') // C
 89         {
 90             fputc('/',g_state.outputfile);
 91             fputc('*',g_state.outputfile);
 92             g_state.state = C_COMMENT_STATE;
 93         }
 94         else
 95         {
 96            fputc('/',g_state.outputfile);
 97            fputc('*',g_state.outputfile);
 98         }
 99         break;
100     case '\"':
101         fputc('\"',g_state.outputfile);
102         g_state.state = QUOTATION_STATE;
103         break;
104     case EOF:
105         g_state.state = END_STATE;
106         break;
107     default:
108         fputc(ch, g_state.outputfile);
109         break;
110         }
111 }
112 void EventProAtC(char ch)
113 {
114     char nextch;
115     switch(ch)
116     {
 INSERT >> <rtComment.cpp  < Even
117         case '/':
118             nextch = fgetc(g_state.inputfile);
119             if (nextch == '/' || nextch == '*')
120             {
121                 fputc(' ', g_state.outputfile);
122                 fputc(' ', g_state.outputfile);
123             }
124             else
125             {
126                 fputc(ch, g_state.outputfile);
127             }
128             break;
129         case '*':
130             while(ch == '*')
131             {
132                 nextch = fgetc(g_state.inputfile);
133                 if (nextch == '/')
134                 {
135                     fputc('*', g_state.outputfile);
136                     fputc('/', g_state.outputfile);
137                     g_state.state = NO_COMMENT_STATE;
138                     break;
139                 }
140                 else if(nextch == '*')
141                 {
142                     fputc('*', g_state.outputfile);
143                     ch = nextch;
144                 }
145                 else
146                 {
147                     fputc('*', g_state.outputfile);
148                     fputc(nextch, g_state.outputfile);
149                     break;
150                 }
151             }
152             break;
153         case '\"':
154             g_state.pre_state = g_state.state;
155             g_state.state = QUOTATION_STATE;
156             fputc('\"', g_state.outputfile);
 INSERT >> <rtComment.cpp  < EventProAtC() < cpp <<  66% : 156:  1 < ! trailin…
-- 插入 --
157             break;
158         default:
159             fputc(ch,g_state.outputfile);
160             break;
161     }
162 }
163 void EventProAtCpp(char ch)
164 {
165     char nextch;
166     switch(ch)
167     {
168         case EOF:
169             fputc('*', g_state.outputfile);
170             fputc('/', g_state.outputfile);
171             g_state.state = END_STATE;
172             break;
173         case '\n':
174             fputc('*', g_state.outputfile);
175             fputc('/', g_state.outputfile);
176             fputc('\n', g_state.outputfile);
177             g_state.state = NO_COMMENT_STATE;
178             break;
179
180         case '/':
181             nextch = fgetc(g_state.inputfile);
182             if (nextch == '/' || nextch == '*')
183             {
184                 fputc(' ', g_state.outputfile);
185                 fputc(' ', g_state.outputfile);
186             }
187             else
188             {
189                 fputc(ch, g_state.outputfile);
190                 fputc(nextch, g_state.outputfile);
191             }
192             break;

193         case '*':
194             while(ch == '*')
195             {
196                 nextch = fgetc(g_state.inputfile);
197                 if (nextch == '/')
198                 {
199                     fputc(' ', g_state.outputfile);
200                     fputc(' ', g_state.outputfile);
201                     break;
202                 }
203                 else if(nextch == '*')
204                 {
205                     fputc('*', g_state.outputfile);
206                     ch = nextch;
207                 }
208                 else
209                 {
210                     fputc('*', g_state.outputfile);
211                     fputc(nextch, g_state.outputfile);
212                     break;
213                 }
214             }
215             break;
216         case '\"':
217             g_state.pre_state = g_state.state;
218             g_state.state = QUOTATION_STATE;
219             fputc('\"', g_state.outputfile);
220             break;
221         default:
222             fputc(ch,g_state.outputfile);
223             break;
224     }
225 }
226 void EventProAtQuotation(char ch)
227 {
228     switch (ch)
229     {
230         case '\"':
231             g_state.state = g_state.pre_state;
232         default :
 INSERT >> <t.cpp  < EventPro
233             fputc(ch, g_state.outputfile);
234             break;
235     }
2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值