-
Notifications
You must be signed in to change notification settings - Fork 0
/
alice_lex.py
127 lines (116 loc) · 2.46 KB
/
alice_lex.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
import ply.lex as lex
reserved_words = {
'begin' : 'BEGIN',
'main' : 'MAIN',
'let' : 'LET',
'input' : 'INPUT',
'print' : 'PRINT',
'if' : 'IF',
'then' : 'THEN',
'else' : 'ELSE',
'do' : 'DO',
'while' : 'WHILE',
'for' : 'FOR',
'module' : 'MODULE',
'return' : 'RETURN',
'endprog' : 'ENDPROG',
'end' : 'END',
'and' : 'AND',
'or' : 'OR',
'int' : 'INT',
'float' : 'FLOAT',
'string' : 'STRING',
'void' : 'VOID',
'size' : 'SIZE',
'mean' : 'MEAN',
'median' : 'MEDIAN',
'mode' : 'MODE',
'variance' : 'VARIANCE',
'std' : 'STD',
'range' : 'RANGE',
'sum' : 'SUM',
'min' : 'MIN',
'max' : 'MAX',
'histogram' : 'HIST',
'violin' : 'VIOLIN',
'box' : 'BOXPLOT',
'bar' : 'BAR',
'scatter' : 'SCATTER',
'mirror' : 'MIRROR'
}
tokens = [
'ID',
'CTE_I',
'CTE_F',
'CTE_STRING',
'EXPONENT',
'ADD',
'DECREASE',
'PLUS',
'MINUS',
'MULTIPLY',
'DIVIDE',
'INT_DIV',
'LT',
'LE',
'GT',
'GE',
'EQ',
'NE',
'ASSIGN',
'COLON',
'SEMICOLON',
'TYPE_ASSIGN',
'COMA',
'LPAREN',
'RPAREN',
'L_SBRKT',
'R_SBRKT'
] + list(reserved_words.values())
t_EXPONENT = r'\^'
t_ADD = r'\+\+'
t_DECREASE = r'--'
t_PLUS = r'\+'
t_MINUS = r'-'
t_MULTIPLY = r'\*'
t_INT_DIV = r'//'
t_DIVIDE = r'/'
t_ASSIGN = r'\<-'
t_LE = r'\<='
t_LT = r'\<'
t_GE = r'>='
t_GT = r'>'
t_EQ = r'\=\='
t_NE = r'¬\='
t_TYPE_ASSIGN = r'\:\:'
t_COLON = r'\:'
t_SEMICOLON = r'\;'
t_COMA = r'\,'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_L_SBRKT = r'\['
t_R_SBRKT = r'\]'
t_ignore = ' \t\n\r\f'
def t_ID(t):
r'[a-zA-Z_]\w*'
if t.value in reserved_words.keys():
t.type = reserved_words[t.value]
else:
t.type = 'ID'
return t
def t_CTE_F(t):
r'\d+\.(\d*)?(e(\+|-)?\d+)?'
t.value = float(t.value)
return t
def t_CTE_I(t):
r'\d+'
t.value = int(t.value)
return t
def t_CTE_STRING(t):
r'"([^\\"\n]+|\\.)*"'
t.type = 'CTE_STRING'
return t
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex()