forked from yesusbc/MaeStar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaeStarLex.py
198 lines (162 loc) · 5.25 KB
/
maeStarLex.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
import ply.lex as lex
import quadruple
square_obj = quadruple.Quadruple()
# Quadruple
operand_stack = list()
# Variable Tables
variable_tables = dict()
# Procedure directory
procedure_directory = dict()
# Sets for saving the line number of different variables
int_set = set()
double_set = set()
method_set = set()
# List of token names
tokens = [ 'ID', 'CONST', 'MINUS', 'DIVISION', 'TIMES', 'MOD', 'LPAREN', 'RPAREN', 'LBRACE', 'RBRACE',
'LBRACKET', 'RBRACKET','LESSTHAN', 'GREATERTHAN', 'EQUALS', 'NOTEQUAL', 'COMA', 'SEMICOLON',
'ASSIGN', 'PLUSPLUS', 'MINUSMINUS', 'STRING']
# Reserved words
reserved = {'if': 'IF',
'do': 'DO',
'while': 'WHILE',
'main': 'MAIN',
'for': 'FOR',
'else': 'ELSE',
'read': 'READ',
'write': 'WRITE',
'call': 'CALL',
'int': 'INT',
'double': 'DOUBLE',
'method': 'METHOD',
'plus': 'PLUS',
'and': 'AND',
'or': 'OR'
}
tokens += list(reserved.values())
# Each token is specified by writing a regular expression rule compatible with Python's re module.
# t_PLUS = r'\PLUS'
t_PLUSPLUS = r'\++'
t_MINUS = r'\-'
t_MINUSMINUS = r'\--'
t_DIVISION = r'/'
t_TIMES = r'\*'
t_MOD = r'\%'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACE = r'\{'
t_RBRACE = r'\}'
t_LBRACKET =r'\['
t_RBRACKET = r'\]'
#t_QUOT = r'["]'
t_LESSTHAN = r'\<'
t_GREATERTHAN = r'\>'
t_EQUALS = r'\=='
t_NOTEQUAL = r'\!='
t_COMA = r'\,'
t_SEMICOLON = r'\;'
t_ASSIGN = r'\='
# To ignore characters spaces and tabs
t_ignore = " \t\r"
"""
************************************************
************************************************
Regular expression rules with no action code
************************************************
************************************************
"""
# Check for reserved words
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t_dict = t.__dict__
line_number = t_dict['lineno']
value = t_dict['value']
if t.value in reserved:
t.type = reserved[t.value]
if value == 'method':
method_set.add(t_dict['lineno'])
if value == 'main':
if len(method_set) > 0:
num = square_obj.get_num()
square_obj.quadruple_dict[0].pop()
square_obj.quadruple_dict[0].append(num + 1)
else:
operand_stack.append(value) # Push "operand's stack" (id)
if line_number in int_set:
if value not in variable_tables:
variable_tables[line_number] = [['int', 0],['symbol', value]]
elif line_number in double_set:
if value not in variable_tables:
variable_tables[line_number] = [['doubles', 0],['symbol', value]]
elif line_number in method_set:
operand_stack.pop()
if len(method_set) == 1:
square_obj.square("goto", '_', '_', '_')
procedure_directory[value] = square_obj.get_num()
# print(procedure_directory)
return t
def t_STRING(t):
r'"([^"\n]|(\\"))*"'
tval = t.__dict__
value = tval['value']
# A regular expression rule for CONSTANTS (NUMBERS)
def t_CONST(t):
r'\d+'
operand_stack.append(t.value)
t.value = int(t.value)
if t.__dict__['lineno'] in int_set:
# if len(variable_tables[t.__dict__['lineno']]) < 3:
if not isinstance(variable_tables[t.__dict__['lineno']][0][1], list):
# [0] * t.value First Dimension
variable_tables[t.__dict__['lineno']][0][1] = [variable_tables[t.__dict__['lineno']][0][1]] # 0 to list [0]
for i in range(t.value - 1):
variable_tables[t.__dict__['lineno']][0][1].append(0)
operand_stack.pop()
else:
# [0,0,0] * t.value Multiple Dimension
# We cannot do [0]*number because that create a reference
# Therefore we need to create every object individually
len_array = len(variable_tables[t.__dict__['lineno']][0][1])
variable_tables[t.__dict__['lineno']][0][1] = [variable_tables[t.__dict__['lineno']][0][1]]
for _ in range(t.value - 1):
to_add_list = list()
for i in range(len_array):
to_add_list.append(0)
variable_tables[t.__dict__['lineno']][0][1].append(to_add_list)
operand_stack.pop()
"""
try:
t.value = int(t.value)
except ValueError:
print("Integer value too large {0}".format(t.value))
t.value = 0
"""
return t
def t_COMMENT(t):
r'\#.*'
pass
# No return value. Token discarded
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# print(operand_stack)
# for x in square_obj.quadruple_dict:
# print(square_obj.quadruple_dict[x])
# print("********")
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
""" ************For testing the lexer give the lexer some input************
while True:
# Tokenize
print("*"*40)
s = input()
lexer.input(s)
tok = lexer.token()
if not tok:
break # No more input
print(tok)
"""