Hi all,
I have implemented a generator-based scanner in Python that tokenizes a string into tuples of the form (token type, token value):
for token in scan("a(b)"):
print token
would print
("literal", "a")
("l_paren", "(")
...
The next task implies parsing the token stream and for that, I need be able to look one item ahead from the current one without moving the pointer ahead as well. The fact that iterators and generators do not provide the complete sequence of items at once but each item as needed makes lookaheads a bit trickier compared to lists, since the next item is not known unless __next__() is called.
What could a straightforward implementation of a generator-based lookahead look like? Currently I'm using a workaround which implies making a list out of the generator:
token_list = [token for token in scan(string)]
The lookahead then is easily implemented by something like that:
try:
next_token = token_list[index + 1]
except: IndexError:
next_token = None
Of course this just works fine. But thinking that over, my second question arises: Is there really a point of making scan() a generator in the first place?
Thanks in advance!
Regards,
jena