compilation_course_compiler/lexer.py

48 lines
1.1 KiB
Python

import sly
class Lexer(sly.Lexer):
# define token types
tokens = {
# Keyworkds
BREAK, CASE, DEFAULT, ELSE, FLOAT,
IF, INPUT, INT, OUTPUT, SWITCH, WHILE,
# Operators
RELOP, ADDOP, MULOP, OR, AND, NOT, CAST,
# misc
ID, NUM
}
ignore = ' \t'
literals = { '(', ')', '{', '}', ',', ':', ';', '=' }
# define each token
RELOP = r'(==|!=|<=|>=|>|<)'
ADDOP = r'(\+|-)'
MULOP = r'(\*|/)'
OR = r'\|\|'
AND = r'&&'
NOT = r'!'
CAST = r'static_cast<(int|float)>'
ID = r'[a-zA-Z][a-zA-Z0-9]*'
NUM = r'[0-9]+(\.[0-9]*)?'
# define keywords on ID
ID['break'] = BREAK
ID['case'] = CASE
ID['default'] = DEFAULT
ID['else'] = ELSE
ID['float'] = FLOAT
ID['if'] = IF
ID['input'] = INPUT
ID['int'] = INT
ID['output'] = OUTPUT
ID['switch'] = SWITCH
ID['while'] = WHILE
@_(r'\n+')
def newline(self, t):
self.lineno += t.value.count('\n')
def error(self, t):
print(f"Error at line {self.lineno}: Unexpected character {t.value[0]}")
self.index += 1