-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
63 lines (63 loc) · 1.44 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
#LEXER 2.21
program = raw_input('TOKENIZE:\n')
lst = program.split()
tokens = []
op = {
'~': 'TILDE',
'`': 'GRAVE',
'!': 'EXCL',
'@': 'ATSIGN',
'#': 'HASH',
'$': 'DOLLAR',
'%': 'PERCENT',
'^': 'CARET',
'&': 'ANDSIGN',
'*': 'STAR',
'(': 'LPAREN',
')': 'RPAREN',
'-': 'MINUS',
'+': 'PLUS`',
'=': 'EQUAL',
'{': 'LBRACE',
'}': 'RBRACE',
'[': 'LBRACK',
']': 'RBRACK',
'|': 'PIPE',
'\'': 'QUOTE',
'\"': 'DQUOTE',
':': 'COLON',
';': 'SEMIC',
'.': 'PERIOD',
',': 'COMMA',
'\\': 'BSLASH',
'<': 'LARROW',
'>': 'RARROW',
'?': 'QUESTION',
'/': 'SLASH',
}
for i in lst:
if len(i) > 1:
if i.startswith('raw_input(\''):
tokens.append('CMD(INPUT)')
tokens.append('STR(%s)' % i[11:-2])
elif i.startswith('raw_input('):
tokens.append('CMD(INPUT)')
tokens.append('ID(%s)' % i[10:-1])
elif i.startswith('\''):
tokens.append('STR(%s)' % i[1:-1])
elif i == 'print':
tokens.append('CMD(PRINT)')
elif i == '\n':
tokens.append('NEWLINE')
else:
tokens.append('ID({})'.format(i))
elif i.isdigit():
tokens.append('NUM({})'.format(i))
elif i.isalpha():
tokens.append('CHAR({})'.format(i))
elif i in op:
tokens.append(op[i])
else:
tokens = ['Error']
break
print ' '.join(tokens)