-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlex.py
More file actions
55 lines (45 loc) · 1.08 KB
/
lex.py
File metadata and controls
55 lines (45 loc) · 1.08 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
import ply.lex as lex
tokens = [
'CREATE', 'TABLE', 'INSERT', 'INTO', 'VALUES',
'PRIMARY', 'KEY',
'INT', 'VARCHAR', 'DECIMAL', 'DATE',
'IDENTIFIER', 'NUMBER', 'STRING',
'LPAREN', 'RPAREN', 'COMMA', 'SEMICOLON'
]
reserved = {
'CREATE': 'CREATE',
'TABLE': 'TABLE',
'INSERT': 'INSERT',
'INTO': 'INTO',
'VALUES': 'VALUES',
'PRIMARY': 'PRIMARY',
'KEY': 'KEY',
'INT': 'INT',
'VARCHAR': 'VARCHAR',
'DECIMAL': 'DECIMAL',
'DATE': 'DATE'
}
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_COMMA = r','
t_SEMICOLON = r';'
t_ignore = ' \t'
def t_IDENTIFIER(t):
r'[A-Za-z_][A-Za-z0-9_]*'
t.type = reserved.get(t.value.upper(), 'IDENTIFIER')
return t
def t_NUMBER(t):
r'\d+(\.\d+)?'
t.value = float(t.value) if '.' in t.value else int(t.value)
return t
def t_STRING(t):
r'\'[^\']*\''
t.value = t.value[1:-1]
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
def t_error(t):
print(f"Lexer ERROR: Illegal character '{t.value[0]}' at line {t.lineno}")
t.lexer.skip(1)
lexi = lex.lex()