-
Notifications
You must be signed in to change notification settings - Fork 0
/
our_parser.py
221 lines (142 loc) · 4.1 KB
/
our_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
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
219
220
221
import our_lexer
import ply.yacc as yacc
tokens = our_lexer.tokens
# by default start symbol is defined by the first rule function defined
# but the below variable let's functions be moved :)
start = 'program'
def p_program(p):
' program : A B C BEGIN D END'
def p_A(p):
'''A : empty
| CONST A_1'''
def p_A_1(p):
'''A_1 : const_decl
| const_decl A_1'''
def p_B(p):
'''B : empty
| VAR B_1'''
def p_B_1(p):
'''B_1 : var_decl
| var_decl B_1'''
def p_C(p):
'''C : empty
| proc_decl C'''
def p_D(p):
'''D : statement
| statement D'''
def p_const_decl(p):
''' const_decl : identifier E '=' integer_constant'''
def p_E(p):
'''E : empty
| ',' identifier E'''
def p_var_decl(p):
""" var_decl : identifier E ':' type """
def p_proc_decl(p):
"""proc_decl : PROCEDURE identifier '(' F ')' ';' block"""
def p_F(p):
'''F : empty
| format F_1'''
def p_F_1(p):
'''F_1 : empty
| ';' format F_1'''
def p_format(p):
'''format : identifier E ':' type
| identifier E ':' mode type'''
def p_mode(p):
'''mode : IN
| OUT
| IN_OUT'''
def p_type(p):
'type : INTEGER'
def p_block(p):
'''block : BEGIN A B D END
| BEGIN A B END'''
def p_print(p):
""" print : PRINT '(' string_constant G ')' """
def p_G(p):
'''G : empty
| ',' expression G'''
def p_read(p):
'''read : READ '(' string_constant H ')' '''
def p_H(p):
''' H : empty
| ',' var H'''
def p_cond(p):
''' cond : IF bool THEN statement
| IF bool THEN statement ELSE statement'''
def p_call(p):
"""call : CALL identifier '(' ')'
| CALL identifier '(' expression G ')' """
def p_var(p):
"""var : identifier
| identifier '[' expression ']' """
def p_statement_assign(p):
'''statement_assign : var assign_op expression
'''
def p_statement(p):
'''statement : block
| print
| read
| statement_assign
| cond
| statement_for
| return
| call'''
def p_return(p):
'return : RETURN'
def p_bool(p):
""" bool : NOT bool
| bool AND bool
| bool OR bool
| expression relop expression
| '(' bool ')' """
def p_empty(p):
'empty :'
pass
def p_statement_for(p):
' statement_for : FOR identifier assign_op TO expression DO statement'
def p_relop(p):
""" relop : '='
| less_equal_sign
| '>'
| greater_equal_sign
| '<'
| not_equal_sign """
# expressions : **************
precedence = (
('left', 'AND', 'OR'),
('right','NOT'),
('left', '>', '<',
'greater_equal_sign',
'less_equal_sign',
'not_equal_sign'),
('left', '+', '-'),
('left', '*', '/'),
('right', 'UMINUS')
)
def p_expression(p):
"""expression : integer_constant
| expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| '-' expression %prec UMINUS
| '(' expression ')'
| var """
# ################## Error Recovery ##############
def p_error(p): # panic mode
if not p:
print("unexpected end of file")
return
# Read ahead looking for a closing '}'
print("error here -->{}:{} near {}".format(p.lineno, p.lexpos, p.value))
while True:
tok = parser.token() # Get the next token
if not tok or tok.type == 'END':
break
parser.restart()
# ##################################################
parser = yacc.yacc(tabmodule='parsing_tables')
with open('input.txt') as f:
s = f.read()
yacc.parse(s)