-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.py
More file actions
188 lines (145 loc) · 3.86 KB
/
Lexer.py
File metadata and controls
188 lines (145 loc) · 3.86 KB
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
import ply.lex as lex
# -------------------------------------------------------------
# ------------- Lexical Analyzer ------------------------------
# -------------------------------------------------------------
# -------------------------------------------------------------
# Literals
# -------------------------------------------------------------
binaryOperators = [
'+', # Plus
'-', # Minus
'*', # Times
'/', # Divde
]
literals = binaryOperators + [
':', # Colon
',', # Coma
';', # Semicolon
'(', ')', # Parentheses
'[', ']', # Square Brackets
'{', '}', # Curly Brackets
]
# -------------------------------------------------------------
# Reserved Tokens
# -------------------------------------------------------------
specialFunctons = {
'eye': 'EYE',
'zeros': 'ZEROS',
'ones': 'ONES',
'print': 'PRINT'
}
typicalLanguageKeywords = {
'if': 'IF',
'else': 'ELSE',
'for': 'FOR',
'while': 'WHILE',
'break': 'BREAK',
'continue': 'CONTINUE',
'return': 'RETURN',
}
reserved = {}
reserved.update(specialFunctons)
reserved.update(typicalLanguageKeywords)
# -------------------------------------------------------------
# Tokens
# -------------------------------------------------------------
matrixBinaryOperations = [
'DOTADD', # .+
'DOTSUB', # .-
'DOTMUL', # .*
'DOTDIV' # ./
]
matrixOperations = [
'TRANSPOSE', # '
]
assignmentOperations = [
'ASSIGN', # =
'SUBASSIGN', # -=
'ADDASSIGN', # +=
'DIVASSIGN', # /=
'MULTASSIGN' # \ *=
]
relationalOperators = [
'LT', # <
'GT', # >
'LTE', # <=
'GTE', # >=
'EQL', # ==
'NEQ', # \ !=
]
primitives = [
'INTNUM', # int
'FLOATNUM', # float
'STR'
]
identifier = ['ID']
# List of token names. This is always required
tokens = \
matrixBinaryOperations +\
matrixOperations +\
assignmentOperations +\
relationalOperators +\
primitives +\
identifier +\
list(reserved.values())
# -------------------------------------------------------------
# Matching Rules
# -------------------------------------------------------------
# Note:
# - maximal is match is chosen (pattern which consumes the most characters)
# - patterns appearing closer take precedence:
# - functions defined higher in this file,
# - strings are sorted based on pattern length.
t_DOTADD = r'\.\+' # .+
t_DOTSUB = r'\.-' # .-
t_DOTMUL = r'\.\*' # .*
t_DOTDIV = r'\./' # ./
t_TRANSPOSE = r'\''
t_ASSIGN = r'='
t_SUBASSIGN = r'-='
t_ADDASSIGN = r'\+='
t_DIVASSIGN = r'/='
t_MULTASSIGN = r'\*='
t_LT = r'<'
t_GT = r'>'
t_LTE = r'<='
t_GTE = r'>='
t_EQL = r'=='
t_NEQ = r'!='
def t_ID(t):
# I reject id containg only __
r'(([a-zA-Z])|(_+[a-zA-Z0-9]+))[a-zA-Z_0-9]*'
t.type = reserved.get(t.value, 'ID')
return t
def t_STR(t):
# We allow for escaping (") within a string using (\")
# Multiline strings are disallowed.
r'"((\\")|[^"\n])*"'
t.value = str(t.value[1:-1]) \
.replace(r'\"', "\"")
return t
def t_FLOATNUM(t):
# It also allows for leading zeros and for scientific notation.
r'(\d+(\.\d*)|\.\d+)([eE][+-]?\d+)?'
t.value = float(t.value)
return t
def t_INTNUM(t):
r'(\d+)' # It allows for leading zeros.
t.value = int(t.value)
return t
def t_newline(t):
# Define a rule so we can track line numbers
r'\n+'
t.lexer.lineno += len(t.value)
def t_COMMENT(t):
# This one needs to be placed at the bottom so that it does not interfere with others!
r'\#.*'
pass
def t_error(t):
# We could maybe combine errors into one msg instead of reporting single characters.
t.lexer.skip(1)
return t
def t_eof(t):
return None
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'