# Stephanie Taylor # CS333 # Spring 2026 # Tokenizer for simple examples in lecture # T -> 0 | ... | 9 # "+" { echo (); return new Token (TokenClass . PLUS); } # "-" { echo (); return new Token (TokenClass . MINUS); } # The value field will always be a string. This makes it easier for the parsing.s class Token: def __init__( self, value ): self.value = value def __str__( self ): return str(self.value) class Tokenizer: def __init__( self, filename=None, lines=None ): """ If a filename is given, open it and store the lines as a list of strings. else if lines is not None, store the lines else don't really do anything (not useful) """ if filename is not None: fobj = open( filename ) self.lines = fobj.readlines() fobj.close() elif lines is not None: self.lines = lines self.cur_line = 0 self.cur_index = 0 # Increment the line and column indices until they indicate the # beginning of a token. def passWhitespace(self): if self.cur_line == len(self.lines): return if self.cur_index == len(self.lines[self.cur_line]): self.cur_line += 1 self.cur_index = 0 self.passWhitespace() return foundIt = False # search this line for a non-whitespace character for self.cur_index in range(self.cur_index,len(self.lines[self.cur_line])): if self.lines[self.cur_line][self.cur_index] not in " \t\n\r": foundIt = True break # if we are still in whitespace, go on to the next line if self.cur_index == len(self.lines[self.cur_line])-1 and not foundIt: self.cur_line += 1 self.cur_index = 0 self.passWhitespace() # If we find a comment, then we need to skip the rest of the line # and go on to the next. elif self.lines[self.cur_line][self.cur_index:self.cur_index+2] == '//': self.cur_line += 1 self.cur_index = 0 self.passWhitespace() def getToken(self): self.passWhitespace() if self.cur_line == len(self.lines): # We are at the end of the file. No more tokens return None cur_char = self.lines[self.cur_line][self.cur_index] if cur_char in "+-": self.cur_index += 1 return Token( cur_char ) elif cur_char in "0123456789": self.cur_index += 1 return Token( cur_char ) else: print( "Problem - can't identify", cur_char ) if __name__ == '__main__': t = Tokenizer( lines=['3 - 4+5 ']) while True: tok = t.getToken() if tok is None: break print( tok )