这里刚开始试用python的函数功能,可能把局部变量和全局变量给写乱了,后期交之前还想再优化的时候发现越改越乱,太真实的hhh
实验四:语法分析器1–预测分析器构造
实验目的: 通过编写一个预测分析器,掌握自上而下构造语法分析器的方法之一------预测分析法。
实验要求:
1、该语法分析器的任务如下:
(1) 可识别程序代码中符合以下文法中的3-5个,返回该语句为何种语句,或提示出错
文法G1:
L→E
E→E=T|T
E→E+T|T
T→T-F|F
F→(E)|id
文法G2:
L→E
E→E=T|T
E→E*T|T
T→T/F|T%F|F
F→(E)|id
文法G3:
D→TL
T→int|flort
L→id R
R→,id R|ε
文法G4:
L →E;L|ε
E →TE’
E’→+TE’|-TE’|ε
T →FT’
T’→*FT’|/FT’|mod FT’|ε
F →(E)|id|num
文法G5:
stmt→if expr then stmt else stmt
|if expr then stmt
|other
(2)出错处理可自行设计,并添加在预测分析表中
测试用例:
#include <stdio.h>
#include <math.h>
int main ( )
{int a,b,c,disc,x1,x2,p,q;
scanf("%lf%lf%lf",&a,&b,&c);
sqrt=(b*b)/(4*a*c)
disc=b+b-a+c;
if (disc<0) p=2.0*a;
else p=-b/(2.0*a)
printf("x1=%7.2f\nx2=%7.2f\n",x1,x2);
}
return 0;
}
环境:python 2.7 IDE:pyCharm
实验代码
因为还是用了之前实验2,3的内容,所以list还是这些
# -*- coding: UTF-8 -*-
# 设置列表
list0 = ['IF', 11, 'THEN', 12, 'ELSE', 13, 'INT', 14, 'CHAR', 15, 'FOR', 16]
list1 = ['{', 90, '}', 91, '(', 92, ')', 93]
list2 = ['=', 21, '>=', 22, "==", 23, '+', 24, '/', 25, '%', 26, '++', 27, '"', 28, ';', 29] # 运算符
list4 = [] # 数字 动态添加
list5 = [] # 变量名 动态添加,检查是否存在
基本就是之前实验2的东西,作为一个分割函数
def splitfuc():
list_fin = []
list_temp = []
fo = open("*****路径\\test3.txt", "r")
str_1 = fo.readlines()
fo.close()
# print str_1
for y in range(len(str_1)):
list_temp = list_temp + str_1[y].split(' ') # 突然发现这应该一句话就能解决hhh
y = 0
p = len(list_temp)
# print list_temp
while y < p:
if list_temp[y] == '':
del list_temp[y]
p = p - 1
else:
y = y + 1
# print list_temp
fo = open("C:\\Users\\Launcher-Z\\Desktop\\3_output.txt", "w")
# 开始从第一个单词循环
j = 0
k = 0
# 开始遍历
for idx in range(len(list_temp)):
str_new = list_temp[idx]
i = 0
while i < len(str_new):
if 'a' <= str_new[i] <= 'z' or 'A' <= str_new[i] <= 'Z':
if i != len(str_new)-1:
end = i + 1
while end < len(str_new):
if 'a' <= str_new[end] <= 'z' or 'A' <= str_new[end] <= 'Z' or '0' <= str_new[end] <= '9':
end = end + 1
else:
end = end - 1
break
if i != end:
tmp = str_new[i:end+1]
if tmp.upper() in list0:
list_fin = list_fin + [tmp]
fo.write(tmp)
fo.write(' ')
else:
if tmp not in list5:
list5.append(tmp)
list5.append(50 + k)
k = k + 1
list_fin = list_fin + [tmp]
fo.write(tmp)
fo.write(' ')
else:
list_fin = list_fin + [tmp]
fo.write(tmp)
fo.write(' ')
i = end + 1
continue
else:
tmp = str_new[i]
if tmp not in list5:
list5.append(tmp)
list5.append(50 + k)
k = k + 1
list_fin = list_fin + [tmp]
fo.write(tmp)
fo.write(' ')
else:
list_fin = list_fin + [tmp]
fo.write(tmp)
fo.write(' ')
i = end + 1
continue
else:
tmp = str_new[i]
if tmp not in list5:
list5.append(tmp)
list5.append(50 + k)
k = k + 1
list_fin = list_fin + [tmp]
fo.write(tmp)
fo.write(' ')
else:
list_fin = list_fin + [tmp]
fo.write(tmp)
fo.write(' ')
i = i + 1
continue
else:
if '0' <= str_new[i] <= '9':
sum_1 = 0
sum_2 = 0.0
flg_num = 0
x = 1
while i < len(str_new):
if '0' <= str_new[i] <= '9':
if flg_num == 0:
sum_1 = sum_1 * 10 + int(str_new[i])
i = i + 1
else:
sum_2 = sum_2 + int(str_new[i]) * (0.1 ** x)
x = x + 1
i = i + 1
else:
if str_new[i] == '.':
i = i + 1
flg_num = 1
if str_new[i] <= '0' or str_new[i] >= '9':
list_fin = list_fin + [sum_1]
fo.write(str(sum_1))
fo.write('.')
fo.write(str_new[i])
fo.write(' ')
num_end = i
i = i + 1
break
else:
continue
else:
num_end = i
if flg_num == 0:
if sum_1 not in list4:
list4.append(sum_1)
list4.append(40 + j)
j = j + 1
list_fin = list_fin + [sum_1]
fo.write(str(sum_1))
fo.write(' ')
break
else:
sum_2 = sum_2 + sum_1
if sum_2 not in list4:
list4.append(sum_2)
list4.append(40 + j)
j = j + 1
list_fin = list_fin + [sum_2]
fo.write(str(sum_2))
fo.write(' ')
break
i = num_end-1
else:
# = 与 ==
if str_new[i] == '=':
if str_new[i + 1] == '=':
list_fin = list_fin + ['==']
fo.write('==')
fo.write(' ')
i = i + 1
else:
list_fin = list_fin + ['=']
fo.write('=')
fo.write(' ')
else:
# < 与 <=
if str_new[i] == '>':
if str_new[i + 1] == '=':
list_fin = list_fin + ['>=']
fo.write('>=')
fo.write(' ')
i = i + 1
else:
list_fin = list_fin + ['>']
fo.write('>')
fo.write(' ')
else:
# + 与 ++
if str_new[i] == '+':
if str_new[i + 1] == '+':
list_fin = list_fin + ['++']
fo.write('++')
fo.write(' ')
i = i + 1
else:
list_fin = list_fin + ['+']
fo.write('+')
fo.write(' ')
else:
if str_new[i] == '/':
list_fin = list_fin + ['/']
fo.write('/')
fo.write(' ')
else:
if str_new[i] == '%':
list_fin = list_fin + ['%']
fo.write('%')
fo.write(' ')
else:
if str_new[i] == '"':
list_fin = list_fin + ['"']
fo.write('"')
fo.write(' ')
else:
if str_new[i] == ';':
list_fin = list_fin + [';']
fo.write(';')
fo.write(' ')
else:
if str_new[i] == '{':
list_fin = list_fin + ['{']
fo.write('{')
fo.write(' ')
else:
if str_new[i] == '}':
list_fin = list_fin + ['}']
fo.write('}')
fo.write(' ')
else:
if str_new[i] == '(':
list_fin = list_fin + ['(']
fo.write('(')
fo.write(' ')
else:
if str_new[i] == ')':
list_fin = list_fin + [')']
fo.write(')')
fo.write(' ')
else:
if str_new[i] == '\n':
fo.write('\n')
else:
# 错误判断,一般就是符号错误,最多2个,看下一位'='判断
if str_new[i] not in list2 and list1:
if i != len(str_new) - 1:
if str_new[i + 1] == '=':
list_fin = list_fin + [str_new[i: i + 2]]
fo.write(str_new[i: i + 2])
fo.write(' ')
i = i + 1
else:
list_fin = list_fin + [str_new[i]]
fo.write(str_new[i])
fo.write(' ')
else:
list_fin = list_fin + [str_new[i]]
fo.write(str_new[i])
fo.write(' ')
i = i + 1
return list_fin # 分割函数
开始准备要用到的数据结构
stack = [] # 模拟的一个堆栈
# G3 文法分析表
dict_G3 = {
'G3': ['int', 'float', ',', 'id', '#'],
'D': ['B G', 'B G', 'err', 'err', 'err'],
'B': ['int', 'float', 'err', 'err', 'err'],
'G': ['err', 'err', 'id R', 'id R', 'err'],
'R': ['err', 'err', ', id R', 'err', 'Null']
}
# G4 文法分析表
dict_G4 = {
'G4': ['id', 'num', '(', '+', '-', '*', '/', 'mod', ';', ')', '#'],
'L': ['E ; L', 'E ; L', 'E ; L', 'err', 'err', 'err', 'err', 'err', 'err', 'err', 'Null'],
'E': ['T e', 'T e', 'T e', 'err', 'err', 'err', 'err', 'err', 'err', 'err', 'err'],
'e': ['err', 'err', 'err', '+ T e', '- T e', 'err', 'err', 'err', 'Null', 'Null', 'err'],
'T': ['F t', 'F t', 'F t', 'err', 'err', 'err', 'err', 'err', 'err', 'err', 'err'],
't': ['err', 'err', 'err', 'Null', 'Null', '* F t', '/ F t', 'mod F t', 'Null', 'Null', 'err'],
'F': ['id', 'num', '( E )', 'err', 'err', 'err', 'err', 'err', 'err', 'err', 'err']
}
# G5 文法分析表1.0
dict_G5 = {
'G5': ['if', 'else', 'then', 'string', '#'],
'S': ['if E then S s', 'err', 'err', 'E', 'err'],
's': ['err', 'else S', 'err', 'err', 'Null'],
'E': ['err', 'err', 'err', 'string', 'err']
}
# 判断str的first是在哪个字典中,从而选择哪个函数进行预测分析
str_spl = splitfuc()
print str_spl # 全局变量,处理后分隔好的list
接下去新建一个list用来保存这个语法允许的首个词是啥,不允许的就用err代替
# G3_check 处理
list_check_G3_first = []
for index in range(len(dict_G3['G3'])):
list_check_G3_first = list_check_G3_first + [dict_G3['G3'][index]] #检查first的list_check,从而选择文法
for index in range(len(dict_G3['G3'])):
if dict_G3['D'][index] == 'err':
list_check_G3_first[index] = 'err'
# print 'xxxxxxxxxxx check_G3_first'
# print list_check_G3_first
# print 'xxxxxxxxxxx G3'
# print dict_G3['G3']
# G4_check 处理
list_check_G4_first = []
for index in range(len(dict_G4['G4'])):
list_check_G4_first = list_check_G4_first + [dict_G4['G4'][index]] # 检查first的list_check,从而选择文法
for index in range(len(dict_G4['G4'])):
if dict_G4['L'][index] == 'err':
list_check_G4_first[index] = 'err'
# print 'xxxxxxxxxxx check_G4_first'
# print list_check_G4_first
# print 'xxxxxxxxxxx G4'
# print dict_G4['G4']
# G5_check 处理
list_check_G5_first = []
for index in range(len(dict_G5['G5'])):
list_check_G5_first = list_check_G5_first + [dict_G5['G5'][index]] # 检查first的list_check,从而选择文法
for index in range(len(dict_G5['G5'])):
if dict_G5['S'][index] == 'err':
list_check_G5_first[index] = 'err'
# print 'xxxxxxxxxxx check_G5_first'
# print list_check_G5_first
# print 'xxxxxxxxxxx G5'
# print dict_G5['G5']
对G3文法的处理函数,应该是有史以来自己写代码时候注释最清楚的了hhh
def func_g3(str_spl):
# 处理成输入符号---- w# 替换 非在 G3 内的终结符为 'id' 可以放在G3函数中 输入的东西为处理好的str_spl
# 如果语句的首个词在list_check中,则选择那个文法的处理函数
str_spl_cpy = []
for index in range(len(str_spl)):
str_spl_cpy = str_spl_cpy + [str_spl[index]] # 复制list
for index in range(len(str_spl)):
if str_spl[index] in dict_G3['G3']:
continue
else:
str_spl_cpy[index] = 'id'
str_spl_cpy = str_spl_cpy + ['#']
# cpy比原来的list中多了 # 其他的id什么的都 index 从原list中返回真实的
print 'xxxxxxxxxxx spl_cpy'
print str_spl_cpy
# 开始遍历cpy的list,进行对stack的操作
stack = ['#', 'D'] # 初始状态
# for index in range(len(str_spl_cpy)):
# print 'xxxxxxxxxxx test stack'
# print dict_G3['D']
# print dict_G3[stack[1]]
# 开始循环
# 1.查看栈顶元素
# 2.目前的spl_cpy[index]在G3中的index_2是多少
# 3.栈顶元素对应的list[index_2](是否存在)如D[index_2]是‘B G’
# 4.弹出栈顶,按空格分开反向入栈
# 5.判断栈顶是否=spl_cpy[index] ----是则弹出栈顶元素并输出,index下一个;----不是则重复1-5
# 可以打包成G3处理函数
index = 0
list_after_check = []
# 开始循环,按照输入流中的一步一步走
while str_spl_cpy[index] != '#':
while stack[len(stack)-1] != str_spl_cpy[index]: # 推进栈中更新
list_temp = []
if str_spl_cpy[index] in dict_G3['G3']:
index_2 = dict_G3['G3'].index(str_spl_cpy[index])
else:
print 'error1 含有语法定义中未存在的元素'
break
if dict_G3[stack[len(stack)-1]][index_2] != 'err':
if dict_G3[stack[len(stack)-1]][index_2] == 'Null':
print '***弹出了:', stack[len(stack) - 1]
stack.remove(stack[len(stack) - 1])
else:
list_temp = dict_G3[stack[len(stack) - 1]][index_2].split(' ')
list_temp.reverse()
print 'temp\n', list_temp
print 'stack before \n', stack
stack.remove(stack[len(stack) - 1])
stack = stack + list_temp
print 'stack after\n', stack
else:
print 'error1 含有语法定义中未存在的元素'
break
print '弹出了:', stack[len(stack)-1]
print '是原来的:', str_spl[index]
list_after_check = list_after_check + [str_spl[index]]
stack.remove(stack[len(stack)-1])
index = index + 1
print '目前到达的输入流位置指向:', str_spl_cpy[index]
# 输入流指针已经到了最后,开始清空堆栈里的东西
while len(stack) > 1:
index_2 = dict_G3['G3'].index(str_spl_cpy[index])
if dict_G3[stack[len(stack) - 1]][index_2] != 'err':
if dict_G3[stack[len(stack)-1]][index_2] == 'Null':
print 'stack before \n', stack
print '弹出了:', stack[len(stack) - 1]
stack.remove(stack[len(stack) - 1])
print 'stack after\n', stack
else:
print 'error2 没有按照语法'
else:
print 'error1 有语法定义中未存在的元素'
print '这是语法G3,经过循环遍历之后的原来语句是:', list_after_check
# G3函数结束
对G4文法的处理函数
def func_g4(str_spl):
str_spl_cpy = []
for index in range(len(str_spl)):
str_spl_cpy = str_spl_cpy + [str_spl[index]] # 复制list
for index in range(len(str_spl)):
if str_spl[index] in dict_G4['G4']:
continue
else:
if isinstance(str_spl_cpy[index], int):
str_spl_cpy[index] = 'num'
else:
if 'a' <= str_spl_cpy[index][0] <= 'z' or 'A' <= str_spl_cpy[index][0] <= 'Z':
str_spl_cpy[index] = 'id'
else:
continue
str_spl_cpy = str_spl_cpy + ['#']
# cpy比原来的list中多了 # 其他的id什么的都 index 从原list中返回真实的
print 'xxxxxxxxxxx spl_cpy'
print str_spl_cpy
# 开始遍历cpy的list,进行对stack的操作
stack = ['#', 'L'] # 初始状态
# for index in range(len(str_spl_cpy)):
# print 'xxxxxxxxxxx test stack'
# print dict_G3['D']
# print dict_G3[stack[1]]
# 开始循环
# 1.查看栈顶元素
# 2.目前的spl_cpy[index]在G3中的index_2是多少
# 3.栈顶元素对应的list[index_2](是否存在)如D[index_2]是‘B G’
# 4.弹出栈顶,按空格分开反向入栈
# 5.判断栈顶是否=spl_cpy[index] ----是则弹出栈顶元素并输出,index下一个;----不是则重复1-5
# 可以打包成G3处理函数
index = 0
flg = 0
list_after_check = []
# 开始循环,按照输入流中的一步一步走
while str_spl_cpy[index] != '#':
while stack[len(stack) - 1] != str_spl_cpy[index]: # 推进栈中更新
list_temp = []
if str_spl_cpy[index] in dict_G4['G4']:
index_2 = dict_G4['G4'].index(str_spl_cpy[index])
else:
print 'error1 含有语法定义中未存在的元素'
flg = 1
break
if dict_G4[stack[len(stack) - 1]][index_2] != 'err':
if dict_G4[stack[len(stack) - 1]][index_2] == 'Null':
print '遇到ε弹出了:', stack[len(stack) - 1]
stack.remove(stack[len(stack) - 1])
else:
list_temp = dict_G4[stack[len(stack) - 1]][index_2].split(' ')
list_temp.reverse()
print 'temp\n', list_temp
print 'stack before \n', stack
stack.remove(stack[len(stack) - 1])
stack = stack + list_temp
print 'stack after\n', stack
else:
print 'error1 含有语法定义中未存在的元素'
flg = 1
break
if flg == 1:
break
print '弹出了:', stack[len(stack) - 1]
print '是原来的:', str_spl[index]
list_after_check = list_after_check + [str_spl[index]]
stack.remove(stack[len(stack) - 1])
index = index + 1
print '目前到达的输入流位置指向:', str_spl_cpy[index]
print '目前的stack里面有:', stack
print '目前到达的输入流位置指向:', str_spl_cpy[index]
print '目前的stack里面有:', stack
# 输入流指针已经到了最后,开始清空堆栈里的东西
if flg != 1:
while len(stack) > 1:
index_2 = dict_G4['G4'].index(str_spl_cpy[index])
if dict_G4[stack[len(stack) - 1]][index_2] != 'err':
if dict_G4[stack[len(stack) - 1]][index_2] == 'Null':
print 'stack before \n', stack
print '遇到ε弹出了:', stack[len(stack) - 1]
stack.remove(stack[len(stack) - 1])
print 'stack after\n', stack
else:
print 'error2 没有按照语法来写语句'
flg = 1
break
else:
flg = 1
print 'error2 没有按照语法来写语句(末尾缺少 ; )'
break
if flg != 1:
print '这是语法G4,经过循环遍历之后的原来语句是:', list_after_check
else:
print '虽然开头对应了文法G4,但是他里面存在不符合文法的字符或中间没按照文法写'
else:
print '虽然开头对应了文法G4,但是他里面存在不符合文法的字符或中间没按照文法写'
# G4函数结束
对G5文法的处理函数,后面堆栈操作基本都一个样,其实可以再写个堆栈操作函数的,那个时候怠惰了
def func_g5(str_spl):
str_temp = ''
list_temp_for_g5 = []
str_spl_cpy = []
for index in range(len(str_spl)):
str_spl_cpy = str_spl_cpy + [str_spl[index]] # 复制list
str_spl_cpy = str_spl_cpy + ['#']
# 先拼接起来
index = 0
while str_spl_cpy[index] != '#':
if str_spl_cpy[index] != 'if':
if str_spl_cpy[index] != 'else':
if str_spl_cpy[index] != 'then':
str_temp = str_temp + str_spl_cpy[index]
else:
list_temp_for_g5 = list_temp_for_g5 + [str_temp]
str_temp = ''
list_temp_for_g5 = list_temp_for_g5 + ['then']
else:
list_temp_for_g5 = list_temp_for_g5 + [str_temp]
str_temp = ''
list_temp_for_g5 = list_temp_for_g5 + ['else']
else:
list_temp_for_g5 = list_temp_for_g5 + [str_temp]
str_temp = ''
list_temp_for_g5 = list_temp_for_g5 + ['if']
index = index + 1
list_temp_for_g5 = list_temp_for_g5 + [str_temp]
while '' in list_temp_for_g5:
list_temp_for_g5.remove('')
print 'xxxxxxxxxxx list temp for g5'
print list_temp_for_g5
# 再替换str内容为string 准备处理
str_spl_cpy = []
for index in range(len(list_temp_for_g5)):
str_spl_cpy = str_spl_cpy + [list_temp_for_g5[index]] # 复制list
str_spl_cpy = str_spl_cpy + ['#']
str_spl_cpy_g5 = []
for index in range(len(str_spl_cpy)):
str_spl_cpy_g5 = str_spl_cpy_g5 + [str_spl_cpy[index]] # 复制list
for index in range(len(str_spl_cpy_g5)):
if str_spl_cpy_g5[index] != 'if':
if str_spl_cpy_g5[index] != 'else':
if str_spl_cpy_g5[index] != 'then':
if str_spl_cpy_g5[index] != '#':
str_spl_cpy_g5.insert(index, 'string')
str_spl_cpy_g5.remove(str_spl_cpy_g5[index + 1])
# cpy比原来的list中多了 # 其他的id什么的都 index 从原list中返回真实的
print 'xxxxxxxxxxx spl_cpy'
print str_spl_cpy
print 'xxxxxxxxxxx spl_cpy_g5'
print str_spl_cpy_g5
# 开始遍历cpy的list,进行对stack的操作
stack = ['#', 'S'] # 初始状态
# 开始循环
# 1.查看栈顶元素
# 2.目前的spl_cpy[index]在G3中的index_2是多少
# 3.栈顶元素对应的list[index_2](是否存在)如D[index_2]是‘B G’
# 4.弹出栈顶,按空格分开反向入栈
# 5.判断栈顶是否=spl_cpy[index] ----是则弹出栈顶元素并输出,index下一个;----不是则重复1-5
# 可以打包成G3处理函数
index = 0
flg = 0
list_after_check = []
# 开始循环,按照输入流中的一步一步走
while str_spl_cpy_g5[index] != '#':
while stack[len(stack) - 1] != str_spl_cpy_g5[index]: # 推进栈中更新
list_temp = []
if str_spl_cpy_g5[index] in dict_G5['G5']:
index_2 = dict_G5['G5'].index(str_spl_cpy_g5[index])
else:
print 'error1 含有语法定义中未存在的元素'
flg = 1
break
if dict_G5[stack[len(stack) - 1]][index_2] != 'err':
if dict_G5[stack[len(stack) - 1]][index_2] == 'Null':
print '遇到ε弹出了:', stack[len(stack) - 1]
stack.remove(stack[len(stack) - 1])
else:
list_temp = dict_G5[stack[len(stack) - 1]][index_2].split(' ')
list_temp.reverse()
print 'temp\n', list_temp
print 'stack before \n', stack
stack.remove(stack[len(stack) - 1])
stack = stack + list_temp
print 'stack after\n', stack
else:
print 'error1 含有语法定义中未存在的元素'
flg = 1
break
if flg == 1:
break
print '弹出了:', stack[len(stack) - 1]
print '是原来的:', str_spl_cpy[index]
list_after_check = list_after_check + [str_spl_cpy[index]]
stack.remove(stack[len(stack) - 1])
index = index + 1
print '目前到达的输入流位置指向:', str_spl_cpy_g5[index]
print '目前的stack里面有:', stack
print '目前到达的输入流位置指向:', str_spl_cpy_g5[index]
print '目前的stack里面有:', stack
# 输入流指针已经到了最后,开始清空堆栈里的东西
if flg != 1:
while len(stack) > 1:
index_2 = dict_G5['G5'].index(str_spl_cpy_g5[index])
if dict_G5[stack[len(stack) - 1]][index_2] != 'err':
if dict_G5[stack[len(stack) - 1]][index_2] == 'Null':
print 'stack before \n', stack
print '遇到ε弹出了:', stack[len(stack) - 1]
stack.remove(stack[len(stack) - 1])
print 'stack after\n', stack
else:
print 'error2 没有按照语法来写语句'
flg = 1
break
else:
flg = 1
print 'error2 没有按照语法来写语句(末尾缺少 ; )'
break
if flg != 1:
print '这是语法G5,经过循环遍历之后的原来语句是:', list_after_check
else:
print '虽然开头对应了文法G5,但是他里面存在不符合文法的字符或中间没按照文法写'
else:
print '虽然开头对应了文法G5,但是他里面存在不符合文法的字符或中间没按照文法写'
# G5函数结束
判断哪个文法的函数
def choose_gn(str_spl):
str_cpy_g3 = []
str_cpy_g4 = []
str_cpy_g3 = str_cpy_g3 + [str_spl[0]]
str_cpy_g4 = str_cpy_g4 + [str_spl[0]]
if str_cpy_g3[0] not in dict_G3['G3']: # G3
str_cpy_g3[0] = 'id'
if isinstance(str_cpy_g4[0], int): # G4
str_cpy_g4[0] = 'num'
else:
if 'a' <= str_cpy_g4[0][0] <= 'z' or 'A' <= str_cpy_g4[0][0] <= 'Z':
str_cpy_g4[0] = 'id'
if str_spl[0] == 'if':
func_g5(str_spl)
else:
if str_cpy_g3[0] in list_check_G3_first:
func_g3(str_spl)
else:
if str_cpy_g4[0] in list_check_G4_first:
func_g4(str_spl)
else:
print 'err3,目前没有文法匹配该语句'
最后执行
choose_gn(str_spl)
代码结束
问题讨论
本来还想再进一步对if else 语句中的嵌套语句做一个嵌套分析的难度应该也不大,但是最后就是越改越乱了