Skip to content

Instantly share code, notes, and snippets.

@hassaku63
Created November 6, 2022 16:20
Show Gist options
  • Save hassaku63/5899c6aab279ed1ed62d4ee149ae6b3d to your computer and use it in GitHub Desktop.
Save hassaku63/5899c6aab279ed1ed62d4ee149ae6b3d to your computer and use it in GitHub Desktop.
Python で S式(整数の四則演算だけ)を字句解析する
from enum import Enum
from functools import wraps
def assert_simgle_char(f):
@wraps(f)
def inner(c: str):
if len(c) != 1:
raise ValueError(c)
return f(c)
return inner
@assert_simgle_char
def is_space(c):
return c in '\s\n\t\r '
@assert_simgle_char
def is_numeric(c):
return c in '0123456789'
def tokenize(code: str) -> list[str]:
tokens = []
i = 0
while (True):
c = code[i]
if c in '()':
tokens.append(c)
i += 1
elif c in '+-*/':
tokens.append(c)
i += 1
elif is_numeric(c):
buf = ''
while (True):
if is_numeric(code[i]):
buf += code[i]
i += 1
continue
tokens.append(buf)
break
elif is_space(c):
i += 1
if len(code) <= i:
break
return tokens
if __name__ == '__main__':
ret = tokenize('(+ 1 23)')
print(ret)
ret = tokenize('(* (+ 100 234) 987))')
print(ret)
@hassaku63
Copy link
Author

$ python -m tokenizer
['(', '+', '1', '23', ')']
['(', '*', '(', '+', '100', '234', ')', '987', ')', ')']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment