Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python programming; Please help!!! We want to build a tokenizer for simple expre

ID: 3712293 • Letter: P

Question

Python programming; Please help!!!

We want to build a tokenizer for simple expressions such as xpr = "res = 3 + x_sum*11". Such expressions comprise only three tokens, as follows: (1) Integer literals: one or more digits e.g., 3 11; (2) Identifiers: strings starting with a letter or an underscore and followed by more letters, digits, or underscores e.g., res x_sum; (3) Operators: = + * . Leading or trailing whitespace characters should be skipped.

(c) To find which token each lexeme is associated with, we only need to find the first non-empty item in each tuple. Write a tokenize generator (using re.findall and map) that returns all pairs (tuples) of lexemes and tokens. The output of list(tokenize(xpr)) should thus be:

[('res', 'id'), ('=', 'op'), ('3', 'int'), ('+', 'op'), ('x_sum', 'id'), ('*', 'op'), ('11', 'int')]

Explanation / Answer

tokenize.tokenize:

tokenize module provides a lexical scanner for Python. The scanner in this module returns comments as tokens as well, making it useful for implementing “pretty-printers,” including colorizers for on-screen displays.

To simplify token stream handling, all operator and delimiter tokens and Ellipsis are returned using the generic OP token type. The exact type can be determined by checking the exact_type property on the named tuple returned from tokenize.tokenize().

The tokenize() generator requires one argument, readline, which must be a callable object which provides the same interface as the io.IOBase.readline() method of file objects. Each call to the function should return one line of input as bytes.

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed (the last tuple item) is the logical line; continuation lines are included. The 5 tuple is returned as a named tuple with the field names: type string start end line.

The returned named tuple has an additional property named exact_type that contains the exact operator type for token.OP tokens. For all other token types exact_type equals the named tuple type field.

import tokenize
import token
import io
import collections

class Token(collections.namedtuple('Token', 'num val start end line')):

@property
def name(self):
return token.tok_name[self.num]

def __repr__(self):
return '{} {!r} ({} {}): {!r}'.format(self.name, self.val,
self.start, self.end, self.line)

line = b'res = 3 + x_sum*11'
result = [Token(*item) for item in tokenize.tokenize(io.BytesIO(line).readline)]
result = [(tok.val, tok.name) for tok in result[1:-1]]
print(result)

which yields to

[('res', 'NAME'), ('=', 'OP'), ('3', 'NUMBER'), ('+', 'OP'), ('x_sum', 'NAME'), ('*', 'OP'), ('11', 'NUMBER')]

Modify the Token.name property to match your specification exactly, since the token names as shown above are already defined in the token module.