Exemple #1
0
    def relat(self):
        '''
        Relation --> Addition [ RelOp Addition ]
        :return: ast.relation
        '''
        left_tree = self.addition()

        if self.curr_tok[0] in Parser.RelOp_first:
            if self.curr_tok[0] == lexer.Lexer.GT:
                self.curr_tok = next(self.lex)
                right_tree = self.addition()
                left_tree = ast.OpExpr(left_tree, right_tree, ">")
            elif self.curr_tok[0] == lexer.Lexer.GTEQ:
                self.curr_tok = next(self.lex)
                right_tree = self.addition()
                left_tree = ast.OpExpr(left_tree, right_tree, ">=")
            elif self.curr_tok[0] == lexer.Lexer.LT:
                self.curr_tok = next(self.lex)
                right_tree = self.addition()
                left_tree = ast.OpExpr(left_tree, right_tree, "<")
            else:
                self.curr_tok = next(self.lex)
                right_tree = self.addition()
                left_tree = ast.OpExpr(left_tree, right_tree, "<=")
        return left_tree
Exemple #2
0
 def ExpOp(self):
     left = self.fact()
     if self.curr_tok[0] == lexer.Lexer.POWER:
         self.curr_tok = next(self.lex)
         right = self.ExpOp()
         left = ast.OpExpr(left, right, '**')
     return left
Exemple #3
0
    def addition(self):
        '''
        Addition --> Term { AddOp Term }
        :return: ast.add
        '''
        left_tree = self.term()

        while self.curr_tok[0] in Parser.addOp_first:  # while we have an AddOp
            if self.curr_tok[0] == lexer.Lexer.PLUS:
                self.curr_tok = next(self.lex)
                right_tree = self.term()
                left_tree = ast.OpExpr(left_tree, right_tree, "+")
            else:
                self.curr_tok = next(self.lex)
                right_tree = self.term()
                left_tree = ast.OpExpr(left_tree, right_tree, "-")
        return left_tree
Exemple #4
0
    def equality(self):
        '''
        Equality --> Relation [ EquOp Addition ] 
        :return: ast.equality
        '''
        left_tree = self.relat()

        if self.curr_tok[0] in Parser.EquOp_first:
            if self.curr_tok[0] == lexer.Lexer.EQ_EQ:
                self.curr_tok = next(self.lex)
                right_tree = self.relat()
                left_tree = ast.OpExpr(left_tree, right_tree, "==")
            else:
                self.curr_tok[0] = next(self.lex)
                right_tree = self.relat()
                left_tree = ast.OpExpr(left_tree, right_tree, "!=")
        return left_tree
Exemple #5
0
 def _expr(self):
     node = self._expr_component()
     if self.cur_token.type in self.operators:
         op = self.cur_token.type
         self._get_next_token()
         rhs = self._expr_component()
         return ast.OpExpr(op, node, rhs)
     else:
         return node
Exemple #6
0
    def conj(self):
        '''
        Conjunction --> Equality { && Equality }
        :return: ast.conj
        '''
        left_tree = self.equality()

        while self.curr_tok[0] == lexer.Lexer.AND:
            self.curr_tok = next(self.lex)
            right_tree = self.equality()
            left_tree = ast.OpExpr(left_tree, right_tree, "&&")
        return left_tree
Exemple #7
0
    def expr(self):
        '''
        Expression --> Conjunction { || Conjunction }
        :return: ast.expr
        '''
        left_tree = self.conj()

        while self.curr_tok[0] == lexer.Lexer.OR:  # While we have ||
            self.curr_tok = next(self.lex)
            right_tree = self.conj()
            left_tree = ast.OpExpr(left_tree, right_tree, "||")
        return left_tree
Exemple #8
0
    def term(self):
        '''
        Term --> ExpOp { MulOp ExprOp }
        :return: ast.Expr
        '''
        left_tree = self.ExpOp()
        while self.curr_tok[0] in Parser.mulOp_first:  # while we have an MulOp
            if self.curr_tok[0] == lexer.Lexer.TIMES:
                self.curr_tok = next(self.lex)
                right_tree = self.ExpOp()
                left_tree = ast.OpExpr(left_tree, right_tree, "*")
            elif self.curr_tok[0] == lexer.Lexer.DIV:
                self.curr_tok = next(self.lex)
                right_tree = self.ExpOp()
                left_tree = ast.OpExpr(left_tree, right_tree, "/")
            else:
                self.curr_tok = next(self.lex)
                right_tree = self.ExpOp()
                left_tree = ast.OpExpr(left_tree, right_tree, "%")

        # what is true if we get here
        return left_tree
Exemple #9
0
    def _expr(self):
        """Parse an expr of the form:

                expr op expr

           We only allow a single operator between expressions. Additional
           operators should be nested using parens, e.g. x + (y * z)
        """
        node = self._expr_component()
        if self.cur_token.type in self.operators:
            op = self.cur_token.type
            self._get_next_token()
            rhs = self._expr_component()
            return ast.OpExpr(op, node, rhs)
        else:
            return node