-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlexer.py
More file actions
91 lines (80 loc) · 1.41 KB
/
lexer.py
File metadata and controls
91 lines (80 loc) · 1.41 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
88
89
90
91
import ply.lex as lex
from parser import *
from ply.lex import TOKEN
# Lista tokenów
tokens = (
'NUMBER',
'PLUS',
'MINUS',
'MULTIPLY',
'DIVIDE',
'POWER',
'MODULO',
'PRINT',
'SEMICOLON',
'EQUAL_EQUAL',
'NOT_EQUAL',
'LESS',
'GREATER',
'LESS_EQUAL',
'GREATER_EQUAL',
'EQUAL',
'PLUS_EQUAL',
'MINUS_EQUAL',
'MULTIPLY_EQUAL',
'DIVIDE_EQUAL',
'ASSIGN',
'ID',
'MINUS_MINUS',
'PLUS_PLUS',
'IF',
'ELSE',
'LBRACE',
'RBRACE'
)
# Reguły tokenów
t_ASSIGN = r'='
t_PLUS = r'\+'
t_MINUS = r'-'
t_MULTIPLY = r'\*'
t_DIVIDE = r'/'
t_POWER = r'\^'
t_MODULO = r'%'
t_PRINT = r'print'
t_SEMICOLON = r';'
t_EQUAL_EQUAL = r'=='
t_NOT_EQUAL = r'!='
t_LESS = r'<'
t_GREATER = r'>'
t_LESS_EQUAL = r'<='
t_GREATER_EQUAL = r'>='
t_EQUAL = r'='
t_PLUS_EQUAL = r'\+='
t_MINUS_EQUAL = r'-='
t_MULTIPLY_EQUAL = r'\*='
t_DIVIDE_EQUAL = r'/='
t_MINUS_MINUS = r'\-\-'
t_PLUS_PLUS = r'\+\+'
t_LBRACE = r'\{'
t_RBRACE = r'\}'
def t_ID(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
t.type = reserved.get(t.value, 'ID')
return t
reserved = {
'print': 'PRINT'
}
# Ignorowane znaki (spacje)
t_ignore = ' \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)
# Obsługa błędów
def t_error(t):
print(f"Nieznany symbol: {t.value[0]}")
t.lexer.skip(1)
lexer = lex.lex()