-
Notifications
You must be signed in to change notification settings - Fork 0
/
myYacc.py
224 lines (195 loc) · 6.04 KB
/
myYacc.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
222
223
224
from myLex import tokens
import ply.yacc as yacc
import re
def p_Program(p):
"Program : HEAD Decls ENDHEAD Bodys END"
print(p[2] + "START\n" + p[4] + "STOP\n", end = "")
def p_Decls(p):
"Decls : Decls Decl"
p[0] = p[1] + p[2]
def p_Bodys(p):
"Bodys : Bodys Body"
p[0] = p[1] + p[2]
def p_DeclId(p):
"Decl : ID"
if p[1] in parser.idTab:
p[0] = """PUSHS "Error variable already in use"\n""" + "WRITES" + "\n"
else:
p[0] = "PUSHI 0\n"
parser.idTab[p[1]] = (parser.proxAddr, "INT", 1)
parser.proxAddr += 1
def p_DeclIdArray(p):
"Decl : ID '[' NUM ']'"
if p[1] in parser.idTab:
p[0] = """PUSHS "Error variable already in use"\n""" + "WRITES" + "\n"
else:
p[0] = "PUSHN " + str(p[3]) + "\n"
parser.idTab[p[1]] = (parser.proxAddr, "ARRAY", int(p[3]))
parser.proxAddr += int(p[3])
def p_Decl(p):
"Decl : ID '=' Expression"
if p[1] in parser.idTab:
p[0] = """PUSHS "Error variable already in use"\n""" + "WRITES" + "\n"
else:
p[0] = p[3]
parser.idTab[p[1]] = (parser.proxAddr, "INT", 1)
parser.proxAddr += 1
def p_Simple(p):
"""Bodys : Body
Decls : Decl
Body : Expression
Body : Atrib
Body : Logic
Expression : Term
Expression : Logic
Term : Factor
Body : String
Body : Read
Body : Cond
Body : Cicle
"""
p[0] = p[1]
def p_Empty(p):
"Body : "
"Decl : "
p[0] = ""
def p_Atrib(p):
"Atrib : ID '=' Expression"
if (p[1] in parser.idTab):
p[0] = p[3] + "STOREG " + str(parser.idTab[p[1]][0]) + "\n"
else:
p[0] = """PUSHS "Error variable not declared"\n""" + "WRITES" + "\n"
def p_ExpressionOperations(p):
"""Expression : Expression '+' Term
| Expression '-' Term
| Expression EQ Term
| Expression NEQ Term
| Expression '>' Term
| Expression MOREEQ Term
| Expression '<' Term
| Expression LESSEQ Term"""
if (p[2] == '+'):
p[0] = p[1] + p[3] + "ADD \n"
elif (p[2] == '-'):
p[0] = p[1] + p[3] + "SUB \n"
elif (p[2] == "=="):
p[0] = p[1] + p[3] + "EQUAL \n"
elif (p[2] == "!="):
p[0] = p[1] + p[3] + "EQUAL\nNOT\n"
elif (p[2] == '>'):
p[0] = p[1] + p[3] + "SUP \n"
elif (p[2] == ">="):
p[0] = p[1] + p[3] + "SUPEQ \n"
elif (p[2] == '<'):
p[0] = p[1] + p[3] + "INF \n"
elif (p[2] == "<="):
p[0] = p[1] + p[3] + "INFEQ \n"
def p_TermOperations(p):
"""Term : Term '*' Factor
| Term '/' Factor
| Term '%' Factor"""
if p[2] == '*':
p[0] = p[1] + p[3] + "MUL \n"
elif p[2] == '/':
p[0] = p[1] + p[3] + "DIV \n"
elif p[2] == '%':
p[0] = p[1] + p[3] + "MOD \n"
def p_FactorNum(p):
"Factor : NUM"
p[0] = "PUSHI " + str(p[1]) + "\n"
def p_FactorID(p):
"Factor : ID"
if (p[1] in parser.idTab):
p[0] = "PUSHG " + str(parser.idTab[p[1]][0]) + "\n"
else:
p[0] = """PUSHS "Error variable not declared"\n""" + "WRITES" + "\n"
def p_FactorExpression(p):
"Factor : '(' Expression ')'"
p[0] = p[2]
def p_LogicExpression(p):
"Logic : Expression"
p[0] = p[1]
def p_LogicAnd(p):
"Logic : '(' Logic '&' Expression ')'"
p[0] = p[2] + p[4] + "MUL \n"
def p_LogicOr(p):
"Logic : '(' Logic '|' Expression ')'"
p[0] = p[2] + p[4] + "ADD \n" + p[2] + p[4] + "MUL\nSUB\n"
def p_String(p):
"String : PRINT '(' STRING ')'"
p[0] = "PUSHS " + p[3] + "\n" + "WRITES\n"
def p_StringID(p):
"String : PRINT '(' ID ')'"
if (p[3] in parser.idTab):
p[0] = "PUSHG " + str(parser.idTab[p[3]][0]) + "\n" + "STRI\n" + "WRITES\n"
else:
p[0] = """PUSHS "Error variable not declared"\n""" + "WRITES" + "\n"
def p_ReadID(p):
"Read : INPUT '(' ID ')'"
if (p[3] in parser.idTab):
p[0] = "READ\nATOI\nSTOREG " + str(parser.idTab[p[3]][0]) + "\n"
else:
p[0] = """PUSHS "Error variable not declared"\n""" + "WRITES" + "\n"
def p_Cond(p):
"Cond : IF '(' Expression ')' '{' Bodys '}' ELSE '{' Bodys '}'"
p[0] = p[3] + "JZ I" + str(parser.ifCounter) + "\n" + p[6] + "JUMP EI" + str(parser.ifCounter) + "\nI" + str(parser.ifCounter) + ": nop\n" + p[10] + "EI" + str(parser.ifCounter) + ": nop\n"
parser.ifCounter += 1
def p_Cicle(p):
"Cicle : WHILE '(' Expression ')' DO '{' Bodys '}'"
p[0] = "W" + str(parser.whileCounter) + ": nop\n" + p[3] + "JZ EW" + str(parser.whileCounter ) + "\n" + p[7] + "JUMP W" + str(parser.whileCounter) + "\nEW" + str(parser.whileCounter) + ": nop\n"
parser.whileCounter += 1
def p_error(p):
print('Syntax error: ', p)
parser.success = False
parser = yacc.yacc()
parser.proxAddr = 0
parser.ifCounter = 0
parser.whileCounter = 0
parser.idTab = {}
with open("input.txt", 'r') as file:
text = file.read()
parser.success = True
parser.parse(text)
#"""
#Program : HEAD Decls ENDHEAD Bodys END
#Decls : Decls Decl
# | Decl
#Bodys : Bodys Body
# | Body
#Decl : ID
# | ID '=' Expression
# | "
#Body : Expression
# | Atrib
# | Logics
# | String
# | Read
# | Cond
# | Cicle
# | "
#Atrib : ID '=' Expression
#Expression : Term
# | Expression '+' Term
# | Expression '-' Term
# | Expression EQ Term
# | Expression NEQ Term
# | Expression MORE Term
# | Expression MOREEQ Term
# | Expression LESS Term
# | Expression LESSEQ Term
#Term : Factor
# | Term '*' Factor
# | Term '/' Factor
# | Term '%' Factor
#Factor : NUM
# | ID
# | '(' Expression ')'
#Logic : '(' Logic '&' Expression ')'
# | '(' Logic '|' Expression ')'
# | Expression
#String : PRINT '(' STRING ')'
# | PRINT '(' ID ')'"
#Read : INPUT '(' ID ')'
#Cond : IF '(' Expression ')' '{' Bodys '}' ELSE '{' Bodys '}'
#Cicle : WHILE '(' Expression ')' DO '{' Bodys '}'
#"""