-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.py
72 lines (53 loc) · 1.56 KB
/
example.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
from purplex import Lexer, TokenDef
from purplex import Parser, attach
from purplex import LEFT, RIGHT
class MyLexer(Lexer):
INTEGER = TokenDef(r'\d+')
LPAREN = TokenDef(r'\(')
RPAREN = TokenDef(r'\)')
TIMES = TokenDef(r'\*')
DIVIDE = TokenDef(r'/')
PLUS = TokenDef(r'\+')
MINUS = TokenDef(r'-')
WHITESPACE = TokenDef(r'[\s\n]+', ignore=True)
class MyParser(Parser):
LEXER = MyLexer
START = 'e'
PRECEDENCE = (
(RIGHT, 'UMINUS'),
(LEFT, 'TIMES', 'DIVIDE'),
(LEFT, 'PLUS', 'MINUS'),
)
@attach('e : LPAREN e RPAREN')
def brackets(self, lparen, expr, rparen):
return expr
@attach('e : e PLUS e')
def addition(self, left, op, right):
return left + right
@attach('e : e MINUS e')
def subtract(self, left, op, right):
return left - right
@attach('e : e TIMES e')
def multiply(self, left, op, right):
return left * right
@attach('e : e DIVIDE e')
def division(self, left, op, right):
return left / right
@attach('e : MINUS e', prec_symbol='UMINUS')
def negate(self, minus, expr):
return -expr
@attach('e : INTEGER')
def number(self, num):
return int(num)
if __name__ == '__main__':
parser = MyParser()
problems = [
('2 + 3 * 4 - 4', 10),
('-4', -4),
('-4 * 2', -8),
('-2 * - (1 + 1)', 4),
('6 / 2 * 4 - 8 * 1', 4),
]
for problem, answer in problems:
result = parser.parse(problem)
print(problem, '=', result, ';', result == answer)