-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
154 lines (121 loc) · 2.83 KB
/
project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from ply import lex, yacc
import cmath as math
'''
expr : term2 term1 term0
term2 : SIGN NUMBER VARIABLE CARAT 2
term1 : SIGN NUMBER VARIABLE term1a
term1a : CARAT 1 | empty
term0 : SIGN NUMBER term0a
term0a : VARIABLE CARAT 0 | empty
'''
# List of token names. This is always required
tokens = (
'NUMBER',
'VARIABLE',
'PLUS',
'MINUS',
'CARAT',
'SIGN'
)
# Regular expression rules for simple tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_VARIABLE = r'[a-zA-Z]'
t_CARAT = r'\^'
t_SIGN = r'-|\+'
# A regular expression rule with some action code
def t_NUMBER(t):
r'\d+'
try:
t.value = int(t.value)
except ValueError:
print("Integer value too large %d", t.value)
t.value = 0
return t
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
quad={}
def p_expr(t):
'''expr : term2 term1 term0'''
print(f'Solution 1 : {quad["sol1"]}')
print(f'Solution 2 : {quad["sol2"]}')
def p_expr_term2(t):
'''term2 : SIGN NUMBER VARIABLE CARAT NUMBER'''
if (t[5] == 2):
if (t[1] == '+'):
quad['a'] = t[2]
elif(t[1] == '-'):
quad['a'] = t[2] * -1
else:
#throw some error?
quad['a'] = -1
def p_expr_term1a(t):
'''term1a : CARAT NUMBER
| empty'''
if (t[1] == '^'):
if(t[2] == '1'):
pass
else:
#error?
pass
#quad['b'] = -1
def p_expr_term1(t):
'''term1 : SIGN NUMBER VARIABLE term1a'''
if (t[1] == '+'):
quad['b'] = t[2]
elif(t[1] == '-'):
quad['b'] = t[2] * -1
elif(t[1] == ''):
quad['b'] = 0
else:
#throw some error?
quad['b'] = -1
def p_expr_term0a(t):
'''term0a : VARIABLE CARAT NUMBER
| empty '''
if (t[1] == 'x'):
if(t[3] == '0'):
pass
else:
#error?
pass
#quad['c'] = -1
def p_expr_term0(t):
'''term0 : SIGN NUMBER term0a'''
if (t[1] == '+'):
quad['c'] = t[2]
elif(t[1] == '-'):
quad['c'] = t[2] * -1
elif (t[1] == ''):
quad['c'] = 0
else:
#throw some error?
quad['c'] = -1
a = quad['a']
b = quad['b']
c = quad['c']
quad['sol1'] = (-b - math.sqrt((b**2) - (4 * a * c)) / (2 * a))
quad['sol2'] = (-b + math.sqrt((b**2) - (4 * a * c)) / (2 * a))
def p_error(t):
print(f"Syntax error at '{t.value}'")
print(t)
def p_empty(t):
'empty :'
pass
parser = yacc.yacc()
while True:
try:
w = input('>')
except EOFError:
break
parser.parse(w)