-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.py
130 lines (117 loc) · 2.3 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
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
128
129
130
import ply.lex as lex
# reserved words
reserved = {
'if' : 'IF',
'var': 'VAR',
'else' : 'ELSE',
'program' : 'PROGRAM',
'int' : 'INT',
'float': 'FLOAT',
'objects': 'OBJECTS',
'object': 'OBJECT',
'string': 'STRING',
'char': 'CHAR',
'main': 'MAIN',
'void': 'VOID',
'inherits': 'INHERITS',
'return': 'RETURN',
'write': 'WRITE',
'read': 'READ',
'while': 'WHILE',
'for': 'FOR',
'open': 'OPEN',
'close': 'CLOSE',
'file': 'FILE',
'to': 'TO',
'function': 'FUNCTION',
'functions': 'FUNCTIONS',
'do': 'DO',
'func': 'FUNC',
}
# List of token names. This is always required
tokens = [
'ID',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'GREATER',
'GREATEROREQUAL',
'LESSOREQUAL',
'LESS',
'NOTEQUAL',
'LBRACKET',
'RBRACKET',
'LSQRBRACKET',
'RSQRBRACKET',
'LPAREN',
'RPAREN',
'SEMICOLON',
'COLON',
'EQUALS',
'EQUAL',
'COMMA',
'AND',
'OR',
'DOT',
'CTEI',
'CTEF',
'CTESTRING'
] + list(reserved.values())
# Regular expression rules for simple tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'\/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_GREATER = r'>'
t_GREATEROREQUAL = r'>='
t_LESS = r'<'
t_LESSOREQUAL = r'<='
t_LBRACKET = r'\{'
t_RBRACKET = r'\}'
t_LSQRBRACKET = r'\['
t_RSQRBRACKET = r'\]'
t_SEMICOLON = r'\;'
t_COLON = r'\:'
t_EQUALS = r'\='
t_COMMA = r'\,'
t_CTESTRING = r'\".*\"'
t_EQUAL = r'\=='
t_NOTEQUAL = r'\!='
t_AND = r'\&&'
t_OR = r'\|\|'
t_DOT = r'\.'
# REGEX
#Define an ID
def t_ID(t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
t.type = reserved.get(t.value, 'ID')
return t
# Define a constant float
def t_CTEF(t):
r'[-+]?[0-9]+\.[0-9]+'
t.value = float(t.value)
return t
# Define a constant int
def t_CTEI(t):
r'[-+]?[0-9]+'
t.value = int(t.value)
return t
# # Define a constant string
# def t_CTESTRING(t):
# r'\"([^\\\"]|\\.)*\"'
# return t
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# A string containing ignored characters (spaces and tabs)
t_ignore = ' \t'
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()