def 表达式(self): sub_tree = TreeNode('<表达式>') child_tree = None heading_operator = None syntax_tree = None if isinstance(self.token, Operator) and self.token.value in ['+', '-']: # TODO child_tree = TreeNode(self.token) heading_operator = self.token.value self.token = self.lexer.forward() anal_term, ast_term = self.项() if child_tree is not None: child_tree.sublings.append(anal_term) else: child_tree = anal_term if heading_operator is None or heading_operator in ['+']: syntax_tree = ast_term else: ast_heading_zero = TreeNode(Int(0)) syntax_tree = SyntaxTree.BinaryExpression(heading_operator, ast_heading_zero, ast_term) while isinstance(self.token, Operator) and self.token.value in ['+', '-']: anal_add, ast_add = self.加法运算符() child_tree.sublings.append(anal_add) anal_term, rhs_term = self.项() syntax_tree = SyntaxTree.BinaryExpression(ast_add, syntax_tree, rhs_term) child_tree.sublings.append(anal_term) else: sub_tree.child = child_tree return sub_tree, syntax_tree
def __init__(self, const_decl=None, var_decl=None, proc_decl=None, stmt=None): TreeNode.__init__(self, 'SubRoutine') self.dict = { 'ConstDeclaration': const_decl, 'VariableDeclaration': var_decl, 'ProcDeclaration': proc_decl, 'Statement': stmt } if const_decl is not None: self.child = const_decl if var_decl is not None: if self.child is not None: self.child.sublings.append(var_decl) else: self.child = var_decl if proc_decl is not None: if self.child is not None: self.child.sublings.append(proc_decl) else: self.child = proc_decl if stmt is not None: if self.child is not None: self.child.sublings.append(stmt) else: self.child = stmt
def 无符号整数(self): sub_tree = TreeNode('<无符号整数>') if isinstance(self.token, Int): sub_tree.child = TreeNode(self.token) syntax_tree = TreeNode(self.token) self.token = self.lexer.forward() return sub_tree, syntax_tree
def 分程序(self): sub_tree = TreeNode('<分程序>') child_tree, ast_const_decl, ast_var_decl, ast_proc_decl, ast_smnt_tree = None, None, None, None, None if isinstance(self.token, Keyword) and self.token.value == 'const': child_tree, ast_const_decl = self.常量说明部分() if isinstance(self.token, Keyword) and self.token.value == 'var': anal_var_decl, ast_var_decl = self.变量说明部分() if child_tree is None: child_tree = anal_var_decl else: child_tree.sublings.append(anal_var_decl) if isinstance(self.token, Keyword) and self.token.value == 'procedure': anal_proc_decl, ast_proc_decl = self.过程说明部分() if child_tree is None: child_tree = anal_proc_decl else: child_tree.sublings.append(anal_proc_decl) anal_smnt_tree, ast_smnt_tree = self.语句() if child_tree is None: child_tree = anal_smnt_tree else: child_tree.sublings.append(anal_smnt_tree) sub_tree.child = child_tree ast_tree = SyntaxTree.SubRoutine(const_decl=ast_const_decl, var_decl=ast_var_decl, proc_decl=ast_proc_decl, stmt=ast_smnt_tree) return sub_tree, ast_tree
def 标识符(self): sub_tree = TreeNode('<标识符>') if isinstance(self.token, Identifier): sub_tree.child = TreeNode(self.token) syntax_tree = TreeNode(self.token) self.token = self.lexer.forward() return sub_tree, syntax_tree self.unexpected_error('identifier')
def __init__(self, identifier, number): TreeNode.__init__(self, 'ConstDeclarator') self.dict = { 'Identifier': identifier, 'Number': number } self.child = copy.deepcopy(identifier) self.child.sublings.append(number)
def __init__(self, *args): TreeNode.__init__(self, 'ProcDeclaration') self.dict = { 'Declarations': args } self.child = args[0] if len(args) > 1: self.child.sublings.extend(args[1:])
def __init__(self, condition=None, statement=None): TreeNode.__init__(self, 'DoWhileStatement') self.dict = { 'Condition': condition, 'Statement': statement } self.child = copy.deepcopy(condition) self.child.sublings.extend(statement)
def __init__(self, oprator, lhs, rhs): TreeNode.__init__(self, oprator) self.dict = { 'Lhs': lhs, 'Rhs': rhs } self.child = copy.deepcopy(lhs) self.child.sublings.append(rhs)
def __init__(self, io_type, arguments: Arguments): TreeNode.__init__(self, 'IOStatement') self.dict = { 'IOType': io_type, 'Arguments': arguments } self.child = io_type self.child.sublings.append(arguments)
def __init__(self, condition=None, statement=None): TreeNode.__init__(self, 'WhileStatement') self.dict = { 'Condition': condition, 'Statement': statement } self.child = condition self.child.sublings.append(statement)
def __init__(self, lhs, rhs): TreeNode.__init__(self, 'AssignExpression') self.dict = { 'Lhs': lhs, 'Rhs': rhs } self.child = copy.deepcopy(lhs) self.child.sublings.append(rhs)
def __init__(self, *stmt_list): TreeNode.__init__(self, 'BlockStatement') self.dict = { 'StatementList': stmt_list } self.child = stmt_list[0] if len(stmt_list) > 1: self.child.sublings.extend(stmt_list[1:])
def __init__(self, *arg_list): TreeNode.__init__(self, 'Arguments') self.dict = { 'Arguments': arg_list } self.child = arg_list[0] if len(arg_list) > 1: self.child.sublings.extend(arg_list[1:])
def __init__(self, ast_proc_head, ast_subroutine): TreeNode.__init__(self, 'ProcDeclarator') self.dict = { 'ProcHead': ast_proc_head, 'SubRoutine': ast_subroutine } self.child = ast_proc_head self.child.sublings.append(ast_subroutine)
def __init__(self, *args): #super(TreeNode, self).__init__('VariableDeclaration') TreeNode.__init__(self, 'VariableDeclaration') self.dict = { 'Declarations': args } self.child = args[0] if len(args) > 1: self.child.sublings.extend(args[1:])
def __init__(self, identifier, *arguments): TreeNode.__init__(self, 'CallExpression') self.dict = { 'Identifier': identifier, 'Arguments': arguments, } self.child = identifier if arguments is not None and len(arguments) > 0: self.child.sublings.append(arguments)
def 关系运算符(self): sub_tree = TreeNode('<关系运算符>') if isinstance(self.token, Operator): if self.token.value in ['=', '<>', '<', '<=', '>', '>=']: child_tree = TreeNode(self.token) self.token = self.lexer.forward() sub_tree.child = child_tree return sub_tree, child_tree.data.value self.unexpected_error('compare operator')
def 读语句(self): sub_tree = TreeNode('<读语句>') if isinstance(self.token, Keyword) and self.token.value == 'read': io_type = TreeNode(self.token) child_tree = TreeNode(self.token) sub_tree.child = child_tree self.token = self.lexer.forward() ast_args = self.rw_arg(child_tree) syntax_tree = SyntaxTree.IOStatement(io_type, ast_args) return sub_tree, syntax_tree
def __init__(self, condition, true_stmt, false_stmt=EmptyStatement()): TreeNode.__init__(self, 'IfStatement') self.dict = { 'Condition': condition, # condition is a binary operator 'TrueStatement': true_stmt, 'FalseStatement': false_stmt } self.child = copy.deepcopy(condition) self.child.sublings.append(true_stmt) self.child.sublings.append(false_stmt)
def 程序(self): sub_tree = TreeNode('<程序>') child_tree, syntax_tree = self.分程序() if isinstance(self.token, Delimiter) and self.token.value == '.': child_tree.sublings.append(TreeNode(self.token)) self.token = self.lexer.forward() sub_tree.child = child_tree return sub_tree, syntax_tree else: self.unexpected_error('.')
def 写语句(self): sub_tree = TreeNode('<写语句>') if isinstance(self.token, Keyword) and self.token.value == 'write': io_type = TreeNode(self.token) child_tree = TreeNode(self.token) sub_tree.child = child_tree self.token = self.lexer.forward() ast_args = self.rw_arg(child_tree) syntax_tree = SyntaxTree.IOStatement(io_type, ast_args) return sub_tree, syntax_tree else: self.unexpected_error('write')
def 项(self): sub_tree = TreeNode('<项>') child_tree, ast_tree = self.因子() while isinstance(self.token, Operator) and self.token.value in ['*', '/']: anal_mul, ast_mul = self.乘法运算符() child_tree.sublings.append(anal_mul) fractor, ast_fractor = self.因子() ast_tree = SyntaxTree.BinaryExpression(ast_mul, ast_tree, ast_fractor) child_tree.sublings.append(fractor) else: sub_tree.child = child_tree return sub_tree, ast_tree
def 乘法运算符(self): sub_tree = TreeNode('<乘法运算符>') if isinstance(self.token, Operator): if self.token.value == '*' or self.token.value == '/': child_tree = TreeNode(self.token) self.token = self.lexer.forward() sub_tree.child = child_tree return sub_tree, child_tree.data.value else: self.unexpected_error('* or /') else: self.unexpected_error('operator')
def 常量定义(self): analysis_tree = TreeNode('<常量定义>') child_tree, syntax_indentifier = self.标识符() if isinstance(self.token, Operator) and self.token.value == '=': child_tree.sublings.append(TreeNode(self.token)) self.token = self.lexer.forward() subling, syntax_int = self.无符号整数() child_tree.sublings.append(subling) analysis_tree.child = child_tree syntax_tree = SyntaxTree.ConstDeclarator(syntax_indentifier, syntax_int) return analysis_tree, syntax_tree else: self.unexpected_error('const')
def 赋值语句(self): sub_tree = TreeNode('<赋值语句>') child_tree, ast_identifier_lhs = self.标识符() if isinstance(self.token, Operator) and self.token.value == ':=': child_tree.sublings.append(TreeNode(self.token)) self.token = self.lexer.forward() expression, ast_identifier_rhs = self.表达式() child_tree.sublings.append(expression) sub_tree.child = child_tree syntax_tree = SyntaxTree.AssignExpression(ast_identifier_lhs, ast_identifier_rhs) return sub_tree, syntax_tree else: self.unexpected_error(':=')
def rw_arg(self, child_tree): expected = '(' if isinstance(self.token, Delimiter) and self.token.value == '(': child_tree.sublings.append(TreeNode(self.token)) self.token = self.lexer.forward() args_list = self.narg(child_tree, self.标识符) expected = ')' if isinstance(self.token, Delimiter) and self.token.value == ')': child_tree.sublings.append(TreeNode(self.token)) self.token = self.lexer.forward() syntax_tree = SyntaxTree.Arguments(*args_list) return syntax_tree self.unexpected_error(expected)
def 过程调用语句(self): sub_tree = TreeNode('<过程调用语句>') expected = 'call' if isinstance(self.token, Keyword) and self.token.value == 'call': child_tree = TreeNode(self.token) self.token = self.lexer.forward() identifier, ast_identifier = self.标识符() expected = 'identifier' if identifier is not None: child_tree.sublings.append(identifier) sub_tree.child = child_tree syntax_tree = SyntaxTree.CallExpression(identifier=ast_identifier) return sub_tree, syntax_tree self.unexpected_error(expected)
def 当型循环语句(self): sub_tree = TreeNode('<当型循环语句>') expected = 'while' if isinstance(self.token, Keyword) and self.token.value == 'while': child_tree = TreeNode(self.token) self.token = self.lexer.forward() condition, ast_condition = self.条件() child_tree.sublings.append(condition) expected = 'do' if isinstance(self.token, Keyword) and self.token.value == 'do': child_tree.sublings.append(TreeNode(self.token)) self.token = self.lexer.forward() statement, ast_stmt = self.语句() child_tree.sublings.append(statement) sub_tree.child = child_tree syntax_tree = SyntaxTree.WhileStatement(condition=ast_condition, statement=ast_stmt) return sub_tree, syntax_tree self.unexpected_error(expected)
def 条件(self): sub_tree = TreeNode('<条件>') syntax_tree = None if isinstance(self.token, Keyword) and self.token.value == 'odd': child_tree = TreeNode(self.token) self.token = self.lexer.forward() expression, ast_expression = self.表达式() syntax_tree = SyntaxTree.OddStatement(ast_expression) else: expression, ast_lhs = self.表达式() child_tree = expression operator, ast_operator = self.关系运算符() child_tree.sublings.append(operator) expression, ast_rhs = self.表达式() syntax_tree = SyntaxTree.BinaryExpression(ast_operator, ast_lhs, ast_rhs) child_tree.sublings.append(expression) sub_tree.child = child_tree return sub_tree, syntax_tree