-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
57 lines (43 loc) · 1.39 KB
/
parser.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
from ply import yacc
from lexer import Lexer
from nonTerminal import NonTerminal
from codeGenerator import CodeGenerator
class Parser:
tokens = Lexer().tokens
def __init__(self):
self.tempCount = 0
self.codeGenerator = CodeGenerator()
def p_program(self, p):
"program : exp"
pass
def p_exp_sum(self, p):
"exp : exp SUM exp"
self.codeGenerator.generate_arithmetic_code(p, self.new_temp())
def p_exp_sub(self, p):
"exp : exp SUB exp"
self.codeGenerator.generate_arithmetic_code(p, self.new_temp())
def p_exp_mul(self, p):
"exp : exp MUL exp"
self.codeGenerator.generate_arithmetic_code(p, self.new_temp())
def p_exp_div(self, p):
"exp : exp DIV exp"
self.codeGenerator.generate_arithmetic_code(p, self.new_temp())
def p_exp_integer(self, p):
"exp : INTEGER"
p[0] = NonTerminal()
p[0].value = p[1]
def new_temp(self):
temp = "T" + str(self.tempCount)
self.tempCount += 1
return temp
precedence = (
('left', 'SUM', 'SUB'),
('left', 'MUL', 'DIV')
)
def p_error(self, p):
print(p.value)
raise Exception('ParsingError: invalid grammar at ', p)
def build(self, **kwargs):
"""build the parser"""
self.parser = yacc.yacc(module=self, **kwargs)
return self.parser