Skip to content

Commit 6112d9b

Browse files
authored
class BasicLexer
Thus we will need some basic tokens such as NAME, NUMBER, STRING. In any programming language, there will be space between two characters. Thus we create an ignore literal. Then we also create the basic literals like ‘=’, ‘+’ etc., NAME tokens are basically names of variables, which can be defined by the regular expression [a-zA-Z_][a-zA-Z0-9_]*. STRING tokens are string values and are bounded by quotation marks(” “). This can be defined by the regular expression \”.*?\”
1 parent f581e7e commit 6112d9b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
tokens = { NAME, NUMBER, STRING }
2+
ignore = '\t '
3+
literals = { '=', '+', '-', '/',
4+
'*', '(', ')', ',', ';'}
5+
6+
7+
# Define tokens as regular expressions
8+
# (stored as raw strings)
9+
NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
10+
STRING = r'\".*?\"'
11+
12+
# Number token
13+
@_(r'\d+')
14+
def NUMBER(self, t):
15+
16+
# convert it into a python integer
17+
t.value = int(t.value) return t
18+
19+
# Comment token
20+
@_(r'//.*')
21+
def COMMENT(self, t):
22+
pass
23+
24+
# Newline token(used only for showing
25+
# errors in new line)
26+
@_(r'\n+')
27+
def newline(self, t):
28+
self.lineno = t.value.count('\n')

0 commit comments

Comments
 (0)