diff --git a/semantic/rule.py b/semantic/rule.py index 0447699..7edee65 100644 --- a/semantic/rule.py +++ b/semantic/rule.py @@ -271,14 +271,14 @@ class SemanticRuleFactory: # 28 if rule_key == 'Code28C0': return Code28C0(node) - if rule_key == 'Code28C0': - return Code28C0(node) + if rule_key == 'Code28E': + return Code28E(node) # 29 if rule_key == 'Code29C0': return Code29C0(node) - if rule_key == 'Code29C0': - return Code29C0(node) + if rule_key == 'Code29E': + return Code29E(node) # 30 if rule_key == 'LocalDefine30C0': @@ -300,6 +300,106 @@ class SemanticRuleFactory: if rule_key == 'LocalDefineFollow31E': return LocalDefineFollow31E(node) + # 34 + if rule_key == 'NormalStatement34C1': + return NormalStatement34C1(node) + if rule_key == 'NormalStatement34E': + return NormalStatement34E(node) + + # 35 + if rule_key == 'NormalStatementFollow35C0': + return NormalStatementFollow35C0(node) + if rule_key == 'NormalStatementFollow35C2': + return NormalStatementFollow35C2(node) + if rule_key == 'NormalStatement35E': + return NormalStatement35E(node) + + # 36 + if rule_key == 'NormalStatementFollow36C0': + return NormalStatementFollow36C0(node) + if rule_key == 'NormalStatementFollow36E': + return NormalStatementFollow36E(node) + + # 37 + if rule_key == 'VarFollow37E': + return VarFollow37E(node) + + # 38 + if rule_key == 'VarFollow38E': + return VarFollow38E(node) + + # 39 + if rule_key == 'CallFollow39C1': + return CallFollow39C1(node) + if rule_key == 'CallFollow39E': + return CallFollow39E(node) + + # 40 + if rule_key == 'CallParams40C0': + return CallParams40C0(node) + if rule_key == 'CallParams40E': + return CallParams40E(node) + + # 41 + if rule_key == 'CallParams41E': + return CallParams41E(node) + + # 42 + if rule_key == 'CallParamList42C0': + return CallParamList42C0(node) + if rule_key == 'CallParamList42E': + return CallParamList42E(node) + + # 43 + if rule_key == 'CallParamFollow43C1': + return CallParamFollow43C1(node) + if rule_key == 'CallParamFollow43E': + return CallParamFollow43E(node) + + # 44 + if rule_key == 'CallParamFollow44E': + return CallParamFollow44E(node) + + # 45 + if rule_key == 'SelectionStatement45C1': + return SelectionStatement45C1(node) + if rule_key == 'SelectionStatement45C3': + return SelectionStatement45C3(node) + if rule_key == 'SelectionStatement45C5': + return SelectionStatement45C5(node) + if rule_key == 'SelectionStatement45E': + return SelectionStatement45E(node) + + # 46 + if rule_key == 'SelectionFollow46C2': + return SelectionFollow46C2(node) + if rule_key == 'SelectionFollow46E': + return SelectionFollow46E(node) + + # 47 + if rule_key == 'SelectionFollow47E': + return SelectionFollow47E(node) + + # 48 + if rule_key == 'IterationStatement48C1': + return IterationStatement48C1(node) + if rule_key == 'IterationStatement48C3': + return IterationStatement48C3(node) + if rule_key == 'IterationStatement48E': + return IterationStatement48E(node) + + # 49 + if rule_key == 'ReturnStatement49E': + return ReturnStatement49E(node) + + # 50 + if rule_key == 'ReturnFollow50E': + return ReturnFollow50E(node) + + # 51 + if rule_key == 'ReturnFollow51E': + return ReturnFollow51E(node) + # 52 if rule_key == 'Exp52C0': return Exp52C0(node) @@ -545,6 +645,7 @@ class FunDefine5C7(SemanticRule): def __rule(self, node): node.fun = node.get_pre_brother(6).lexical + class FunDefine5E(SemanticRule): def execute(self): self.__rule(self.node) @@ -1095,11 +1196,13 @@ class NormalStatement34C1(SemanticRule): 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 + for c in node.children[1].code: + node.code.append(c) # 35 class NormalStatementFollow35C0(SemanticRule): @@ -1115,6 +1218,7 @@ class NormalStatementFollow35C2(SemanticRule): def __rule(self, node): node.fun = node.parent.fun + class NormalStatement35E(SemanticRule): def execute(self): self.__rule(self.node) @@ -1122,13 +1226,13 @@ class NormalStatement35E(SemanticRule): 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}") + 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}") + node.code.append(f"([]= , {node.children[2].name}, {node.children[0].id} , {node.children[0].name})") # 36 class NormalStatementFollow36C0(SemanticRule): @@ -1143,10 +1247,9 @@ 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())}") + node.code.append(f"(call, {node.id}, {str(symbol_table_pool.query(node.id).get_params_num())}, _)") # 37 class VarFollow37E(SemanticRule): @@ -1178,10 +1281,14 @@ 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) + if symbol_table_pool.fun_table.exist(node.id): + if node.children[1].num != symbol_table_pool.query(node.id).get_params_num(): + self.errors.append(f"错误 : 调用函数{node.id}的参数与实际参数不匹配") + for c in node.children[1].code: + node.code.append(c) + else: + self.errors.append(f"错误 : 调用函数{node.id}不存在") + @@ -1214,13 +1321,19 @@ class CallParamList42C0(SemanticRule): 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: + for c in node.children[0].code: node.code.append(c) + for c in node.children[1].code: + node.code.append(c) + node.code.append(f"(param, _, _, {node.children[0].name})") + for name in node.children[1].names: + node.code.append(f"(param, _, _, {name})") # 43 class CallParamFollow43C1(SemanticRule): @@ -1229,6 +1342,7 @@ class CallParamFollow43C1(SemanticRule): def __rule(self, node): node.id = node.parent.id node.fun = node.parent.fun + class CallParamFollow43E(SemanticRule): def execute(self): self.__rule(self.node) @@ -1238,36 +1352,148 @@ class CallParamFollow43E(SemanticRule): for c in node.children[2].code: node.code.append(c) node.num = 1 + node.children[2].num + node.names.append(node.children[1].name) + for name in node.children[2].names: + node.names.append(name) -# 44 class CallParamFollow44E(SemanticRule): def execute(self): self.__rule(self.node) + def __rule(self, node): node.num = 0 + node.code.clear() + node.names.clear() + + +class SelectionStatement45C1(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class SelectionStatement45C3(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class SelectionStatement45C5(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun # 45 class SelectionStatement45E(SemanticRule): def execute(self): self.__rule(self.node) + def __rule(self, node): + if not node.children[1].bool: + self.errors.append(SemanticError('if判断中的表达式不是bool表达式')) + else: + for c in node.children[1].code: + node.code.append(c) + if_block = get_temp_block_name() + else_block = get_temp_block_name() + next_block = get_temp_block_name() + + node.code.append(f"(if, {node.children[1].name}, goto, {if_block})") + + node.code.append(f"(block, _, _, {if_block})") + for c in node.children[3].code: + node.code.append(c) + node.code.append(f"goto, _, _, {next_block}") + + node.code.append(f"(block, _, _, {else_block})") + for c in node.children[5].code: + node.code.append(c) + node.code.append(f"(goto, _, _, {next_block})") + + node.code.append(f"(block, _, _, {next_block})") + +class SelectionFollow46C2(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]}") 这里应该是跳转的地址 , 可是跳转到哪呢 ? + +class SelectionFollow46E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + for c in node.children[2].code: + node.code.append(c) + +class SelectionFollow47E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.code.clear() + +class IterationStatement48C1(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class IterationStatement48C3(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class IterationStatement48E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + judge_block = get_temp_block_name() + iteration_block = get_temp_block_name() + next_block = get_temp_block_name() + node.code.append(f"(block, _, _, {judge_block})") for c in node.children[1].code: node.code.append(c) - # 这里加一个if跳转 + node.code.append(f"(if, {node.children[2].name}, goto, {iteration_block})") + node.code.append(f"(goto, _, _, {next_block})") + node.code.append(f"(block, _, _, {iteration_block})") for c in node.children[3].code: node.code.append(c) + node.code.append(f"(goto, _, _, {judge_block})") + node.code.append(f"(block, _, _, {next_block})") - for c in node.children[5].code: +class ReturnStatement49E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + for c in node.children[1].code: node.code.append(c) +class ReturnFollow50E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.code.append("(return, _, _, _)") +class ReturnFollow51E(SemanticRule): + def execute(self): + self.__rule(self.node) - - + def __rule(self, node): + for c in node.children[0].code: + node.code.append(c) + node.code.append(f"(return, _, _, {node.children[0].name})") @@ -1277,6 +1503,8 @@ class Exp52C0(SemanticRule): def __rule(self, node): self.fun = node.parent.fun + print("52C0") + print(self.fun) class Exp52C1(SemanticRule): def execute(self): @@ -1299,9 +1527,9 @@ class Exp52E(SemanticRule): for c in node.children[1].code: node.code.append(c) # (op, i1, i2, o1) - node.code.append("(" + node.children[1].op + " ,", + node.children[0].name + " ," + node.children[1].name + " ," + name_1 + ")") + node.code.append(f"({node.children[1].op}, {node.children[0].name}, {node.children[1].name}, {name_1})") # (=. i1, _, o1) - node.code.append("(=, " + name_1 + ", _," + node.name + ")") + node.code.append(f"(=, {name_1}, _, {node.name})") else: node.name = node.children[0].name for c in node.children[0].code: @@ -1385,6 +1613,8 @@ class AdditiveExpr61C0(SemanticRule): def __rule(self, node): node.fun = node.parent.fun + print("61C0") + print(node.parent.fun) class AdditiveExpr61C1(SemanticRule): def execute(self): @@ -1392,6 +1622,9 @@ class AdditiveExpr61C1(SemanticRule): def __rule(self, node): node.fun = node.parent.fun + print("61C1") + print(node.parent.fun) + class AdditiveExpr61E(SemanticRule): def execute(self): @@ -1408,9 +1641,9 @@ class AdditiveExpr61E(SemanticRule): node.code.append(c) # (op, in1, in2, o1) - node.code.append("(" + node.children[1].op + ", " + node.children[0].name + ", " + node.children[1].name + ", " + name_1 + ")") + node.code.append(f"({node.children[1].op}, {node.children[0].name}, {node.children[1].name}, {name_1})") # (=, in1, _, o1) - node.code.append("(=, " + name_1, + ", _, " + node.name + ")") + node.code.append(f"(=, {name_1}, _, {node.name})") else: node.name = node.children[0].name @@ -1483,6 +1716,8 @@ class Term66C0(SemanticRule): def __rule(self, node): node.fun = node.parent.fun + print("66C0") + print(node.fun) class Term66C1(SemanticRule): def execute(self): @@ -1506,9 +1741,9 @@ class Term66E(SemanticRule): node.code.append(c) # (op, in1, in2, o1) - node.code.append("(" + node.children[1].op + ", " + node.children[0].name + ", " + node.children[1].name + ", " + name_1 + ")") + node.code.append(f"({node.children[1].op}, {node.children[0].name}, {node.children[1].name}, {name_1})") # (=, in1, _, o1) - node.code.append("(=, " + name_1, + ", _, " + node.name + ")") + node.code.append(f"(=, {name_1}, _, {node.name})") else: node.name = node.children[0].name @@ -1597,6 +1832,8 @@ class Factor72C1(SemanticRule): def __rule(self, node): node.id = node.get_pre_brother(1).lexical node.fun = node.parent.fun + print("72C1") + print(node.fun) class Factor72E(SemanticRule): def execute(self): @@ -1612,14 +1849,16 @@ class Factor73E(SemanticRule): self.__rule(self.node) def __rule(self, node): - node.name = get_temp_var_name - node.code.append("(=, " + node.children[0].lexical, +", _, ", node.name) + node.name = get_temp_var_name() + node.code.append(f"(=, {node.children[0].lexical}, _, {node.name})") class IdFactorFollow74E(SemanticRule): def execute(self): self.__rule(self.node) def __rule(self, node): + print("74E") + print(node.fun) if symbol_table_pool.query(node.fun).exist(node.id): if node.children[0].type == "var": node.name = node.id @@ -1747,3 +1986,11 @@ class ArgListFollow80E(SemanticRule): node.num = 0 node.code.clear() node.names.clear() + +class Factor83E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.name = get_temp_var_name() + node.code.append("(=, " + node.children[0].lexical, +", _, ", node.name) diff --git a/syntax/rule.py b/syntax/rule.py index e21606e..2c88e6f 100644 --- a/syntax/rule.py +++ b/syntax/rule.py @@ -484,58 +484,6 @@ productions = [ Production('local-define-follow', ['semicolon'], None, [None], None, ), - # # 33 - # Production('normal-statement', ['semicolon'], - # None, [None], None, - # ), - # # 34 - # Production('normal-statement', ['id', 'normal-statement-follow'], - # None, [None, None], None, - # ), - # # 35 - # Production('normal-statement-follow', ['var-follow', 'equal', 'exp', 'semicolon'], - # None, [None, None, None, None], None, - # ), - # # 36 - # Production('normal-statement-follow', ['call-follow', 'semicolon'], - # None, [None], None, - # ), - # # 37 - # Production('var-follow', [], - # None, [], None, - # ), - # # 38 - # Production('var-follow', ['left-bracket', 'exp', 'right-bracket'], - # None, [None, None, None], None, - # ), - # # 39 - # Production('call-follow', ['left-parentheses', 'call-params', 'right-parentheses'], - # None, [None, None, None], None, - # ), - # # 40 - # Production('call-params', ['call-param-list'], - # None, [None], None, - # ), - # # 41 - # Production('call-params', [], - # None, [], None, - # ), - # # 42 - # Production('call-param-list', ['exp', 'call-param-follow'], - # None, [None, None], None, - # ), - # # 43 - # Production('call-param-follow', ['comma', 'exp', 'call-param-follow'], - # None, [None, None, None], None, - # ), - # # 44 - # Production('call-param-follow', [], - # None, [], None, - # ), - # # 45 - # 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, @@ -545,12 +493,12 @@ productions = [ None, [None, 'NormalStatement34C1'], 'NormalStatement34E', ), # 35 - Production('normal-statement-follow', ['var-follow', 'equal', 'exp', 'semicolon'], - None, ['NormalStatement35C0', None, 'NormalStatementFollow35C0', None], 'NormalStatement35E', + Production('normal-statement-follow', ['var-follow', 'evaluate', 'exp', 'semicolon'], + None, ['NormalStatementFollow35C0', None, 'NormalStatementFollow35C2', None], 'NormalStatement35E', ), # 36 Production('normal-statement-follow', ['call-follow', 'semicolon'], - None, ['NormalStatementFollow36C0',None], 'NormalStatementFollow36E', + None, ['NormalStatementFollow36C0', None], 'NormalStatementFollow36E', ), # 37 Production('var-follow', [], @@ -566,7 +514,7 @@ productions = [ ), # 40 Production('call-params', ['call-param-list'], - None, ['SemanticRule'], 'CallParams40E', + None, ['CallParams40C0'], 'CallParams40E', ), # 41 Production('call-params', [], @@ -584,34 +532,35 @@ productions = [ 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, + None, [None, 'SelectionStatement45C1', None, 'SelectionStatement45C3', None, 'SelectionStatement45C5'], 'SelectionStatement45E', + ), + # 46 + Production('selection-follow', ['else', 'left-brace', 'code-list', 'right-brace'], + None, [None, None, 'SelectionFollow46C2', None], 'SelectionFollow46E', + ), + # 47 + Production('selection-follow', [], + None, [], 'SelectionFollow47E', + ), + # 48 + Production('iteration-statement', ['while', 'exp', 'left-brace', 'code-list', 'right-brace'], + None, [None, "IterationStatement48C1", None, 'IterationStatement48C3', None], 'IterationStatement48E', + ), + # 49 + Production('return-statement', ['return', 'return-follow'], + None, [None, None], "ReturnStatement49E", + ), + # 50 + Production('return-follow', ['semicolon'], + None, [None], "ReturnFollow50E", + ), + # 51 + Production('return-follow', ['exp', 'semicolon'], + None, [None, None], 'ReturnFollow51E', ), - # # 46 - # Production('selection-follow', ['else', 'left-brace', 'code-list', 'right-brace'], - # None, [None, None, None, None], None, - # ), - # # 47 - # Production('selection-follow', [], - # None, [], None, - # ), - # # 48 - # Production('iteration-statement', ['while', 'exp', 'left-brace', 'code-list', 'right-brace'], - # None, [None, None, None, None, None], None, - # ), - # # 49 - # Production('return-statement', ['return', 'return-follow'], - # None, [None, None], None, - # ), - # # 50 - # Production('return-follow', ['semicolon'], - # None, [None], None, - # ), - # # 51 - # Production('return-follow', ['exp', 'semicolon'], - # None, [None, None], None, - # ), # 52 Production('exp', ['additive-expr', 'exp-follow'], None, ['Exp52C0', 'Exp52C1'], 'Exp52E'), @@ -676,7 +625,7 @@ productions = [ Production('factor', ['id', 'id-factor-follow'], None, [None, 'Factor72C1'], 'Factor72E'), # 73 - Production('factor', ['num'], + Production('factor', ['int-num'], None, [None], 'Factor73E'), # 74 Production('id-factor-follow', ['var-follow'], @@ -707,7 +656,11 @@ productions = [ # 82 Production('arraylist-with-num', ['left-bracket', 'int-num', 'right-bracket'], None, [None, None, None], 'ArrayListWithNum82E', - ) + ), + + # 83 + Production('factor', ['float-num'], + None, [None], 'Factor83E'), ] # 文法开始符号 diff --git a/syntax/syntax.py b/syntax/syntax.py index 2afe848..f5421ba 100644 --- a/syntax/syntax.py +++ b/syntax/syntax.py @@ -430,7 +430,7 @@ class Syntax: if production: # 判断语义规则是否合法 if len(production.right) != len(production.semantic_children): - self.__error = SemanticRuleError('语义规则数量与产生式右边数量不一致 ' + production.str) + self.__error = SemanticRuleError('语义规则数量与产生式右边数量不一致 ' + str(production.str[0])) break # 执行 start 语义 diff --git a/test9.c b/test9.c index 4a06bc4..eb42250 100644 --- a/test9.c +++ b/test9.c @@ -1,7 +1,9 @@ -fn main(a:i32[], b:i32, c:f32[], d:i32) -> f32 { - i32 test; -} - -fn demo() { - i32 fs; +fn main() { + i32 a; + a = 42; + + if a { + } + } +