-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_parser.py
More file actions
159 lines (141 loc) · 6.19 KB
/
Copy pathmy_parser.py
File metadata and controls
159 lines (141 loc) · 6.19 KB
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
from rply import ParserGenerator
class Parser():
def __init__(self):
self.pg = ParserGenerator(
# A list of all token names accepted by the parser.
['START_SEQUENCE', 'END_SEQUENCE', 'STAGE', 'END_STAGE', 'STAGE_ATTRIBUTES',
'PROGRAM', 'REQUIRES', 'END_PROGRAM', 'INITATE', 'BURN_START', 'FOR', 'BURN_END',
'STATUS_REPORT', 'ELSE', 'END_REPORT', 'SUM', 'SUB', 'MUL', 'DIV', 'GREATER_THAN',
'LESS_THAN', 'EQUAL_TO', 'NOT_EQUAL_TO', 'LESS_THAN_OR_EQUAL_TO', 'GREATER_THAN_OR_EQUAL_TO', 'NOT', 'TIME', 'EQUAL', 'CONFIRM', 'COLON', 'PERIOD',
'NEW_LINE', 'NUMBER', 'IDENTIFIER']
)
def parse(self):
# BLOCK
@self.pg.production('program : NEW_LINE program')
@self.pg.production('program : NEW_LINE')
@self.pg.production('program : START_SEQUENCE NEW_LINE block')
@self.pg.production('program : START_SEQUENCE NEW_LINE block NEW_LINE')
def program(p):
return p[1]
@self.pg.production('block : END_SEQUENCE')
def block_end(p):
return p[0]
@self.pg.production('block : build block')
def block_statement(p):
return p[0]
# BUILD
@self.pg.production('build : NEW_LINE')
def build_newline(p):
return p[0]
@self.pg.production('build : IDENTIFIER EQUAL NUMBER NEW_LINE')
def build_iden(p):
return p[2]
@self.pg.production('build : STAGE IDENTIFIER COLON NEW_LINE stage NEW_LINE')
def build_stage(p):
return p[4]
@self.pg.production('build : PROGRAM IDENTIFIER REQUIRES IDENTIFIER NEW_LINE statement END_PROGRAM NEW_LINE')
def build_program(p):
return p[5]
@self.pg.production('build : INITATE IDENTIFIER expression NEW_LINE')
@self.pg.production('build : INITATE IDENTIFIER NEW_LINE')
def build_initiate(p):
if len(p) == 3:
return p[1]
return p[2]
# STAGE
@self.pg.production('stage : stage_attribute NEW_LINE stage')
@self.pg.production('stage : END_STAGE')
def stage_statement(p):
return p[0]
# STAGE ATTRIBUTE
@self.pg.production('stage_attribute : STAGE_ATTRIBUTES EQUAL expression')
def stage_attribute(p):
return p[2]
# STATEMENT
@self.pg.production('statement : BURN_START FOR NUMBER TIME IDENTIFIER NEW_LINE statement BURN_END NEW_LINE')
def statement_burn(p):
return p[6]
@self.pg.production('statement : STATUS_REPORT relational_expression NEW_LINE statement END_REPORT NEW_LINE')
def statement_report(p):
return p[4]
@self.pg.production('statement : STATUS_REPORT relational_expression NEW_LINE statement ELSE NEW_LINE statement END_REPORT NEW_LINE')
def statement_report_else(p):
return p[4]
@self.pg.production('statement : CONFIRM NEW_LINE')
def statement_confirm(p):
return p[0]
# RELATIONAL EXPRESSION
@self.pg.production('relational_expression : expression')
def relational_expression_expression(p):
return p[0]
@self.pg.production('relational_expression : expression GREATER_THAN expression')
@self.pg.production('relational_expression : expression LESS_THAN expression')
@self.pg.production('relational_expression : expression EQUAL_TO expression')
@self.pg.production('relational_expression : expression NOT_EQUAL_TO expression')
@self.pg.production('relational_expression : expression GREATER_THAN_OR_EQUAL_TO expression')
@self.pg.production('relational_expression : expression LESS_THAN_OR_EQUAL_TO expression')
def relational_expression(p):
left = p[0]
right = p[2]
operator = p[1]
if operator.gettokentype() == 'GREATER_THAN':
return operator
elif operator.gettokentype() == 'LESS_THAN':
return operator
elif operator.gettokentype() == 'EQUAL_TO':
return operator
elif operator.gettokentype() == 'NOT_EQUAL_TO':
return operator
elif operator.gettokentype() == 'GREATER_THAN_OR_EQUAL_TO':
return operator
elif operator.gettokentype() == 'LESS_THAN_OR_EQUAL_TO':
return operator
# EXPRESSION
@self.pg.production('expression : term')
def expression_term(p):
return p[0]
@self.pg.production('expression : term SUM term')
@self.pg.production('expression : term SUB term')
def expression(p):
left = p[0]
right = p[2]
operator = p[1]
if operator.gettokentype() == 'SUM':
return operator
elif operator.gettokentype() == 'SUB':
return operator
# TERM
@self.pg.production('term : factor')
def term_factor(p):
return p[0]
@self.pg.production('term : factor MUL factor')
@self.pg.production('term : factor DIV factor')
def term(p):
left = p[0]
right = p[2]
operator = p[1]
if operator.gettokentype() == 'MUL':
return operator
elif operator.gettokentype() == 'DIV':
return operator
# FACTOR
@self.pg.production('factor : NUMBER')
def factor_number(p):
return p[0].value
@self.pg.production('factor : IDENTIFIER')
def factor_identifier(p):
return p[0].value
@self.pg.production('factor : NOT factor')
def factor_not(p):
return p[0]
@self.pg.production('factor : CONFIRM')
def factor_confirm(p):
return p[0]
@self.pg.production('factor : IDENTIFIER PERIOD STAGE_ATTRIBUTES')
def factor_stage_attribute(p):
return p[2]
@self.pg.error
def error_handle(token):
raise ValueError(token)
def get_parser(self):
return self.pg.build()