添加一些节点类型
This commit is contained in:
@@ -12,7 +12,7 @@ class variable_type(object):
|
||||
self.attr=attr
|
||||
self.size=size
|
||||
self.pack=4 # 默认4字节对齐
|
||||
self.member:list[(variable_type,str)]=[]
|
||||
self.member:list[tuple[variable_type,str]]=[]
|
||||
if not (limit is None):
|
||||
self.min=limit[0]
|
||||
self.max=limit[1]
|
||||
|
173
parser_c.py
173
parser_c.py
@@ -16,11 +16,12 @@ class node:
|
||||
type:str="base"
|
||||
token_list:list[lex_token]=dataclasses.field(default_factory=list)
|
||||
child:list=dataclasses.field(default_factory=list)
|
||||
def complite(self):
|
||||
print(f"complite {self.type}")
|
||||
# 文件节点
|
||||
@dataclasses.dataclass
|
||||
class node_file(node):
|
||||
type:str="file"
|
||||
|
||||
# 变量定义节点
|
||||
@dataclasses.dataclass
|
||||
class node_variable_def(node):
|
||||
@@ -71,6 +72,51 @@ class node_typedef(node):
|
||||
class node_func_def(node):
|
||||
type:str="func_def"
|
||||
|
||||
# switch节点
|
||||
@dataclasses.dataclass
|
||||
class node_switch(node):
|
||||
type:str="switch"
|
||||
|
||||
# case节点
|
||||
@dataclasses.dataclass
|
||||
class node_case(node):
|
||||
type:str="case"
|
||||
|
||||
# default
|
||||
@dataclasses.dataclass
|
||||
class node_default(node):
|
||||
type:str="default"
|
||||
|
||||
# break节点
|
||||
@dataclasses.dataclass
|
||||
class node_break(node):
|
||||
type:str="break"
|
||||
|
||||
# return节点
|
||||
@dataclasses.dataclass
|
||||
class node_return(node):
|
||||
type:str="return"
|
||||
|
||||
# 函数调用节点
|
||||
@dataclasses.dataclass
|
||||
class node_call(node):
|
||||
type:str="call"
|
||||
|
||||
# 变量操作节点
|
||||
@dataclasses.dataclass
|
||||
class node_opt(node):
|
||||
type:str="opt_var"
|
||||
|
||||
# 符号节点
|
||||
@dataclasses.dataclass
|
||||
class node_symbol(node):
|
||||
type:str="symbol"
|
||||
|
||||
# string节点
|
||||
@dataclasses.dataclass
|
||||
class node_string(node):
|
||||
type:str="string"
|
||||
|
||||
|
||||
# 找到闭合的括号
|
||||
def find_close(token_list:list[lex_token],token:tuple[int,int]):
|
||||
@@ -87,7 +133,7 @@ def find_close(token_list:list[lex_token],token:tuple[int,int]):
|
||||
raise Exception(f"没有找到闭合的符号 {token[1]}")
|
||||
|
||||
# 找到一个完整的语句
|
||||
def find_sentence(token_list:list[lex_token]):
|
||||
def find_sentence(token_list:list[lex_token],sep:list[int]=[lex_c.TOKEN(";"),lex_c.TOKEN(":")]):
|
||||
bracket_flag=False
|
||||
index=0
|
||||
while index<len(token_list):
|
||||
@@ -102,7 +148,7 @@ def find_sentence(token_list:list[lex_token]):
|
||||
index+=bracket_index
|
||||
if(bracket_flag==True):
|
||||
return token_list[:index+1]
|
||||
elif(token_list[index].token==lex_c.TOKEN(";")):
|
||||
elif(token_list[index].token in sep):
|
||||
return token_list[:index+1]
|
||||
index+=1
|
||||
raise Exception(f"没有找到完整的语句")
|
||||
@@ -133,7 +179,7 @@ def dist_node_type_struct(token_list:list[lex_token]):
|
||||
sentence=find_sentence(token_list_local)
|
||||
v_list.append(dist_node_type(token_list=sentence))
|
||||
token_list_local=token_list_local[len(sentence):]
|
||||
return node_struct_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,body=v_list)
|
||||
return node_struct_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,child=v_list)
|
||||
raise Exception(f"语法错误 {token_list[0]}")
|
||||
|
||||
|
||||
@@ -152,7 +198,7 @@ def dist_node_type_union(token_list:list[lex_token]):
|
||||
sentence=find_sentence(token_list_local)
|
||||
v_list.append(dist_node_type(token_list=sentence))
|
||||
token_list_local=token_list_local[len(sentence):]
|
||||
return node_union_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,body=v_list)
|
||||
return node_union_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,child=v_list)
|
||||
raise Exception(f"语法错误 {token_list[0]}")
|
||||
|
||||
|
||||
@@ -165,24 +211,8 @@ def dist_node_type_enum(token_list:list[lex_token]):
|
||||
elif(token_list[2].token==lex_c.TOKEN("{")):
|
||||
if not token_list[-1].token==lex_c.TOKEN("}"):
|
||||
raise Exception("没有出现预期的符号 '}'")
|
||||
token_list_local=token_list[3:-1]
|
||||
index=0
|
||||
v_list:list[dict]=[]
|
||||
while len(token_list_local)>0:
|
||||
if(token_list_local[0].token==lex_c.TOKEN_SYMBOL):
|
||||
key=token_list_local[0].buff.decode("utf-8")
|
||||
if(token_list_local[1].token==lex_c.TOKEN("=") and token_list_local[2].token==lex_c.TOKEN_NUM):
|
||||
index=int(token_list_local[2].buff.decode("utf-8"))
|
||||
token_list_local=token_list_local[3:]
|
||||
else:
|
||||
index+=1
|
||||
token_list_local=token_list_local[1:]
|
||||
v_list.append({key:index})
|
||||
if(len(token_list_local)>0):
|
||||
if(token_list_local[0].token!=lex_c.TOKEN(",")):
|
||||
raise Exception(f"枚举类型应该使用 ',' 分隔符")
|
||||
token_list_local=token_list_local[1:]
|
||||
return node_enum_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,body=v_list)
|
||||
# token_list_local=token_list[3:-1]
|
||||
return node_enum_def(name=token_list[1].buff.decode("utf-8"),token_list=token_list,child=[])
|
||||
raise Exception(f"语法错误 {token_list[0]}")
|
||||
|
||||
|
||||
@@ -220,13 +250,58 @@ def dist_node_type_typedef(token_list:list[lex_token]):
|
||||
token_list=token_list[1:]
|
||||
if(len(token_list)>1):
|
||||
raise Exception(f"意外的token {token_list[0]}")
|
||||
return node_typedef(name=name,token_list=token_list_local,attr=attr,body=None)
|
||||
return node_typedef(name=name,token_list=token_list_local,child=[])
|
||||
raise Exception(f"语法错误 {token_list[0]}")
|
||||
|
||||
|
||||
|
||||
# 找到子节点
|
||||
def find_child(token_list:list[lex_token],seq:list[int]=[lex_c.TOKEN(";"),lex_c.TOKEN(":")]):
|
||||
child=[]
|
||||
for i in range(len(token_list)):
|
||||
if(token_list[i].token==lex_c.TOKEN("{")):
|
||||
token_list_local=token_list[i+1:-1]
|
||||
break
|
||||
while len(token_list_local)>0:
|
||||
sentence=find_sentence(token_list_local,seq)
|
||||
node_d=dist_node_type(sentence)
|
||||
child.append(node_d)
|
||||
token_list_local=token_list_local[len(sentence):]
|
||||
return child
|
||||
|
||||
|
||||
def dist_node_type_funcdef(token_list:list[lex_token]):
|
||||
for i in range(len(token_list)):
|
||||
if(token_list[i].token==lex_c.TOKEN_SYMBOL):
|
||||
name=token_list[i].buff.decode("utf-8")
|
||||
break
|
||||
return node_func_def(name=[name],token_list=token_list,child=find_child(token_list))
|
||||
|
||||
def dist_node_type_funcdecl(token_list:list[lex_token]):
|
||||
for i in range(len(token_list)):
|
||||
if(token_list[i].token==lex_c.TOKEN_SYMBOL):
|
||||
name=token_list[i].buff.decode("utf-8")
|
||||
return node_func_decl(name=[name],token_list=token_list,child=[])
|
||||
raise Exception(f"函数声明格式错误 {token_list[0]}")
|
||||
|
||||
|
||||
# 第一个token是symbol的处理
|
||||
def dist_node_type_symbol(token_list:list[lex_token]):
|
||||
# 变量赋值或函数调用
|
||||
if(token_list[1].token == lex_c.TOKEN("(")):
|
||||
child=find_child(token_list=token_list[2:-1])
|
||||
return node_call("call",token_list=token_list,child=child)
|
||||
elif(token_list[1].token in [
|
||||
lex_c.TOKEN("="),lex_c.TOKEN_ASSIG_ADD,lex_c.TOKEN_ASSIG_DIV,lex_c.TOKEN_ASSIG_LSH,
|
||||
lex_c.TOKEN_ASSIG_MUL,lex_c.TOKEN_ASSIG_RSH,lex_c.TOKEN_ASSIG_SUB]):
|
||||
name=token_list[1].name
|
||||
child=[node_symbol(name=token_list[0].buff.decode("utf-8"),token_list=token_list[:1]),
|
||||
dist_node_type(token_list=token_list[2:])]
|
||||
return node_opt(name=name,token_list=token_list,child=child)
|
||||
else:
|
||||
# 没有赋值属性的操作
|
||||
name=token_list[1].name
|
||||
return node_opt(name=name,token_list=token_list,child=[])
|
||||
|
||||
|
||||
|
||||
@@ -255,20 +330,58 @@ def dist_node_type(token_list:list[lex_token]):
|
||||
return dist_node_type_enum(token_list=token_list)
|
||||
if(token_list[0].token==lex_c.TOKEN_TYPEDEF):
|
||||
return dist_node_type_typedef(token_list=token_list)
|
||||
if(token_list[0].token==lex_c.TOKEN_SWITCH):
|
||||
child=find_child(token_list)
|
||||
return node_switch(name="switch",token_list=token_list,child=child)
|
||||
if(token_list[0].token==lex_c.TOKEN_CASE):
|
||||
return node_case(name="case",token_list=token_list,child=[])
|
||||
if(token_list[0].token==lex_c.TOKEN_DEFAULT):
|
||||
return node_default(name="default",token_list=token_list,child=[])
|
||||
if(token_list[0].token==lex_c.TOKEN_BREAK):
|
||||
return node_break(name="break",token_list=token_list,child=[])
|
||||
if(token_list[0].token==lex_c.TOKEN_RETURN):
|
||||
return node_return(name="return",token_list=token_list,child=[])
|
||||
if(token_list[0].token==lex_c.TOKEN_STRING):
|
||||
return node_string(name="string",token_list=token_list,child=[])
|
||||
|
||||
raise Exception(f"无法处理的token类型 {token_list[0]}")
|
||||
if(token_list[-1].token==lex_c.TOKEN(")")):
|
||||
# 函数声明
|
||||
return dist_node_type_funcdecl(token_list)
|
||||
elif(token_list[-1].token==lex_c.TOKEN("}")):
|
||||
# 函数定义
|
||||
return dist_node_type_funcdef(token_list=token_list)
|
||||
elif(token_list[0].token==lex_c.TOKEN_SYMBOL):
|
||||
# 变量赋值或函数调用
|
||||
return dist_node_type_symbol(token_list=token_list)
|
||||
else:
|
||||
# 变量定义
|
||||
for i in range(len(token_list)):
|
||||
if(token_list[i].token==lex_c.TOKEN_SYMBOL):
|
||||
name=token_list[i].buff.decode("utf-8")
|
||||
return node_variable_def(name=[name],token_list=token_list,child=[])
|
||||
raise Exception(f"变量定义格式错误 {token_list[0]}")
|
||||
|
||||
|
||||
def print_node(n:node,deep:int):
|
||||
s="|"*deep
|
||||
print(f"{s} {n.type} {n.name}")
|
||||
# n.complite()
|
||||
if (not n.child is None) and len(n.child)>0:
|
||||
for item in n.child:
|
||||
print_node(item,deep+1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
file_name="main.c"
|
||||
with open(file_name,mode='rb') as f:
|
||||
token_list=lex(f.read())
|
||||
file=node_file(name=file_name,token_list=token_list,child=[])
|
||||
file=node_file(name=file_name,token_list=token_list)
|
||||
while len(token_list)>0:
|
||||
sentence=find_sentence(token_list)
|
||||
node_d=dist_node_type(sentence)
|
||||
file.child.append(node_d)
|
||||
print('找到一个语句:')
|
||||
for item in sentence:
|
||||
print(f"\t{item}")
|
||||
token_list=token_list[len(sentence):]
|
||||
# print('找到一个语句:')
|
||||
# for item in sentence:
|
||||
# print(f"\t{item}")
|
||||
token_list=token_list[len(sentence):]
|
||||
print_node(file,0)
|
Reference in New Issue
Block a user