-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation_lex.py
More file actions
87 lines (68 loc) · 1.42 KB
/
validation_lex.py
File metadata and controls
87 lines (68 loc) · 1.42 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
import ply.lex as lex
# List of tokens
tokens = [
'NUMBER',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'LPAREN',
'RPAREN',
'ID',
'EQUAL',
'DOLLAR',
'POINT',
'SEMICOLON',
'STRING',
'LCURLY',
'RCURLY',
'CONDITIONAL',
'INCDEC',
'SINGLEQUOTES',
'DOUBLEQUOTES',
]
# Dictionary containing reserved values
reserved = {
'if' : 'IF',
'else' : 'ELSE',
'while' : 'WHILE',
'for' : 'FOR',
'my' : 'my',
}
# Defining regular expressions for the tokens present in the list above
t_EQUAL = r'\='
t_DOLLAR = r'\$'
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_SEMICOLON = r';'
t_POINT = r'\.'
t_LCURLY = r'\{'
t_RCURLY = r'\}'
t_STRING = r'\"[a-zA-Z_0-9~!@#$%^&*()_+=-|?{}\s]*"' or r"\'[a-zA-Z_0-9~!@#$%^&*()_+=-|?{}\s]*'"
t_CONDITIONAL = r'>=|<=|<|>|==|!='
t_INCDEC = r'\++|--'
t_SINGLEQUOTES = r"\'"
t_DOUBLEQUOTES = r'\"'
# Appending the values in the "reserved" dictionary to the list of tokens
tokens = tokens + list(reserved.values())
# Defining regular expressions for the tokens
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_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value, 'ID')
return t
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex()