diff --git a/main.py b/main.py index 8279f68..39bc682 100644 --- a/main.py +++ b/main.py @@ -28,17 +28,15 @@ if lexical_success: syntax.put_source(lexical_result) syntax_success = syntax.execute() print('语法分析和语义分析是否成功\t', syntax_success) - if not syntax_success: + if syntax_success: + print() + print('语义分析结果:\t') + print('三地址代码:\t') + i = -1 + for code in syntax.get_result().root.code: + i += 1 + print(i, ' \t', code) + else: print('错误原因:\t', syntax.get_error().info) -# if syntax_success: -# print() -# print('语义分析结果:\t') -# print('三地址代码:\t') -# i = -1 -# for code in syntax.get_result().root.code: -# i += 1 -# print(i, ' \t', code) -# else: -# print('错误原因:\t', syntax.get_error().info, syntax.get_error().line, '行') -# else: -# print('错误原因:\t', lexical.get_error().info) +else: + print('错误原因:\t', lexical.get_error().info) diff --git a/semantic/rule.py b/semantic/rule.py index 765be65..b42ef42 100644 --- a/semantic/rule.py +++ b/semantic/rule.py @@ -231,6 +231,74 @@ class SemanticRuleFactory: return GlobalVarDefineFllow20E(node) if rule_key == 'GlobalVarDefineFllow20C2': return GlobalVarDefineFllow20C2(node) + + # 22 + if rule_key == 'CodeBlock22C0': + return CodeBlock22C0(node) + if rule_key == 'CodeBlock22E': + return CodeBlock22E(node) + + # 23 + if rule_key == 'CodeList23C0': + return CodeList23C0(node) + if rule_key == 'CodeList23C1': + return CodeList23C1(node) + if rule_key == 'CodeList23E': + return CodeList23E(node) + + # 24 + if rule_key == 'CodeList24E': + return CodeList24E(node) + + # 25 + if rule_key == 'Code25C0': + return Code25C0(node) + if rule_key == 'Code25E': + return Code25E(node) + + # 26 + if rule_key == 'Code26C0': + return Code26C0(node) + if rule_key == 'Code26E': + return Code26E(node) + + # 27 + if rule_key == 'Code27C0': + return Code27C0(node) + if rule_key == 'Code27E': + return Code27E(node) + + # 28 + if rule_key == 'Code28C0': + return Code28C0(node) + if rule_key == 'Code28C0': + return Code28C0(node) + + # 29 + if rule_key == 'Code29C0': + return Code29C0(node) + if rule_key == 'Code29C0': + return Code29C0(node) + + # 30 + if rule_key == 'LocalDefine30C0': + return LocalDefine30C0(node) + if rule_key == 'LocalDefine30C1': + return LocalDefine30C1(node) + if rule_key == 'LocalDefine30C2': + return LocalDefine30C2(node) + if rule_key == 'LocalDefine30C3': + return LocalDefine30C3(node) + if rule_key == 'LocalDefine30E': + return LocalDefine30E(node) + + # 31 + if rule_key == 'LocalDefineFollow31C1': + return LocalDefineFollow31C1(node) + if rule_key == 'LocalDefineFollow31C2': + return LocalDefineFollow31C2(node) + if rule_key == 'LocalDefineFollow31E': + return LocalDefineFollow31E(node) # 81 if rule_key == 'ArrayListWithNum81E': @@ -326,7 +394,7 @@ class FunDefine5C7(SemanticRule): self.__rule(self.node) def __rule(self, node): - node.fun = node.parent.id + node.fun = node.get_pre_brother(6).lexical class FunDefine5E(SemanticRule): def execute(self): @@ -614,7 +682,7 @@ class GlobalVarDefineFllow20E(SemanticRule): GlobalVar(node.id, node.type, 4 * int(node.length)) ) -class CodeList22C0(SemanticRule): +class CodeBlock22C0(SemanticRule): def execute(self): self.__rule(self.node) @@ -622,7 +690,7 @@ class CodeList22C0(SemanticRule): node.fun = node.parent.fun -class CodeList22E(SemanticRule): +class CodeBlock22E(SemanticRule): def execute(self): self.__rule(self.node) @@ -663,7 +731,7 @@ class CodeList24E(SemanticRule): node.code.clear() -class CodeList25C0(SemanticRule): +class Code25C0(SemanticRule): def execute(self): self.__rule(self.node) @@ -678,7 +746,7 @@ class Code25E(SemanticRule): for c in node.children[0].code: node.code.append(c) -class CodeList26C0(SemanticRule): +class Code26C0(SemanticRule): def execute(self): self.__rule(self.node) @@ -693,7 +761,7 @@ class Code26E(SemanticRule): for c in node.children[0].code: node.code.append(c) -class CodeList27C0(SemanticRule): +class Code27C0(SemanticRule): def execute(self): self.__rule(self.node) @@ -708,7 +776,7 @@ class Code27E(SemanticRule): for c in node.children[0].code: node.code.append(c) -class CodeList28C0(SemanticRule): +class Code28C0(SemanticRule): def execute(self): self.__rule(self.node) @@ -723,7 +791,7 @@ class Code28E(SemanticRule): for c in node.children[0].code: node.code.append(c) -class CodeList29C0(SemanticRule): +class Code29C0(SemanticRule): def execute(self): self.__rule(self.node) @@ -737,9 +805,131 @@ class Code29E(SemanticRule): def __rule(self, node): for c in node.children[0].code: node.code.append(c) - - - + +class LocalDefine30C0(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class LocalDefine30C1(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class LocalDefine30C2(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class LocalDefine30C3(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.type = None + node.length = 0 + node.array = None + if node.get_pre_brother(3).type == 'void' and node.get_pre_brother(2).type =='var': + self.errors.append(SemanticError('变量' + node.id + '不能定义为void类型')) + if node.get_pre_brother(3).type == 'void' and node.get_pre_brother(2).type =='array': + self.errors.append(SemanticError('变量' + node.id + '不能定义为void[]类型')) + if node.get_pre_brother(3).type == 'i32': + if node.get_pre_brother(2).type == 'array': + node.type = 'i32[]' + node.length = node.get_pre_brother(2).length + node.array = True + if node.get_pre_brother(2).type == 'var': + node.type = 'i32' + node.array = False + if node.get_pre_brother(3).type == 'f32': + if node.get_pre_brother(2).type == 'array': + node.type = 'f32[]' + node.array = True + node.length = node.get_pre_brother(2).length + if node.get_pre_brother(2).type == 'var': + node.type = 'f32' + node.array = False + + if node.type == None: + self.errors.append(SemanticError('变量' + node.get_pre_brother(1).lexical + '声明类型不支持')) +class LocalDefine30E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.id = node.children[2].lexical + if symbol_table_pool.query(node.fun).exist(node.id): + self.errors.append(SemanticError('变量' + node.id + '重定义')) + + node.type = None + node.length = 0 + if node.children[0].type == 'void' and node.children[1].type =='var': + self.errors.append(SemanticError('变量' + node.id + '不能定义为void类型')) + if node.children[0].type == 'void' and node.children[1].type =='array': + self.errors.append(SemanticError('变量' + node.id + '不能定义为void[]类型')) + if node.children[0].type == 'i32': + if node.children[1].type == 'array': + node.type = 'i32[]' + node.length = int(node.children[1].length) + symbol_table_pool.query(node.fun).append( + LocalVar(node.id, 'i32[]', 4 * node.length, False) + ) + if node.children[1].type == 'var': + node.type = 'i32' + symbol_table_pool.query(node.fun).append( + LocalVar(node.id, 'i32', 4, False) + ) + if node.children[0].type == 'f32': + if node.children[1].type == 'array': + node.type = 'f32[]' + node.length = int(node.children[1].length) + symbol_table_pool.query(node.fun).append( + LocalVar(node.id, 'f32[]', 4 * node.length, False) + ) + if node.children[1].type == 'var': + node.type = 'f32' + symbol_table_pool.query(node.fun).append( + LocalVar(node.id, 'f32', 4, False) + ) + if node.type == None: + self.errors.append(SemanticError('变量' + node.get_pre_brother(1).lexical + '声明类型不支持')) + + +class LocalDefineFollow31C1(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + +class LocalDefineFollow31C2(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.fun = node.parent.fun + node.type = node.parent.type + node.length = node.parent.length + +class LocalDefineFollow31E(SemanticRule): + def execute(self): + self.__rule(self.node) + + def __rule(self, node): + node.id = node.children[1].lexical + if symbol_table_pool.global_var_table.exist(node.id): + self.errors.append(SemanticError('变量' + node.id + '重定义')) + + symbol_table_pool.global_var_table.append( + GlobalVar(node.id, node.type, 4 * int(node.length)) + ) + diff --git a/syntax/rule.py b/syntax/rule.py index 4ccc29e..87220af 100644 --- a/syntax/rule.py +++ b/syntax/rule.py @@ -367,7 +367,7 @@ productions = [ Production('fun-define', ['fn', 'id', 'left-parentheses', 'params', 'right-parentheses', 'fun-return', 'left-brace', 'code-block', 'right-brace'], - None, [None, None, None, 'FunDefine5C3', None, 'FunDefine5C5', None, None, 'FunDefine5C7'], 'FunDefine5E', + None, [None, None, None, 'FunDefine5C3', None, 'FunDefine5C5', None, 'FunDefine5C7', None], 'FunDefine5E', ), # 6 Production('params', [], @@ -455,28 +455,28 @@ productions = [ ), # 27 Production('code', ['selection-statement'], - None, ['Code27C0'], 'Code26E', + None, ['Code27C0'], 'Code27E', ), # 28 Production('code', ['iteration-statement'], - None, ['Code28C0'], 'Code25E', + None, ['Code28C0'], 'Code28E', ), # 29 Production('code', ['return-statement'], - None, ['Code29C0'], 'Code25E', + None, ['Code29C0'], 'Code29E', + ), + # 30 + Production('local-define', ['type', 'arraylist-with-num', 'id', 'local-define-follow'], + None, ['LocalDefine30C0', "LocalDefine30C1", 'LocalDefine30C2', 'LocalDefine30C3'], 'LocalDefine30E', + ), + # 31 + Production('local-define-follow', ['comma', 'id', 'local-define-follow'], + None, [None, 'LocalDefineFollow31C1', 'LocalDefineFollow31C2'], 'LocalDefineFollow31E', + ), + # 32 + Production('local-define-follow', ['semicolon'], + None, [None], None, ), - # # 30 - # Production('local-define', ['type', 'arraylist', 'id', 'local-define-follow'], - # None, [None, None, None, None], None, - # ), - # # 31 - # Production('local-define-follow', ['comma', 'id', 'local-define-follow'], - # None, [None, None, None], None, - # ), - # # 32 - # Production('local-define-follow', ['semicolon'], - # None, [None], None, - # ), # # 33 # Production('normal-statement', ['semicolon'], # None, [None], None, @@ -553,9 +553,9 @@ productions = [ # Production('return-follow', ['exp', 'semicolon'], # None, [None, None], None, # ), - # # 52 - # Production('exp', ['additive-expr', 'exp-follow'], - # None, ['exp0C0', 'exp0C1'], 'exp0E'), + # 52 + Production('exp', ['additive-expr', 'exp-follow'], + None, ['exp0C0', 'exp0C1'], 'exp0E'), # # 53 # Production('exp-follow', ['rel-op', 'additive-expr'], # None, [None, 'expFollow0C1'], 'expFollow0E'), diff --git a/test9.c b/test9.c index 7c7e147..1b36325 100644 --- a/test9.c +++ b/test9.c @@ -1,3 +1,7 @@ fn main(a:i32[], b:i32, c:f32[], d:i32) -> f32 { - + i32 d; +} + +fn demo() { + i32 fs; }