-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mparser.py
223 lines (173 loc) · 7.38 KB
/
Mparser.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/python
import re
import scanner
import AST
class MParser:
def __init__(self, scanner):
self.scanner = scanner
tokens = scanner.Scanner.tokens
precedence = (
("nonassoc", 'IF'),
("nonassoc", 'ELSE'),
("right", '='),
("nonassoc", "ADDASSIGN", "SUBASSIGN", "MULASSIGN", "DIVASSIGN"),
("nonassoc", 'LESS', 'GREATER', "LESSEQ", "GREATEREQ", "EQ", "NOTEQ"),
("left", '+', '-', 'DOTADD', 'DOTSUB'),
("left", '*', '/', "DOTMUL", "DOTDIV"),
("right", 'NEGATION'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, self.scanner.find_tok_column(p), p.type, p.value))
else:
print("Unexpected end of input")
def p_program(self, p):
"""program : instructions"""
program = AST.Program(p[1])
p[0] = program
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
if len(p) == 3:
p[1].instructions.append(p[2])
p[0] = p[1]
else:
p[0] = AST.Instructions()
p[0].instructions.append(p[1])
def p_instruction(self, p):
"""instruction : return_instruction
| break_instruction
| continue_instruction
| print_instruction
| while_instruction
| for_instruction
| if_instruction
| assignment_instruction
| compound_assignment_instruction
| compound_instruction
"""
#instrukcja = ktoras z tych
p[0] = p[1]
def p_return_instruction(self, p):
"""return_instruction : RETURN expression ';' """
p[0] = AST.ReturnInstruction(p[2], p.lineno(1)) # bez return
def p_break_instruction(self, p):
"""break_instruction : BREAK ';' """
p[0] = AST.BreakInstruction(p.lineno(1))
def p_continue_instr(self,p):
"""continue_instruction : CONTINUE ';' """
p[0] = AST.ContinueInstruction(p.lineno(1))
def p_print_instruction(self, p):
"""print_instruction : PRINT expression_list ';'
| PRINT '"' expression_list '"' ';'
"""
if len(p) == 6:
p[0] = AST.PrintInstructions(p[3], p.lineno(1))
else:
p[0] = AST.PrintInstructions(p[2], p.lineno(1))
def p_assignment_instruction(self, p):
"""assignment_instruction : variable_expression '=' expression ';' """
p[0] = AST.Assignment(p[1], p[3], p.lineno(2))
def p_matrix_assignment(self, p):
"""assignment_instruction : variable_expression '=' '[' expression_lists ']' ';'"""
p[0] = AST.MatrixAssignment(p[1], p[4], p.lineno(2))
def p_binary_expression(self, p):
"""expression : expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| expression EQ expression
| expression NOTEQ expression
| expression LESS expression
| expression GREATER expression
| expression LESSEQ expression
| expression GREATEREQ expression
| expression DOTADD expression
| expression DOTSUB expression
| expression DOTMUL expression
| expression DOTDIV expression
"""
p[0] = AST.BinaryExpression(p[1], p[2], p[3], p.lineno(2))
def p_neg_unary_expression(self, p):
"""expression : '-' expression %prec NEGATION """
p[0] = AST.NegUnaryExpression(p[2], p.lineno(1))
def p_trans_unary_expression(self, p):
"""expression : expression "'" """
p[0] = AST.TransUnaryExpression(p[1], p.lineno(2))
def p_matrix_initialization_zeros(self, p):
"""expression : ZEROS '(' expression ')'"""
p[0] = AST.ZerosInitialization(p[3], p.lineno(1))
def p_matrix_initialization_ones(self, p):
"""expression : ONES '(' expression ')'"""
p[0] = AST.OnesInitialization(p[3], p.lineno(1))
def p_matrix_initialization_eye(self, p):
"""expression : EYE '(' expression ')'"""
p[0] = AST.EyeInitialization(p[3], p.lineno(1))
def p_compound_assignment(self, p):
"""compound_assignment_instruction : variable_expression ADDASSIGN expression ';'
| variable_expression SUBASSIGN expression ';'
| variable_expression MULASSIGN expression ';'
| variable_expression DIVASSIGN expression ';'
"""
p[0] = AST.CompoundAssignment(p[1], p[2], p[3], p.lineno(2))
def p_if_instruction(self, p):
"""if_instruction : IF '(' condition ')' instruction %prec IF
| IF '(' condition ')' instruction ELSE instruction"""
if len(p) == 8:
p[0] = AST.IfElseInstruction(p[3], p[5], p[7], p.lineno(1))
else:
p[0] = AST.IfInstruction(p[3], p[5], p.lineno(2))
def p_while_instruction(self, p):
"""while_instruction : WHILE '(' condition ')' instruction"""
p[0] = AST.WhileInstruction(p[3], p[5], p.lineno(3))
def p_for_instruction(self, p):
"""for_instruction : FOR variable_expression '=' expression ':' expression instruction """
p[0] = AST.ForInstruction(p[2], p[4], p[6], p[7], p.lineno(1))
def p_compound_instruction(self, p):
"""compound_instruction : '{' instructions '}' """
p[0] = AST.CompoundInstruction(p[2], p.lineno(1))
def p_condition(self, p):
"""condition : expression"""
p[0] = p[1]
def p_constant(self, p):
"""expression : FLOATNUM
| INTNUM
| STRING
"""
if re.match(r"\d*\.\d+", str(p[1])):
type = 'float'
elif re.match(r"\d+", str(p[1])):
type = 'int'
else:
type = 'string'
p[0] = AST.Constant(p[1], type, p.lineno(1))
def p_variable_expression(self, p):
"""expression : variable_expression"""
p[0] = p[1]
def p_ID(self, p):
"""variable_expression : ID
| ID '[' expression ',' expression ']'"""
if len(p) == 2:
p[0] = AST.Variable(p[1], p.lineno(1))
else:
p[0] = AST.MatrixElement(p[1], p[3], p[5], p.lineno(2))
def p_paren_expression(self, p):
"""expression : '(' expression ')'"""
p[0] = p[2]
def p_expression_lists(self, p):
"""expression_lists : expression_lists ';' expression_list
| expression_list """
if len(p) == 4:
p[0] = AST.ListsOfExpressions(p.lineno(2))
p[0].flat_expressions(p[1].expression_lists, p[3])
else:
p[0] = AST.ListsOfExpressions(p.lineno(1))
p[0].append_expression(p[1])
def p_expression_list(self, p):
"""expression_list : expression_list ',' expression
| expression """
p[0] = AST.ListOfExpressions(p.lineno(1))
if len(p) == 4:
p[0].flat_expressions(p[1].expression_list, p[3])
else:
p[0].append_expression(p[1])