DDR爱好者之家 Design By 杰米
简单Python词法分析器实现,供大家参考,具体内容如下
词法分析器状态转换图:
词法分析器总流程图:
预处理程序:
词法分析器:
词法分析器程序详细设计
详细代码实现:
#!/usr/bin/env python3.4 # coding=utf-8 import sys import string keywards = {} # 关键字部分 keywards['False'] = 101 keywards['class'] = 102 keywards['finally'] = 103 keywards['is'] = 104 keywards['return'] = 105 keywards['None'] = 106 keywards['continue'] = 107 keywards['for'] = 108 keywards['lambda'] = 109 keywards['try'] = 110 keywards['True'] = 111 keywards['def'] = 112 keywards['from'] = 113 keywards['nonlocal'] = 114 keywards['while'] = 115 keywards['and'] = 116 keywards['del'] = 117 keywards['global'] = 118 keywards['not'] = 119 keywards['with'] = 120 keywards['as'] = 121 keywards['elif'] = 122 keywards['if'] = 123 keywards['or'] = 124 keywards['yield'] = 125 keywards['assert'] = 126 keywards['else'] = 127 keywards['import'] = 128 keywards['pass'] = 129 keywards['break'] = 130 keywards['except'] = 131 keywards['in'] = 132 keywards['raise'] = 133 # 符号 keywards['+'] = 201 keywards['-'] = 202 keywards['*'] = 203 keywards['/'] = 204 keywards['='] = 205 keywards[':'] = 206 keywards['<'] = 207 keywards['>'] = 208 keywards['%'] = 209 keywards['&'] = 210 keywards['!'] = 211 keywards['('] = 212 keywards[')'] = 213 keywards['['] = 214 keywards[']'] = 215 keywards['{'] = 216 keywards['}'] = 217 keywards['#'] = 218 keywards['|'] = 219 keywards[','] = 220 # 变量 # keywards['var'] = 301 # 常量 # keywards['const'] = 401 # Error # keywards['const'] = 501 signlist = {} # 预处理函数,将文件中的空格,换行等无关字符处理掉 def pretreatment(file_name): try: fp_read = open(file_name, 'r') fp_write = open('file.tmp', 'w') sign = 0 while True: read = fp_read.readline() if not read: break length = len(read) i = -1 while i < length - 1: i += 1 if sign == 0: if read[i] == ' ': continue if read[i] == '#': break elif read[i] == ' ': if sign == 1: continue else: sign = 1 fp_write.write(' ') elif read[i] == '\t': if sign == 1: continue else: sign = 1 fp_write.write(' ') elif read[i] == '\n': if sign == 1: continue else: fp_write.write(' ') sign = 1 elif read[i] == '"': fp_write.write(read[i]) i += 1 while i < length and read[i] != '"': fp_write.write(read[i]) i += 1 if i >= length: break fp_write.write(read[i]) elif read[i] == "'": fp_write.write(read[i]) i += 1 while i < length and read[i] != "'": fp_write.write(read[i]) i += 1 if i >= length: break fp_write.write(read[i]) else: sign = 3 fp_write.write(read[i]) except Exception: print(file_name, ': This FileName Not Found!') def save(string): if string in keywards.keys(): if string not in signlist.keys(): signlist[string] = keywards[string] else: try: float(string) save_const(string) except ValueError: save_var(string) def save_var(string): if string not in signlist.keys(): if len(string.strip()) < 1: pass else: if is_signal(string) == 1: signlist[string] = 301 else: signlist[string] = 501 def save_const(string): if string not in signlist.keys(): signlist[string] = 401 def save_error(string): if string not in signlist.keys(): signlist[string] = 501 def is_signal(s): if s[0] == '_' or s[0] in string.ascii_letters: for i in s: if i in string.ascii_letters or i == '_' or i in string.digits: pass else: return 0 return 1 else: return 0 def recognition(filename): try: fp_read = open(filename, 'r') string = "" sign = 0 while True: read = fp_read.read(1) if not read: break if read == ' ': if len(string.strip()) < 1: sign = 0 pass else: if sign == 1 or sign == 2: string += read else: save(string) string = "" sign = 0 elif read == '(': if sign == 1 or sign == 2: string += read else: save(string) string = "" save('(') elif read == ')': if sign == 1 or sign == 2: string += read else: save(string) string = "" save(')') elif read == '[': if sign == 1 or sign == 2: string += read else: save(string) string = "" save('[') elif read == ']': if sign == 1 or sign == 2: string += read else: save(string) string = "" save(']') elif read == '{': if sign == 1 or sign == 2: string += read else: save(string) string = "" save('{') elif read == '}': if sign == 1 or sign == 2: string += read else: save(string) string = "" save('}') elif read == '<': save(string) string = "" save('<') elif read == '>': save(string) string = "" save('>') elif read == ',': save(string) string = "" save(',') elif read == "'": string += read if sign == 1: sign = 0 save_const(string) string = "" else: if sign != 2: sign = 1 elif read == '"': string += read if sign == 2: sign = 0 save_const(string) string = "" else: if sign != 1: sign = 2 elif read == ':': if sign == 1 or sign == 2: string += read else: save(string) string = "" save(':') elif read == '+': save(string) string = "" save('+') elif read == '=': save(string) string = "" save('=') else: string += read except Exception as e: print(e) def main(): if len(sys.argv) < 2: print("Please Input FileName") else: pretreatment(sys.argv[1]) recognition('file.tmp') for i in signlist.keys(): print("(", signlist[i], ",", i, ")") if __name__ == '__main__': main()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]