import ply.lex as lex
# List of token names
tokens = (
'NUMBER',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'IDENTIFIER',
'KEYWORD',
)
# Regular expression rules for simple tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
# Define a regular expression for identifying numbers
t_NUMBER = r'\d+'
# Define a regular expression for identifiers (names starting with a letter, followed by letters or digits)
t_IDENTIFIER = r'[a-zA-Z_][a-zA-Z0-9_]*'
# Define a rule for keywords
def t_KEYWORD(t):
r'\b(if|else|while|for)\b'
return t
# Define a rule for ignoring spaces and newlines
t_ignore = ' \t\n'
# Define a rule for handling errors
def t_error(t):
print(f"Illegal character '{t.value[0]}'")
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# Sample input string
data = '''
if x + 10 > 20:
y = x * 2
'''
# Input the string to the lexer
lexer.input(data)
# Tokenize the input
tok = lexer.token()
if not tok:
break
print(f'Token: {tok.type}, Value: {tok.value}')
aW1wb3J0IHBseS5sZXggYXMgbGV4CgojIExpc3Qgb2YgdG9rZW4gbmFtZXMKdG9rZW5zID0gKAogICAgJ05VTUJFUicsCiAgICAnUExVUycsCiAgICAnTUlOVVMnLAogICAgJ1RJTUVTJywKICAgICdESVZJREUnLAogICAgJ0lERU5USUZJRVInLAogICAgJ0tFWVdPUkQnLAopCgojIFJlZ3VsYXIgZXhwcmVzc2lvbiBydWxlcyBmb3Igc2ltcGxlIHRva2Vucwp0X1BMVVMgPSByJ1wrJwp0X01JTlVTID0gcictJwp0X1RJTUVTID0gcidcKicKdF9ESVZJREUgPSByJy8nCgojIERlZmluZSBhIHJlZ3VsYXIgZXhwcmVzc2lvbiBmb3IgaWRlbnRpZnlpbmcgbnVtYmVycwp0X05VTUJFUiA9IHInXGQrJwoKIyBEZWZpbmUgYSByZWd1bGFyIGV4cHJlc3Npb24gZm9yIGlkZW50aWZpZXJzIChuYW1lcyBzdGFydGluZyB3aXRoIGEgbGV0dGVyLCBmb2xsb3dlZ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