Python做一个词法分析器

真是不做不知道,短短120行代码花了一个多小时......

一堆if+VsDebug不熟练搞得我头炸了

先贴代码:

def Lex(scr:str,word:list=["void","ret","if","while","for","as","global","self","break","char","byte","short","int","long","float"]):
	tree=[]
	sym=""
	getstr=[]
	typeOfget=1
	n=['0','1','2','3','4','5','6','7','8','9']
	s=['@','+','-','*','/','&','^','%','|',"'",'"','[',']','{','}','(',')','.',',','!',';',':','<','>','=']
	line=1
	last=""
	for i in scr:
		if i=="\n":
			line=line+1
		match typeOfget:
			case 0:
				if last=="\\":
					match i:
						case "n":
							getstr[-1]="\n"
						case "\'":
							getstr[-1]=i
						case "\"":
							getstr[-1]=i
						case "t":
							getstr[-1]="\t"
						case _:
							getstr.append(i)
				else:
					if i==sym:
						tree.append(["str",''.join(getstr),line])
						getstr=[]
						typeOfget=1
					else:
						getstr.append(i)
			case 1:
				if i=="\n":
					if getstr!=[]:
						if ''.join(getstr) in word:
							tree.append(["word",''.join(getstr),line])
						else:
							tree.append(["name",''.join(getstr),line])
						getstr=[]
				elif getstr=="" and i in n:
					typeOfget=2
					getstr.append(i)
				elif i in s:
					if getstr!=[]:
						if ''.join(getstr) in word:
							tree.append(["word",''.join(getstr),line])
						else:
							tree.append(["name",''.join(getstr),line])
						getstr=[]
					if i=="\"" or i=="'":
						sym=i
						typeOfget=0
					else:
						tree.append(["symbol",i,line])
				elif i in [" ","\t"]:
					if getstr!=[]:
						if ''.join(getstr) in word:
							tree.append(["word",''.join(getstr),line])
						else:
							tree.append(["name",''.join(getstr),line])
						getstr=[]
					if last=="\n":
						typeOfget=3
						getstr.append(i)
				elif i in n:
					if getstr==[]:
						typeOfget=2
					getstr.append(i)
				else:
					getstr.append(i)
			case 2:
				if i in n or i in ['a','b','c','d','e','f','g','A','B','C','D','E','F','G','h','H']:
					getstr.append(i)
				elif i in [" ","\t","\n"]:
					tree.append(["num",''.join(getstr),line])
					getstr=[]
					typeOfget=1
			case 3:
				if i in ["\t"," "]:
					getstr.append(i)
				elif i=="\n":
					getstr=[]
					typeOfget=1
				elif i in n:
					tree.append(["format",''.join(getstr),line])
					getstr=[]
					typeOfget=2
					getstr.append(i)
				elif i in s:
					if getstr!=[]:
						tree.append(["format",''.join(getstr),line])
						getstr=[]
					if i in ['"',"'"]:
						typeOfget=0
						sym=i
					else:
						tree.append(["symbol",i,line])
						typeOfget=1
		last=i
	match typeOfget:
		case 0:
			return [False,line,"字符没有结束"]
		case 1:
			if getstr!=[]:
				if ''.join(getstr) in word:
					tree.append(["word",''.join(getstr),line])
				else:
					tree.append(["name",''.join(getstr),line])
		case 2:
			if getstr!=[]:
				tree.append(["num",''.join(getstr),line])
	return [True,tree]
if __name__=="__main__":
	import sys
	with open(sys.argv[1],"r") as f:
		f=f.read()
	del sys
	print(Lex(f))

简单说一下:

word表示保留字,输出列表第一项表示是否成功如果成功第二项是解析结果,不成功是错误信息和哪一行错误.

解析结构的每一项是类型+内容+行数

这只是第二遍解析(第一遍解析去注释没做),第三遍解析具体处理要看语法定义了就没做.

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值