添加一些语法
This commit is contained in:
154
parser_c.py
154
parser_c.py
@@ -3,28 +3,160 @@ import sys
|
||||
import dataclasses
|
||||
from lex_c import lex_token
|
||||
from lex_c import lex
|
||||
import lex_c
|
||||
from node_declear import dist_node_type_struct
|
||||
from node_declear import dist_node_type_union
|
||||
from node_declear import dist_node_type_enum
|
||||
from node_declear import dist_node_type_typedef
|
||||
|
||||
_NodeTypeTable=[
|
||||
"file","vdecl","fdef"
|
||||
]
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class node:
|
||||
name:str
|
||||
next:None
|
||||
chid:None
|
||||
token_list:list[lex_token]
|
||||
type:str="base"
|
||||
token_list:list[lex_token]=dataclasses.field(default_factory=list)
|
||||
|
||||
# 变量声明节点
|
||||
# 文件节点
|
||||
@dataclasses.dataclass
|
||||
class node_vdecl(node):
|
||||
vvalue:None
|
||||
vtype:str
|
||||
vattr:list[str]
|
||||
class node_file(node):
|
||||
type:str="file"
|
||||
body:list=dataclasses.field(default_factory=list)
|
||||
|
||||
# 变量定义节点
|
||||
@dataclasses.dataclass
|
||||
class node_variable_def(node):
|
||||
type:str="variable_def"
|
||||
vvalue=None
|
||||
vtype:str="unknown"
|
||||
vattr:list[str]=dataclasses.field(default_factory=list)
|
||||
|
||||
# 结构体声明节点
|
||||
@dataclasses.dataclass
|
||||
class node_struct_decl(node):
|
||||
type:str="struct_decl"
|
||||
|
||||
# 结构体定义节点
|
||||
@dataclasses.dataclass
|
||||
class node_struct_def(node):
|
||||
type:str="struct_def"
|
||||
body:list[node_variable_def]=dataclasses.field(default_factory=list)
|
||||
|
||||
# 联合体声明节点
|
||||
@dataclasses.dataclass
|
||||
class node_union_decl(node):
|
||||
type:str="union_decl"
|
||||
|
||||
# 联合体定义节点
|
||||
@dataclasses.dataclass
|
||||
class node_union_def(node):
|
||||
type:str="union_def"
|
||||
body:list[node_variable_def]=dataclasses.field(default_factory=list)
|
||||
|
||||
# 枚举声明节点
|
||||
@dataclasses.dataclass
|
||||
class node_enum_decl(node):
|
||||
type:str="enum_decl"
|
||||
|
||||
# 枚举定义节点
|
||||
@dataclasses.dataclass
|
||||
class node_enum_def(node):
|
||||
type:str="enum_def"
|
||||
body:list[dict]=dataclasses.field(default_factory=list)
|
||||
|
||||
# 函数声明节点
|
||||
@dataclasses.dataclass
|
||||
class node_func_decl(node):
|
||||
type:str="func_decl"
|
||||
rettype:str="unknown"
|
||||
retattr:list[str]=dataclasses.field(default_factory=list)
|
||||
para:list[node_variable_def]=dataclasses.field(default_factory=list)
|
||||
|
||||
#typedef 节点
|
||||
@dataclasses.dataclass
|
||||
class node_typedef(node):
|
||||
type:str="typedef"
|
||||
attr:list[str]=dataclasses.field(default_factory=list)
|
||||
body:node=None
|
||||
|
||||
# 函数定义节点
|
||||
@dataclasses.dataclass
|
||||
class node_fdef(node):
|
||||
rettype:str
|
||||
class node_func_def(node):
|
||||
type:str="func_def"
|
||||
rettype:str="unknown"
|
||||
retattr:list[str]=dataclasses.field(default_factory=list)
|
||||
para:list[node_variable_def]=dataclasses.field(default_factory=list)
|
||||
body:list[node]=dataclasses.field(default_factory=list)
|
||||
|
||||
|
||||
# 找到闭合的括号
|
||||
def find_close(token_list:list[lex_token],token:tuple[int,int]):
|
||||
if token_list[0].token!=token[0]:
|
||||
return 0
|
||||
num=0
|
||||
for index,item in enumerate(token_list):
|
||||
if(item.token==token[0]):
|
||||
num+=1
|
||||
elif(item.token==token[1]):
|
||||
num-=1
|
||||
if(num==0):
|
||||
return index
|
||||
raise Exception(f"没有找到闭合的符号 {token[1]}")
|
||||
|
||||
# 找到一个完整的语句
|
||||
def find_sentence(token_list:list[lex_token]):
|
||||
bracket_flag=False
|
||||
index=0
|
||||
while index<len(token_list):
|
||||
if(token_list[index].token==lex_c.TOKEN("(")):
|
||||
bracket_index=find_close(token_list[index:],(lex_c.TOKEN("("),lex_c.TOKEN(")")))
|
||||
if(bracket_index>0):
|
||||
bracket_flag=True
|
||||
index+=bracket_index
|
||||
elif(token_list[index].token==lex_c.TOKEN("{")):
|
||||
bracket_index=find_close(token_list[index:],(lex_c.TOKEN("{"),lex_c.TOKEN("}")))
|
||||
if(bracket_index>0):
|
||||
index+=bracket_index
|
||||
if(bracket_flag==True):
|
||||
return token_list[:index+1]
|
||||
elif(token_list[index].token==lex_c.TOKEN(";")):
|
||||
return token_list[:index+1]
|
||||
index+=1
|
||||
raise Exception(f"没有找到完整的语句")
|
||||
|
||||
|
||||
|
||||
# 判断一个语句的类型
|
||||
def dist_node_type(token_list:list[lex_token]):
|
||||
if(token_list[0].token==lex_c.TOKEN_EXTERN):
|
||||
token_list=token_list[1:]
|
||||
if(token_list[-1].token==lex_c.TOKEN(";")):
|
||||
token_list=token_list[:-1]
|
||||
if(token_list[0].token==lex_c.TOKEN_STRUCT):
|
||||
return dist_node_type_struct(token_list=token_list)
|
||||
if(token_list[0].token==lex_c.TOKEN_UNION):
|
||||
return dist_node_type_union(token_list=token_list)
|
||||
if(token_list[0].token==lex_c.TOKEN_ENUM):
|
||||
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)
|
||||
|
||||
raise Exception(f"无法处理的token类型 {token_list[0]}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("main.c",mode='rb') as f:
|
||||
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,body=[])
|
||||
while len(token_list)>0:
|
||||
sentence=find_sentence(token_list)
|
||||
node_d=dist_node_type(sentence)
|
||||
file.body.append(node_d)
|
||||
print('找到一个语句:')
|
||||
for item in sentence:
|
||||
print(f"\t{item}")
|
||||
token_list=token_list[len(sentence):]
|
Reference in New Issue
Block a user