zoukankan      html  css  js  c++  java
  • Deleting comments in c or java code by awk scripts

      Last night , I attended writting examination for Bai Du,There is a problem, ask us implement a state machine to deleting the comments in c code,I don't know how to implement. So I writte this awk script to handle this problem.

      I test my scripts by about 10 000 lines c code ,it is reliable to deleting all comments,I also confirm it isn't delete c  code but I just test it by a little code.

          My classmate tell me, my scripts have some bugs ,but he just tell me there have some bugs.If you find there are some bugs ,please tell me the details.I will appreciate it.

          Following commants, my script can delete it ,I had many testing.

           1.  int tag = 0; //something about comments

           2. //something about comments

         3.  /*  something about comments */

           4. int tag = 0, /* someting about comments */  mytag = 0;

           5. /*  something

         about 

         comments */

          6  /* something

               * about

              * comments

              */

        My awk script is following:

               {

          ##The befor line don't contains /*

          if(deletetag == 0){

            mathc($0,///)

            ##This line contains //

            if ( RLENGTH == 2)

            {

              code = 0

              ##There are some c code befor //

              if( RSTART > 0) {

                print substr($0,0,RSTART-1)

              }

            } else {

              match($0,//*/)  

              tmpstart = RSTART

              ##This line contains /*

              if (RLENGTH == 2){

                   code=2

                match($0,/*//)  

                ##This line contains */

                if (RLENGTH == 2){

                  print substr($0,0,tmpstart-1) substr($0,RSTART + RLENGTH +1)

                } else {

                  deletetag=1

                }

              }

            }

          } else  if (deletetag == 1){      ##Befor line contains /*

              code=0

              match($0,/*//)

              ##This line contians */

              if(RLENGTH == 2){

                 deletetag=0

              }

          }

          ##This line don't contians // and /* and The befor line don't contians /*, so print this line

          if ( code ==1){

            print $0

          }

          code=1

       }

      

      If my awk scripts have some bugs ,please mail me lijidong@ict.ac.cn or qinqiqili@163.com , I will appreciate it. Thanks.

  • 相关阅读:
    一个iOS图片选择器的DEMO(实现图片添加,宫格排列,图片长按删除,以及图片替换等功能)
    [小细节,大BUG]记录一些小问题引起的大BUG(长期更新....)
    利用runTime,实现以模型为主的字典转模型(注意与KVC的区别)
    项目总结(三)--- 关于版本控制器
    项目总结(一)--- 关于用到的设计模式
    EntityFramework与TransactionScope事务和并发控制
    Entity Framework与ADO.NET批量插入数据性能测试
    Mathtype公式位置偏上
    FreeSWITCH 加载模块过程解读
    FreeSWITCH调用第三方TTS 使用tts_commandline
  • 原文地址:https://www.cnblogs.com/ordili/p/3996001.html
Copyright © 2011-2022 走看看