-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
161 lines (144 loc) · 4.53 KB
/
lexer.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
from lextable import LexTable
from source import Source
from compileerror import CompileError
# all key words are in lower case, operators are in upper case
TYPE = {
'begin': 1,
'end': 2,
'if': 3,
'then': 4,
'else': 5,
'ID': 6,
'INT': 7,
'<': 8,
'<=': 9,
'=': 10,
'<>': 11,
'>': 12,
'>=': 13,
'+': 14,
'-': 15,
'*': 16,
'/': 17,
':=': 18,
'var': 19,
'while': 20,
'do': 23,
';': 24, # End Of Sentence
'(': 27, # (
')': 28 # )
}
def lex(source: Source) -> LexTable:
"""
:param source_path: path to the source code file
:return: List of tuple, which stores key word and identifiers and its type
"""
lex_table = LexTable(source) # type:LexTable
while True:
ch = source.get()
if ch in ['\n', '\r', ' ']:
continue
if ch is None:
return lex_table
buffer = []
if ch.isalpha():
buffer.append(ch)
ch = source.get()
while ch.isalnum():
buffer.append(ch)
ch = source.get()
source.back()
try:
res = (TYPE[''.join(buffer).lower()], ''.join(buffer).lower())
except KeyError:
res = (TYPE['ID'], ''.join(buffer))
lex_table.append(res)
elif ch.isdigit():
buffer.append(ch)
ch = source.get()
while ch.isdigit():
buffer.append(ch)
ch = source.get()
source.back()
lex_table.append((TYPE['INT'], int(''.join(buffer))))
else: # not a identifier or number
if ch == '<':
ch = source.get()
if ch == '=':
lex_table.append((TYPE['<='], ch))
elif ch == '>':
lex_table.append((TYPE['<>'], '<>'))
else:
source.back()
lex_table.append((TYPE['<'], ch))
continue
elif ch == '=':
lex_table.append((TYPE['='], ch))
elif ch == '>':
ch = source.get()
if ch == '=':
lex_table.append((TYPE['>='], ch))
else:
source.back()
lex_table.append((TYPE['>'], '>'))
continue
elif ch == ':':
ch = source.get()
if ch == '=':
lex_table.append((TYPE[':='], ':='))
continue
else:
print_error(source, 'see : but no =, did you mean := ?')
break
elif ch == ';':
lex_table.append((TYPE[';'], ch))
continue
elif ch == '+':
lex_table.append((TYPE['+'], ch))
continue
elif ch == '-':
lex_table.append((TYPE['-'], ch))
continue
elif ch == '*':
lex_table.append((TYPE['*'], ch))
continue
elif ch == '(':
lex_table.append((TYPE['('], ch))
continue
elif ch == ')':
lex_table.append((TYPE[')'], ch))
continue
elif ch == '/':
ch = source.get()
if ch == '*':
ch = source.get()
while True:
if ch == '*':
ch = source.get()
if ch == '/':
break
ch = source.get()
else:
source.back()
lex_table.append((TYPE['/'], ch))
continue
elif ch == '{':
ch = source.get()
if ch == '*':
while True:
if ch == '*':
ch = source.get()
if ch == '}':
break
ch = source.get()
continue
else:
print('error at line:%d column%d' % (source.r, source.c))
raise CompileError('error', (source.r, source.c))
def print_error(source: Source, msg: str = ''):
print(msg + (' at row:%d, col:%d' % (source.r, source.c)))
raise CompileError(msg, (source.r, source.c))
if __name__ == '__main__':
source = Source('test.pas')
table = lex(source)
print(table.table)