def p_expr(p): '''expr : expr ADD expr | expr SUB expr | expr MUL expr | expr DIV expr''' global MOD_G if p[2] == '+': p[0] = add_mod(true_modulo(p[1], MOD_G), true_modulo(p[3], MOD_G)) write_list('+') elif p[2] == '-': p[0] = sub_mod(true_modulo(p[1], MOD_G), true_modulo(p[3], MOD_G)) write_list('-') elif p[2] == '*': p[0] = mul_mod(true_modulo(p[1], MOD_G), true_modulo(p[3], MOD_G)) write_list('*') elif p[2] == '/': p[0] = div_mod(true_modulo(p[1], MOD_G), true_modulo(p[3], MOD_G)) write_list('/')
def p_powl_VAL(p): '_powl : VAL' p[0] = p[1] write_list(inv_addmod(p[1]))
def p_powr_UNARY_MINUS(p): '_powr : SUB _powr %prec UNARY_MINUS' global MOD_G p[0] = -p[2] top: int = pop() write_list(inv_addmod(-top, mod=MOD_G - 1))
def p_powr_VAL(p): '_powr : VAL' global MOD_G p[0] = p[1] write_list(inv_addmod(p[1], mod=MOD_G - 1))
def p_umin_BRACES(p): '_umin : L_BRACE expr R_BRACE' p[0] = p[2] write_list('n')
def p_umin_VAL(p): '_umin : VAL' p[0] = p[1] write_list(inv_addmod(-p[1]))
def p_expr_POW(p): 'expr : _powl POW _powr' p[0] = pow_mod(true_modulo(p[1], MOD_G), true_modulo(p[3], MOD_G)) write_list('^')
def p_expr_VAL(p): 'expr : VAL' p[0] = p[1] write_list(inv_addmod(p[1]))