def parse_expr(self): """ Expression ::= primary-Expression (Operator primary-Expression)* """ token = self.token_current() token_lookahead = self.token_lookahead() if token_lookahead.type == scanner.TK_LPAREN: """ Identifier '(' Param ')' """ ident = token.val # save ident name self.token_accept(scanner.TK_IDENTIFIER) # scan the token self.token_accept(scanner.TK_LPAREN) # accept left paren expr = self.parse_param_expr() self.token_accept(scanner.TK_RPAREN) e1 = ast.CallCommand(ident, expr) else: e1 = self.parse_secexpr() token = self.token_current() while token.type == scanner.TK_OPERATOR and token.val in [ '+', '-', '<', '>', '=' ]: oper = token.val self.token_accept_any() e2 = self.parse_secexpr() e1 = ast.BinaryExpression(e1, oper, e2) token = self.token_current() return e1
def parse_add_expression(self): e1 = self.parse_mul_expression() token = self.token_current() while token.type == scanner.TK_OPERATOR and token.val in ['+', '-']: oper = token.val self.token_accept_any() e2 = self.parse_mul_expression() token = self.token_current() e1 = ast.BinaryExpression(e1, oper, e2) return e1
def p_expression_math(self, p): """ expression : expression PLUS expression | expression MINUS expression | expression TIMES expression | expression DIVIDE expression | expression MPLUS expression | expression MMINUS expression | expression MTIMES expression | expression MDIVIDE expression """ p[0] = ast.BinaryExpression(p[1], p[2], p[3], p.lexer.lexer.lineno)
def parse_tertiary_expression(self): """ Sec-Expression ( Oper-Sec Sec-Expression )* """ e1 = self.parse_secondary_expression() token = self.token_current() while token.type == scanner.TK_OPERATOR and token.val in ['+', '-']: oper = token.val self.token_accept_any() e2 = self.parse_secondary_expression() token = self.token_current() e1 = ast.BinaryExpression(e1, oper, e2) return e1
def parse_expr(self): """ Expression ::= primary-Expression (operator primary-Expression)* """ e1 = self.parse_pri_expr() token = self.token_current() while (token.type == scanner.TK_OPERATOR): op = token.val self.token_accept(scanner.TK_OPERATOR) e2 = self.parse_pri_expr() token = self.token_current() e1 = ast.BinaryExpression(e1, op, e2) return e1
def parse_secexpr(self): """ SecExpr :== PriExpr (OPER_PRI PriExpr)*""" e1 = self.parse_priexpr() token = self.token_current() while token.type == scanner.TK_OPERATOR and token.val in [ '*', '/', '\\' ]: oper = token.val self.token_accept_any() e2 = self.parse_priexpr() token = self.token_current() e1 = ast.BinaryExpression(e1, oper, e2) return e1
def parse_expression(self): """ Expression ::= primary-Expression (Operator primary-Expression)* """ e1 = self.parse_add_expression() token = self.token_current() while token.type == scanner.TK_OPERATOR and token.val in [ '<', '=', '>' ]: oper = token.val self.token_accept_any() e2 = self.parse_add_expression() token = self.token_current() e1 = ast.BinaryExpression(e1, oper, e2) return e1
def parse_secondary_expression(self): """ Pri-Expression ( Oper-Pri Pri-Expression )* """ e1 = self.parse_primary_expression() token = self.token_current() while token.type == scanner.TK_OPERATOR and token.val in [ '*', '/', '' ]: oper = token.val self.token_accept_any() e2 = self.parse_primary_expression() token = self.token_current() e1 = ast.BinaryExpression(e1, oper, e2) return e1
def p_relation(self, p): """relation : expression comparison_operator expression""" p[0] = ast.BinaryExpression(p[1], p[2], p[3], p.lexer.lexer.lineno)