-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpycalc.py
172 lines (149 loc) · 3.85 KB
/
pycalc.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
#
# Copyright © 2019 Keith Packard <keithp@keithp.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
import sys
lex_c = False
def getc():
global lex_c
if lex_c:
c = lex_c
lex_c = False
else:
c = sys.stdin.read(1)
return c
def ungetc(c):
global lex_c
lex_c = c
lex_value = False
def lex():
global lex_value
while True:
c = getc()
if c == '':
return 'END'
if c == '+':
return "PLUS"
if c == '-':
return "MINUS"
if c == '*':
return "TIMES"
if c == '/':
return "DIVIDE"
if c == '(':
return "OP"
if c == ')':
return "CP"
if c == '\n':
return "NL"
if '0' <= c and c <= '9':
v = ord(c) - ord('0')
while True:
c = getc()
if '0' <= c and c <= '9':
v = v * 10 + ord(c) - ord('0')
else:
ungetc(c)
break;
lex_value = v
return "NUMBER"
value_stack = ()
def head(s):
return s[0]
def rest(s):
return s[1:]
def push(v):
global value_stack
value_stack = (v,) + value_stack
def pop():
global value_stack
v = head(value_stack)
value_stack = rest(value_stack)
return v
#
# lines : line lines
# |
#
# expr : term PLUS expr
# | term MINUS expr
# | term
#
# expr : term expr-p
# expr-p: PLUS expr
# | MINUS expr
# |
from pycalc_gram import *
def is_non_terminal(item):
return item[0].islower()
def is_terminal(item):
return item[0].isupper()
def is_action(item):
return item[0] == '@'
def error(msg):
print(msg)
exit(1)
def test():
global lex_value
global value_stack
global parse_table
table = parse_table
stack = ('start',)
token = False
while True:
if stack:
top = head(stack)
stack = rest(stack)
else:
top = False
if top and is_action(top):
if top == "@PUSH":
push(lex_value)
elif top == "@ADD":
push(pop() + pop())
elif top == "@SUBTRACT":
a = pop()
b = pop()
push(b - a)
elif top == "@TIMES":
push(pop() * pop())
elif top == "@DIVIDE":
a = pop()
b = pop()
push(b / a)
elif top == "@NEGATE":
a = pop()
push(-a)
elif top == "@PRINT":
print("= %r" % pop())
continue
if not token:
token = lex()
if not top:
if token == 'END':
return
error("parse stack empty at %r" % token)
if is_terminal(top):
if top == token:
token = False
else:
error("parse error. got %r expected %r" % (token, top))
else:
key = (token, top)
if key not in table:
error("parse error at %r %r" % (token, top))
stack = table[key] + stack
test()