def assign_stmt(self): node = Node(self.values[self.iterator]) #node.set_children(Node(self.values[self.iterator])) self.match('identifier') self.match(':=') node.set_children(self.exp()) return node
def exp(self): left_node=self.simple_exp() if self.token=='<' or self.token=='>' or self.token=='=': op_node=Node(self.values[self.iterator]) self.comparison_op() op_node.set_children(left_node) op_node.set_children(self.simple_exp()) left_node = op_node return left_node
def term(self): right_node = self.factor() while self.token=='*' or self.token=='/': op_node = Node(self.values[self.iterator]) self.mulop() op_node.set_children(right_node) right_node = op_node right_node.set_children(self.factor()) return right_node
def simple_exp(self): left_node=self.term() while self.token=='+' or self.token=='-': op_node = Node(self.values[self.iterator]) self.addop() op_node.set_children(left_node) op_node.set_children(self.term()) left_node = op_node return left_node
def term(self): right_node = self.factor() while self.token == '*' or self.token == '/': op_node = Node(self.values[self.iterator]) self.match(self.token, nonTerminal=True) op_node.set_children(right_node) right_node = op_node right_node.set_children(self.factor()) return right_node
def simple_exp(self): left_node = self.term() while self.token == '+' or self.token == '-': op_node = Node(self.values[self.iterator]) self.match(self.token, nonTerminal=True) op_node.set_children(left_node) op_node.set_children(self.term()) left_node = op_node return left_node
def repeat_stmt(self): repeat_node=Node(self.values[self.iterator]) if self.token=='repeat': self.match('repeat') repeat_node.set_children(self.stmt_sequence()) self.match('until') #until_node = Node('until') #until_node.set_children(self.exp()) repeat_node.set_children(self.exp()) return repeat_node
def stmt_sequence(self): start_node = Node(' ') start_node.set_children(self.statement()) end_if = self.types[self.iterator-1] == 'end' while self.token==';' or end_if: if not end_if: if not self.match(';'): break next_node=self.statement() if next_node == None: break else: start_node.set_children(next_node) end_if = self.types[self.iterator-1] == 'end' return start_node
def if_stmt(self): if_node=Node(self.values[self.iterator]) if self.token=='if': self.match('if') exp_node = Node(' ') exp_node.set_children(self.exp()) if_node.set_children(exp_node) self.match('then') then_node = Node('then') then_node.set_children(self.stmt_sequence()) if_node.set_children(then_node) if self.token=='else': self.match('else') else_node = Node('else') else_node.set_children(self.stmt_sequence()) if_node.set_children(else_node) self.match('end') return if_node
def write_stmt(self): node=Node(self.values[self.iterator]) self.match('write') node.set_children(self.exp()) return node
def read_stmt(self): node=Node(self.values[self.iterator]) self.match('read') node.set_children(Node(self.values[self.iterator])) self.match('identifier') return node