有一个需求,需要将源码提供出去,交予三方进行安全审核,为了减少代码泄漏带来的影响,要求将
创新互联公司是一家集网站建设,乌翠企业网站建设,乌翠品牌网站建设,网站定制,乌翠网站建设报价,网络营销,网络优化,乌翠网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
多行注释:
/**
多行注释
*/
// 单行注释
对上一步的情况分析后简单的结论,
re.findall("\/\*.*?\*\/", content, re.S)
re.findall("\/\/.*", line, re.S)
本以为这样是一个简单粗暴的方法,真正跑起来之后发现有较大问题
"(?m)^//.*(?=\\n)"
// 后面有一个单独的 “ ,引号的排查优先级高,导致这个引号被保留,导致整体移除异常
‘“’
"select * from user where name = 'root'"
“\"”
有时候捷径不是最快的路径。
折腾了几天之后发现按照穷举法去发现所有的异常case实在是太难了,因为我们的精力是有限的,一时半会无法想到所有的case,那有没有什么办法呢?
这个时候记忆深处的一些内容开始冒泡泡,程序员的三大浪漫之一的 编译原理 开始出现了。
之前懵懵懂懂读过的文本开始有一点点印象了。重新翻开经典之作的内容,看看他是怎么来处理词法和语法的。
position = initial + 2 * 60
,经过语法分析后生成的树
position = initial + 2 * 60
经过语义分析后按照编译原理中讲的过程,要先一个的拆成词,然后将词串成语句,然后一个语句一个语句的处理。
整体的思路:
# coding=utf-8
foler_path = "./java/test/"
def rewriteContent(dirpath, filename, content):
writefile = open(dirpath + "/" + filename, "w+")
# print content
writefile.write(content)
writefile.close()
def clean_all_note():
for dirpath, dirnanes, filenams in os.walk(foler_path):
for filename in filenams:
print dirpath + "/" + filename
clean_note(dirpath, filename)
#判断是否是双引号,需要排除 '"' 和 \" 的情况,
def is_available_quotes(ch, pre_ch,next_ch):
return ch == "\"" and pre_ch != "\\" and not (pre_ch == "'" and next_ch == "'")
#判断是否是多行注释的开头 即 /*
def is_prefix_multiline_comment(ch, pre_ch):
return ch == "*" and pre_ch == "/"
#判断是否是多行注释的结尾,即 */
def is_suffix_multiline_comment(ch, pre_ch):
return ch == "/" and pre_ch == "*"
#判断是否是单行注释 //
def is_single_line_comment(ch, pre_ch):
return ch == "/" and pre_ch == "/"
# 判断是否是换行
def is_line_feed(ch, pre_ch):
return ch == "\n"
def clean_note(dirpath, filename):
file = open(dirpath + "/" + filename, "r+")
content = file.read()
multiline_ing = False
single_line_ing = False
quotes_ing = False
pre_ch = ""
index = 0
lastPoi = 0
newContent = ""
for ch in content:
if multiline_ing:
if is_suffix_multiline_comment(ch,pre_ch):
# print "m l e:" + pre_ch + ch
lastPoi = index+1
multiline_ing = False
elif single_line_ing:
if is_line_feed(ch,pre_ch) or index == len(content)-1:
# print "s l e:" + content[lastPoi:index-1]
lastPoi = index
single_line_ing = False
elif quotes_ing:
#解决 "\\"
if ch == "\\" and pre_ch == "\\":
ch = ''
if is_available_quotes(ch, pre_ch,content[index+1]):
# print "yinhao e :" + pre_ch + ch
newContent = newContent + content[lastPoi:index]
lastPoi = index
quotes_ing = False
else:
if index == len(content)-1:
# print "e s :" + pre_ch + ch
newContent = newContent + content[lastPoi:index+1]
elif is_available_quotes(ch, pre_ch,content[index+1]):
# print "yinhao s :" + pre_ch + ch
# newContent = newContent + content[lastPoi:index]+"----"
quotes_ing = True
elif is_prefix_multiline_comment(ch, pre_ch):
# print "m l s :" + pre_ch + ch
newContent = newContent + content[lastPoi:index-1]
multiline_ing = True
elif is_single_line_comment(ch, pre_ch):
# print "s l s :" + pre_ch + ch
newContent = newContent + content[lastPoi:index-1]
single_line_ing = True
index = index+1
pre_ch = ch
rewriteContent(dirpath, filename, newContent)
if __name__ == '__main__':
for dirpath, dirnanes, filenams in os.walk(foler_path):
for filename in filenams:
clean_note(dirpath, filename)
当前题目:编译原理助我脱坑
转载来于:http://www.shufengxianlan.com/qtweb/news46/119496.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联