• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# calc

ZazoTazo Apr 4th, 2020 136 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. ###
2. ### Zeyad Hesham Nofal 41610028
3. ###
4. import ply.lex as lex
5. import ply.yacc as yacc
6. import sys
7.
8. tokens = [
9.
10.     'INT',
11.     'FLOAT',
12.     'NAME',
13.     'PLUS',
14.     'MINUS',
15.     'DIVIDE',
16.     'MULTIPLY',
17.     'EQUALS'
18.
19. ]
20.
21. t_PLUS = r'\+'
22. t_MINUS = r'\-'
23. t_MULTIPLY = r'\*'
24. t_DIVIDE = r'\/'
25. t_EQUALS = r'\='
26. t_ignore = r' '
27.
28. def t_FLOAT(t):
29.     r'\d+\.\d+'
30.     t.value = float(t.value)
31.     return t
32.
33. def t_INT(t):
34.     r'\d+'
35.     t.value = int(t.value)
36.     return t
37.
38. def t_NAME(t):
39.     r'[a-zA-Z_][a-zA-Z_0-9]*'
40.     t.type = 'NAME'
41.     return t
42.
43. def t_error(t):
44.     print("Illegal characters!")
45.     t.lexer.skip(1)
46.
47. lexer = lex.lex()
48.
49. precedence = (
50.
51.     ('left', 'PLUS', 'MINUS'),
52.     ('left', 'MULTIPLY', 'DIVIDE')
53.
54. )
55.
56. def p_calc(p):
57.     '''
58.    calc : expression
59.         | var_assign
60.         | empty
61.    '''
62.     print(run(p[1]))
63.
64. def p_var_assign(p):
65.     '''
66.    var_assign : NAME EQUALS expression
67.    '''
68.     p[0] = ('=', p[1], p[3])
69.
70. def p_expression(p):
71.     '''
72.    expression : expression MULTIPLY expression
73.               | expression DIVIDE expression
74.               | expression PLUS expression
75.               | expression MINUS expression
76.    '''
77.     p[0] = (p[2], p[1], p[3])
78.
79. def p_expression_int_float(p):
80.     '''
81.    expression : INT
82.               | FLOAT
83.    '''
84.     p[0] = p[1]
85.
86. def p_expression_var(p):
87.     '''
88.    expression : NAME
89.    '''
90.     p[0] = ('var', p[1])
91.
92. def p_error(p):
93.     print("Syntax error found!")
94.
95. def p_empty(p):
96.     '''
97.    empty :
98.    '''
99.     p[0] = None
100.
101.
102. parser = yacc.yacc()
103. env = {}
104.
105. def run(p):
106.     global env
107.     if type(p) == tuple:
108.         if p[0] == '+':
109.             return run(p[1]) + run(p[2])
110.         elif p[0] == '-':
111.             return run(p[1]) - run(p[2])
112.         elif p[0] == '*':
113.             return run(p[1]) * run(p[2])
114.         elif p[0] == '/':
115.             return run(p[1]) / run(p[2])
116.         elif p[0] == '=':
117.             env[p[1]] = run(p[2])
118.             #return ''
119.             print(env)
120.         elif p[0] == 'var':
121.             if p[1] not in env:
122.                 return 'Undeclared variable found!'
123.             else:
124.                 return env[p[1]]
125.     else:
126.         return p
127.
128. while True:
129.     try:
130.         s = input('>> ')
131.     except EOFError:
132.         break
133.     parser.parse(s)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top