/
lexical.py
175 lines (138 loc) · 3.19 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
"""
MUZL PROJECT PART 2
MORGAN PETERS AND SEAN GRAY
2/9/2017
"""
import ply.lex as lex
import sys
states = (
('rblock','exclusive'),
)
# reserved words...
reserved = {
'if': 'IF',
'rule': 'RULE',
# the _T notation tells us it is a type declaration var not the value type
'int': 'INT_T',
'float': 'FLOAT_T',
'string': 'STRING_T',
'char': 'CHAR_T',
'bool': 'BOOL_T',
'hex': 'HEX_T'
}
# Token list
tokens_list = [
'PLUS', 'MINUS', 'TIMES', 'DIVIDE', 'POW',
'LTHAN', # LESSTHAN <
'LETHAN', # LESSTHANOREQUALTO <=
'GTHAN', # GREATERTHAN >
'GETHAN', # GREATERTHANOREQUALTO >=
'ETO', # EQUALTO ==
'RBLOCK',
'ARROW',
'LPAREN', 'RPAREN', 'COLON','DOT',
'ID', # to match all words including reserved
]
# separated for easier reading, list of types with variable values
data_types_list = [
'INT',
'FLOAT',
'CHAR',
'STRING',
'BOOL',
'HEX'
]
# need to put all lists before reserved for some reason
tokens = tokens_list + data_types_list + list(reserved.values())
# regular expressions
t_LETHAN = r'\<='
t_LTHAN = r'\<'
t_GETHAN = r'\>='
t_GTHAN = r'\>'
t_PLUS = r'\+'
t_ARROW = r'->' # longer expressions must go first
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_POW = r'\^'
t_ETO = r'=='
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_COLON = r':'
def t_rblock(t):
r'='
t.lexer.code_start = t.lexer.lexpos
t.lexer.begin('rblock')
def t_rblock_EOL(t):
r'!'
t.value = t.lexer.lexdata[t.lexer.code_start:t.lexer.lexpos-1]
t.type = "RBLOCK"
t.lexer.begin('INITIAL')
return t
def t_rblock_STRING(t):
r'.^!+'
print("inString")
# Ignored characters (whitespace)
t_rblock_ignore = " \t\n"
def t_rblock_error(t):
t.lexer.skip(1)
def t_STRING(t):
r'(").+(")'
t.value = t.value[1:-1] # strips away the quotes
return t
# action code of regular expressions
# hex needs to be before int
def t_HEX(t):
r'0x(\d|[a-f]|[A-F])+'
t.value = t.value
return t
def t_FLOAT(t):
r'\d+\.\d+'
t.value = float(t.value)
return t
def t_INT(t):
r'\d+'
t.value = int(t.value)
return t
def t_CHAR(t):
r'(\')\S(\')'
t.value = t.value[1:-1] # strips away the quotes
return t
def t_BOOL(t):
r'(True|False)'
if t.value == "True":
t.value = True
else:
t.value = False
return t
def t_COMMENT(t):
r'\#.+'
pass
# end of line; syntax the same as the example
'''def t_EOL(t):
r'!+'
t.lexer.lineno += len(t.value)
t.value = 'EOL'''
def t_RESERVED(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value, 'ID') # Check for reserved words
return t
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t \n'
t_DOT = r'\.'
# literals? like for [], ! ect.. do we need these?
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# precedence (i think this is the second part of the project?)
# build it
def getLexer():
return lex.lex()
if __name__ == '__main__':
lexer = getLexer()
# argv[1] is the first command line arg
lexer.input(sys.argv[1])
for tok in lexer:
print(tok)
lexer = lex.lex()