from pyparsing import class Lexer def __init__ self program star Regex

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from pyparsing import *
class Lexer:
def __init__(self, program):
star = Regex("\*").setResultsName('star')
oper_left = Regex("\(").setResultsName('oper_left')
oper_right = Regex("\)").setResultsName('oper_right')
text = Regex("\"[a-z*(+)]+\"").setResultsName('text')
identify = Regex("[A-Z]'|[A-Z]").setResultsName('ident')
lexems = OneOrMore(Group(identify) | Group(oper_left) | Group(oper_right) | Group(text) | Group(star)).parseString(program)
self.lexems = [lexem.items().pop() for lexem in lexems]
def next_token(self): return self.lexems.pop()
def get_lexems(self): return self.lexems