diff --git a/semantic/rule.py b/semantic/rule.py index c1e47cf..0447699 100644 --- a/semantic/rule.py +++ b/semantic/rule.py @@ -1082,6 +1082,195 @@ class LocalDefineFollow31E(SemanticRule): ) +# 32 pass + + +# 33 pass + +# 34 + +class NormalStatement34C1(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.id = node.get_pre_brother(1).lexical + node.fun = node.parent.fun +class NormalStatement34E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.code = node.children[1].code + +# 35 +class NormalStatementFollow35C0(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.fun = node.parent.fun + node.id = node.parent.id + +class NormalStatementFollow35C2(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun +class NormalStatement35E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + if node.children[0].type=='var': + for c in node.children[2].code: + node.code.append(c) + node.code.append(f"{node.children[2].name} , = , _ , {node.id}") + if node.children[0].type =='array': + for c in node.children[0].code: + node.code.append(c) + for c in node.children[2].code: + node.code.append(c) + node.code.append(f"{node.children[2].name} , = , {node.children[0].name} , {node.children[0].id}") + +# 36 +class NormalStatementFollow36C0(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.fun = node.parent.fun + node.id = node.parent.id + + +class NormalStatementFollow36E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.id = node.parent.id + for c in node.children[0].code: + node.code.append(c) + node.code.append(f"call , _ , {node.id} , {str(symbol_table_pool.query(node.id).get_params_num())}") + +# 37 +class VarFollow37E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.type = 'var' + +# 38 +class VarFollow38E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.type = 'array' + for c in node.children[1].code: + node.code.append(c) + node.name = node.children[1].name + + +# 39 +class CallFollow39C1(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.fun = node.parent.fun + node.id = node.parent.id + +class CallFollow39E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + if node.children[1].num != symbol_table_pool.query(node.id).get_params_num(): + print("错误 : 调用函数的参数与实际参数不匹配") + for c in node.children[1].code: + node.code.append(c) + + + +# 40 +class CallParams40E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.num = node.children[0].num + for c in node.children[0].code: + node.code.append(c) +class CallParams40C0(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.id = node.parent.id + node.fun = node.parent.fun + +# 41 +class CallParams41E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.num = 0 + +# 42 +class CallParamList42C0(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.fun = node.parent.fun + node.id = node.parent.id +class CallParamList42E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.num = 1 + node.children[1].num + for c in node.children[0:1].code: + node.code.append(c) + +# 43 +class CallParamFollow43C1(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.id = node.parent.id + node.fun = node.parent.fun +class CallParamFollow43E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + for c in node.children[1].code: + node.code.append(c) + for c in node.children[2].code: + node.code.append(c) + node.num = 1 + node.children[2].num + +# 44 +class CallParamFollow44E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.num = 0 + +# 45 +class SelectionStatement45E(SemanticRule): + def execute(self): + self.__rule(self.node) + def __rule(self, node): + node.fun = node.parent.fun + node.id = node.parent.id + # node.code.append(f"if , _ , _ , {node.children[5]}") 这里应该是跳转的地址 , 可是跳转到哪呢 ? + for c in node.children[1].code: + node.code.append(c) + # 这里加一个if跳转 + for c in node.children[3].code: + node.code.append(c) + + for c in node.children[5].code: + node.code.append(c) + + + + + + + + + class Exp52C0(SemanticRule): def execute(self): self.__rule(self.node) diff --git a/syntax/rule.py b/syntax/rule.py index 7b7d4bb..e21606e 100644 --- a/syntax/rule.py +++ b/syntax/rule.py @@ -536,6 +536,58 @@ productions = [ # Production('selection-statement', ['if', 'exp', 'left-brace', 'code-list', 'right-brace', 'selection-follow'], # None, [None, None, None, None, None, None], None, # ), + # 33 + Production('normal-statement', ['semicolon'], + None, [None], None, + ), + # 34 + Production('normal-statement', ['id', 'normal-statement-follow'], + None, [None, 'NormalStatement34C1'], 'NormalStatement34E', + ), + # 35 + Production('normal-statement-follow', ['var-follow', 'equal', 'exp', 'semicolon'], + None, ['NormalStatement35C0', None, 'NormalStatementFollow35C0', None], 'NormalStatement35E', + ), + # 36 + Production('normal-statement-follow', ['call-follow', 'semicolon'], + None, ['NormalStatementFollow36C0',None], 'NormalStatementFollow36E', + ), + # 37 + Production('var-follow', [], + None, [], 'VarFollow37E', + ), + # 38 + Production('var-follow', ['left-bracket', 'exp', 'right-bracket'], + None, [None, None, None], 'VarFollow38E', + ), + # 39 + Production('call-follow', ['left-parentheses', 'call-params', 'right-parentheses'], + None, [None, 'CallFollow39C1', None], 'CallFollow39E', + ), + # 40 + Production('call-params', ['call-param-list'], + None, ['SemanticRule'], 'CallParams40E', + ), + # 41 + Production('call-params', [], + None, [], 'CallParams41E', + ), + # 42 + Production('call-param-list', ['exp', 'call-param-follow'], + None, ['CallParamList42C0', None], 'CallParamList42E', + ), + # 43 + Production('call-param-follow', ['comma', 'exp', 'call-param-follow'], + None, [None, 'CallParamFollow43C1', None], 'CallParamFollow43E', + ), + # 44 + Production('call-param-follow', [], + None, [], 'CallParamFollow44E', + ), + # 45 + Production('selection-statement', ['if', 'exp', 'left-brace', 'code-list', 'right-brace', 'selection-follow'], + None, [None, None, None, None, None, None], None, + ), # # 46 # Production('selection-follow', ['else', 'left-brace', 'code-list', 'right-brace'], # None, [None, None, None, None], None,