Advertisement
Horikita_Suzune

Untitled

Dec 14th, 2019
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. from ply.lex import lex
  2. from ply.yacc import yacc
  3. from decimal import *
  4. setcontext(Context(prec=2000,rounding=ROUND_HALF_DOWN))
  5. factorial=[1]
  6. fac=Decimal('1')
  7. for i in range(2,1000):
  8.     fac*=Decimal(i)
  9.     factorial.append(fac)
  10.  
  11. def sin(s):
  12.     sin=Decimal(s)
  13.     s=Decimal(s)
  14.     for i in range(5,1000,4):
  15.         sin+=(Decimal((s**Decimal(i)))/Decimal(factorial[i-1]))
  16.     for i in range(3,1000,4):
  17.         sin-=(Decimal((s**Decimal(i)))/Decimal(factorial[i-1]))
  18.     return sin
  19.  
  20. def cos(s):
  21.     cos=Decimal('1')
  22.     s=Decimal(s)
  23.     for i in range(4,1000,4):
  24.         cos+=(Decimal((s**Decimal(i)))/Decimal(factorial[i-1]))
  25.     for i in range(2,1000,4):
  26.         cos-=(Decimal((s**Decimal(i)))/Decimal(factorial[i-1]))
  27.     return cos
  28.  
  29. def tan(s):
  30.     return sin(s)/cos(s)
  31.  
  32. tokens=['NUM','PLUS','MINUS','TIMES','DIVIDE','LPAREN','RPAREN','SIN','COS','TAN']
  33. #tokens=['NUM','PLUS','MINUS','TIMES','DIVIDE','LPAREN','RPAREN', 'SIN']
  34.  
  35. t_ignore=' \t\n'
  36.  
  37. t_PLUS=r'\+'
  38. t_MINUS=r'\-'
  39. t_TIMES=r'\*'
  40. t_DIVIDE=r'/'
  41. t_LPAREN=r'\('
  42. t_RPAREN=r'\)'
  43. t_SIN=r'sin'
  44. t_COS=r'cos'
  45. t_TAN=r'tan'
  46.  
  47. def t_NUM(t):
  48.     r'\d+(\.\d+)?'
  49.  
  50.     t.value=Decimal(t.value)
  51.     return t
  52.  
  53. def t_SIN(t):
  54.     r'sin\((\d+(\.\d+)?)\)'
  55.     t.value=sin(Decimal(t))
  56.     return t
  57.  
  58. def t_COS(t):
  59.     r'cos\((\d+(\.\d+)?)\)'
  60.     t.value=cos(Decimal(t))
  61.     return t
  62.  
  63. def t_TAN(t):
  64.     r'tan\((\d+(\.\d+)?)\)'
  65.     t.value=tan(Decimal(t))
  66.     return t
  67.  
  68. def t_error(t):
  69.     print('Bad Character: {!r}'.format(t.value[0]))
  70.     t.lexer.skip(1)
  71.  
  72. lexer=lex()
  73.  
  74. def p_expr(p):
  75.     '''
  76.    expr : expr PLUS term
  77.         | expr MINUS term
  78.    '''
  79.     if p[2]=='+':
  80.         p[0]=p[1]+p[3]
  81.     elif p[2]=='-':
  82.         p[0]=p[1]-p[3]
  83.  
  84. def p_expr_term(p):
  85.   '''
  86.  expr : term
  87.  '''
  88.   p[0]=p[1]
  89.  
  90. def p_term(p):
  91.   '''
  92.  term : term TIMES factor
  93.       | term DIVIDE factor
  94.  '''
  95.   if p[2]=='*':
  96.     p[0]=p[1]*p[3]
  97.   elif p[2]=='/':
  98.     p[0]=p[1]/p[3]
  99.  
  100. def p_term_factor(p):
  101.   '''
  102.  term : factor
  103.  '''
  104.   p[0]=p[1]
  105.  
  106. def p_factor(p):
  107.   '''
  108.  factor : NUM
  109.  '''
  110.   p[0]=p[1]
  111.  
  112. '''
  113. def p_factor(p):
  114.    
  115.    factor : SIN
  116.           | COS
  117.           | TAN
  118.    
  119.    
  120.    if p[0]=='sin':
  121.        p[0]=Decimal(sin(Decimal(p[1])))
  122.    elif p[0]=='cos':
  123.        p[0]=Decimal(cos(Decimal(p[1])))
  124.    elif p[0]=='tan':
  125.        p[0]=Decimal(sin(Decimal(p[1]))/cos(Decimal(p[1])))
  126.    
  127.    p[0]=p[1]
  128. '''
  129.  
  130. def p_factor_group(p):
  131.   '''
  132.  factor : LPAREN expr RPAREN
  133.  '''
  134.   p[0]=p[2]
  135.  
  136. def p_error(p):
  137.   if p:
  138.       print("Syntax error at '%s'" % p.value)
  139.   else:
  140.       print("Syntax error at EOF")
  141.  
  142. #if __name__ == '__main__':
  143. parser=yacc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement