-
Notifications
You must be signed in to change notification settings - Fork 4
/
ExpressionLanguageLex.py
56 lines (46 loc) · 1.05 KB
/
ExpressionLanguageLex.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
# -------------------------
# ExpressionLanguageLex.py
#----------------------
import ply.lex as lex
reservadas = {
'while' : 'WHILE',
'true' : 'TRUE',
'false' : 'FALSE',
'return' : 'RETURN'
}
tokens = ['COMMA', 'SOMA', 'ID', 'NUMBER', 'VEZES', 'POT', 'LPAREN',
'RPAREN', 'IGUAL', 'LCHAV', 'RCHAV', 'PV'] + list(reservadas.values())
t_IGUAL= r'='
t_SOMA = r'\+'
t_VEZES = r'\*'
t_POT = r'\^'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_COMMA = r','
t_LCHAV = r'{'
t_RCHAV = r'}'
t_PV = r';'
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reservadas.get(t.value,'ID')
return t
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
t_ignore = ' \t'
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
def main():
f = open("input1.su", "r")
lexer = lex.lex(debug=1)
lexer.input(f.read())
print('\n\n# lexer output:')
for tok in lexer:
print ('type:', tok.type, ', value:',tok.value)
if __name__ == "__main__":
main()