text = "hello+there$" # explicit end-of-text marker position = 0 lval = None def parseLetter(): # a | b | ... | y | z global text, position, lval if text[position] < "a" or text[position] > "z": return False lval = text[position] position += 1 return True def parseIdentifier(): # letter | letter identifier global text, position, lval start = position if not parseLetter(): return False parseIdentifier() lval = ["identifier", text[start:position]] return True def parseSum(): # sum + identifier | identifier = identifier ( + identifier )* global text, position, lval if not parseIdentifier(): return False saved = position result = lval while text[position] == "+": position += 1 if not parseIdentifier(): print("identifier expected at position " + str(position)) exit() result = ["add", result, lval] saved = position lval = result return True print(parseSum()) print(position) print(lval)