-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexical.py
288 lines (235 loc) · 6.68 KB
/
lexical.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python
# ==============================================
#
# lexical.py
#
# Lexical Analyser and tokenizer
#
# ==============================================
import ply.lex as lex
# ----------------------------------
#
# Reserverd words or identifiers
#
# ----------------------------------
class MyLexer(object):
reserved = {
# Exceptions syntax
"begin": "BEGIN", # Starts an exception handling block.
"end": "END", # The end of a syntax block. Used by classes, modules, methods, exception handling and control expressions.
# Control expression syntax
"case": "CASE", # Starts a case expression.
# Control expressions
"if": "IF", # Used for if and modifier if statements.
"unless": "UNLESS", # Used for unless and modifier unless statements.
"then": "THEN", # Indicates the end of conditional blocks in control structures.
"elsif": "ELSIF", # An alternate condition for an if expression.
"else": "ELSE", # The unhandled condition in case, if and unless expressions.
"for": "FOR", # A loop that is similar to using the each method.
"in": "IN", # Used to separate the iterable object and iterator variable in a for
"next": "NEXT", # Skips the rest of the block.
"while": "WHILE", # Creates a loop that executes while the condition is true.
"do": "DO", # Starts a block.
"break": "BREAK",
"when": "WHEN", # A condition in a case expression.
"until": "UNTIL", # Creates a loop that executes until the condition is true
# Boolean and Nil Literals
"true": "TRUE", # Boolean true.
"false": "FALSE", # Boolean false.
"nil": "NIL", # A false value usually indicating “no value” or “unknown”.
# Boolean opoerators
"or": "OR", # Boolean or with lower precedence than ||
"and": "AND", # Short-circuit Boolean and with lower precedence than &&
"not": "NOT", # Inverts the following boolean expression. Has a lower precedence than !
# Modules and classes syntax
"class": "CLASS", # Creates or opens a class.
# Methods
"super": "SUPER", # Calls the current method in a superclass.
"self": "SELF", # The object the current method is attached to.
"new": "NEW",
# Method syntax
"def": "DEF", # Defines a method.
"return": "RETURN", # Exits a method.
# Exception handling
"retry": "RETRY", # Retries an exception block.
"ensure": "ENSURE", # Starts a section of code that is always run when an exception is raised.
# Data structure
"Set": "SET", # Set implements a collection of unordered values with no duplicates.
"Hash": "HASH", # A Hash maps each of its unique keys to a specific value.
"Matrix": "MATRIX", # The Matrix class represents a mathematical matrix.
# Line IO
"puts": "PUTS", # Writes objects to the stream.
"print": "PRINT", # Writes to the stream.
"gets": "GETS", # Returns the next line from the current file in ARGF.
}
# ----------------------------------
#
# List of token names
#
# ----------------------------------
tokens = (
# Comparison Operators
"EQUAL", # ==
"NOT_EQ", # !=
"GT_OR_EQ", # >=
"LT_OR_EQ", # <=
# Assignment Operators
"ADD_ASSIGN", # +=
"SUBS_ASSIGN", # -=
"MULT_ASSIGN", # *=
"DIV_ASSIGN", # /=
"MOD_ASSIGN", # %=
"RW_DOUBLE_ARROW", # =>
"RW_ARROW", # ->
"ASSIGN", # =
# Logical Operators
"LOGIC_NOT", # !
"LOGIC_AND", # &&
"LOGIC_OR", # ||
# Arithmetic Operators
"PLUS", # +
"MINUS", # -
"EXPO", # **
"MULT", # *
"DIV", # /
"GREATER_THAN", # >
"LESS_THAN", # <
"MODULO", # %
# Other Operators
"ELLIPSIS", # ...
"DOUBLE_PERIOD", # ..
"PERIOD", # .
"AMPERSAND", # &
"AT_SIGN", # @
"DOLLAR_SIGN",# $
"UNDERSCORE", # _
"VERTICAL_BAR", # |
"TILDE", # ~
# Separators
"SEMICOLON", # ;
"COLON", # :
"COMMA", # ,
# Delimiters
"L_PAREN", # (
"R_PAREN", # )
"L_BRACE", # {
"R_BRACE", # }
"L_BRACKET", # [
"R_BRACKET", # ]
# Literals
"COMPLEX", # 1+1i
"RATIONAL", # 1/2r
"FLOAT", # 0.5
"INTEGER", # 10
"STRING", # "Hi" 'Hi'
# Identifier
"ID", # x, color, UP
# Comment
"LINE_COMMENT", # #...
) + tuple(reserved.values())
# ----------------------------------
#
# Regex rules for tokens
#
# ----------------------------------
# Comparison Operators
t_EQUAL = r"=="
t_NOT_EQ = r"\!="
t_GT_OR_EQ = r">="
t_LT_OR_EQ = r"<="
# Assignment Operators
t_ADD_ASSIGN = r"\+="
t_SUBS_ASSIGN = r"-="
t_MULT_ASSIGN = r"\*="
t_DIV_ASSIGN = r"/="
t_MOD_ASSIGN = r"%="
t_RW_DOUBLE_ARROW = r"=>"
t_RW_ARROW = r"->"
t_ASSIGN = r"="
# Logical Operators
t_LOGIC_NOT = r"\!"
t_LOGIC_AND = r"&&"
t_LOGIC_OR = r"\|\|"
# Arithmetic Operators
t_PLUS = r"\+"
t_MINUS = r"-"
t_EXPO = r"\*\*"
t_MULT = r"\*"
t_DIV = r"/"
t_GREATER_THAN = r">"
t_LESS_THAN = r"<"
t_MODULO = r"%"
# Other Operators
t_ELLIPSIS = r"\.\.\."
t_DOUBLE_PERIOD = r"\.\."
t_PERIOD = r"\."
t_AMPERSAND = r"&"
t_AT_SIGN = r"@"
t_DOLLAR_SIGN = r"\$"
t_UNDERSCORE = r"_"
t_VERTICAL_BAR = r"\|"
t_TILDE = r"~"
# Separators
t_SEMICOLON = r";"
t_COLON = r":"
t_COMMA = r","
# Delimiters
t_L_PAREN = r"\("
t_R_PAREN = r"\)"
t_L_BRACE = r"{"
t_R_BRACE = r"}"
t_L_BRACKET = r"\["
t_R_BRACKET = r"\]"
# Literals
t_FLOAT = r"[0-9]*\.[0-9]+"
t_INTEGER = r"[0-9]+"
t_STRING = r"(\'[^\']*\'|\"[^\"]*\")"
t_ignore = ' \t'
# ----------------------------------
#
# Regex rules with action code
#
# ----------------------------------
def t_NEW_LINE(self, t):
r"\n+"
t.lexer.lineno += len(t.value)
def t_LINE_COMMENT(self, t):
r"\#.*"
pass
def t_ID(self, t):
r"[a-zA-Z_]+\w*"
t.type = self.reserved.get(t.value, "ID")
return t
def t_error(self, t):
print(f"Illegal character {t.value[0]}")
t.lexer.skip(1)
def __init__(self, **kwargs):
self.lexer = lex.lex(module=self, **kwargs, debug=1)
def test(self, data):
tokens = ""
self.lexer.input(data)
for tok in self.lexer:
tokens += f"{' '*(18 - len(tok.type))}{tok.type}{' '*(18 - len(tok.type))}{' '*(17 - len(tok.value))}{tok.value}{' '*(17 - len(tok.value))}{' '*(13 - len(str(tok.lineno)))}{tok.lineno}{' '*(13 - len(str(tok.lineno)))}{' '*(10 - len(str(tok.lexpos)))}{tok.lexpos}{' '*(10 - len(str(tok.lexpos)))}\n"
return tokens
def get_title():
return f"""
Lexical Analysis
| Token type | Token value | Line number | Position |
{('-') * 108}
"""
# ----------------------------------
#
# Build the lexer
#
# ----------------------------------
def lex_data(data):
lexer = MyLexer()
return get_title() + f"{lexer.test(data)}"
def lex_file(file_path):
lexer = MyLexer()
printing_data = get_title()
with open(file_path, mode="r", encoding="utf8") as data:
data_lines = data.readlines()
for line in data_lines:
printing_data += f"{lexer.test(line)}"
return printing_data