[工具]Python 移除代码注释

参考:编译原理删除C/C++代码中的所有注释

各种语言的注释,在处理代码时,都需要进行移除。
一些简单的办法就是使用字符串不断匹配,然后移除。
稍微能上到理论层面的,能落地的文章,就是 编译原理删除C/C++代码中的所有注释 。后来使用Python重新改写了一遍。在使用中发现不能满足要求,比如[特殊注释]的语法不支持,就新增进去;还有int a = -1;这类的注释,需要把初始值-1去掉,也在代码里面新增了。

使用状态机来处理注释移除问题,可以把此类问题完美解决,扩张性也非常强大。相比于lex + yacc 方案,入门和调整更便利。

备注:支持包含中文的注释。

#coding=utf-8

# quote:

class rmcmnt :
    ### members
    m_type = 'CPP'

    ### contructed function
    def __init__(self, type):
        m_type = type

    ### member function

    # remove code comments
    def removecomment(seft, strInput) :
        state = 0;
        strOutput = ''
        strRemoved = ''

        for c in strInput :
            if state == 0 and c == '/' :        # ex. [/]
                state = 1
            elif state == 1 and c == '*' :      # ex. [/*]
                state = 2
            elif state == 1 and c == '/' :      # ex. [#]
                state = 4
            elif state == 1 :                   # ex. [<secure/_stdio.h> or 5/3]
                print('/')
                state = 0

            elif state == 3 and c == '*':       # ex. [/*he**]
                state = 3
            elif state == 2 and c == '*':       # ex. [/*he*]
                state = 3
            elif state == 2:                    # ex. [/*heh]
                state = 2

            elif state == 3 and c == '/':       # ex. [/*heh*/]
                state = 0
            elif state == 3:                    # ex. [/*heh*e]
                state = 2

            elif state == 4 and c == '\\':      # ex. [//hehe\]
                state = 9
            elif state == 9 and c == '\\':      # ex. [//hehe\\\\\]
                state = 9
            elif state == 9:                    # ex. [//hehe\<enter> or //hehe\a]
                state = 4
            elif state == 4 and c == '\n':      # ex. [//hehe<enter>]
                state = 0

            elif state == 0 and c == '\'':      # ex. [']
                state = 5
            elif state == 5 and c == '\\':      # ex. ['\]
                state = 6
            elif state == 6:                    # ex. ['\n or '\' or '\t etc.]
                state = 5
            elif state == 5 and c == '\'':      # ex. ['\n' or '\'' or '\t' ect.]
                state = 0

            elif state == 0 and c == '\"':      # ex. ["]
                state = 7
            elif state == 7 and c == '\\':      # ex. ["\]
                state = 8
            elif state == 8:                    # ex. ["\n or "\" or "\t ect.]
                state = 7
            elif state == 7 and c == '\"':      # ex. ["\n" or "\"" or "\t" ect.]
                state = 0

            ### new request
            #  []
            elif state == 0 and c == '[':       # ex. [[]
                state = 10
            elif state == 10 and c == ']':      # ex. []]
                state = 11

            # [[]]
            elif state == 10 and c == '[':      # ex. []]
                state = 12
            elif state == 12 and c == ']':      # ex. [[]
                state = 13
            elif state == 13 and c == ']':      # ex. [[]
                state = 14

            # remove character in []
            elif state == 10:                
                state = 10
        
            # remove character in [[]]
            elif state == 12:                
                state = 12
            elif state == 13:                
                state = 13
            # restore state
            elif state == 11:                
                state = 0
            elif state == 14:                
                state = 0

            elif state == 11 and c == ']':     
                state = 14
            elif state == 1 and c == ']':     
                state = 13
        
            elif state == 10:                
                state = 10
            elif state == 12:                
                state = 12

            elif state == 11:                
                state = 13
            elif state == 12:                
                state = 0
            elif state == 13:                
                state = 0

            # remove "=-1" in "int a = -1;"
            elif state == 0 and c == '=':     
                state = 15
            elif state == 15 and c == ';':    
                state = 0

            if (state == 0 and c != '/') or state == 5 or\
                state == 6 or state == 7 or state == 8 :
                strOutput += c
            else:
                # removed chareters
                strRemoved += c

        return strOutput

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,154评论 1 32
  • 实验目的:掌握接口的定义及实现,掌握面向接口的编程思想实验内容:1、求任意柱体的体积(面向接口的编程思想)2、动物...
    小石头呢阅读 260评论 0 0
  • 他离开她的身体 冲了个凉擦干水渍 然后正襟危坐说: 我们继续讨论亚里士多德 理想国的未来 三十年后 他又一次离开她...
    楊孜阅读 860评论 0 3
  • 第二十一章 三年后 时光一晃而过,来美国已有三年,赵小深也考到了这边的学校,哥哥赵诚的公司也慢慢走上正轨,...
    书小璇阅读 511评论 5 0
  • 智默堂总部茶事馆门口的舞台上,中日韩三国的表演精采纷至。 不论国籍如何,语言如何,习茶人那平和的面孔,入心的表演都...
    孔雀堂茶号阅读 1,263评论 0 3