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 expres

ID: 3712291 • 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.

(a) Write a regular expression pattern for the above grammar and use it with re.findall to split the expression into a list of lexemes. If using xpr above, the list returned should be:

['res', '=', '3', '+', 'x_sum', '*', '11']

Explanation / Answer

Screenshot

---------------------------------------------------------------------------------------------------------------------------

Program

#import regular expression package to work findall()
import re
#ask user to enter an expression
xpr=input("Please enter a expression:")
#print list of all match the regular expression
print(re.findall("d{1,2}|[A-Za-z][A-Za-z0-9_]*|[= + *]",xpr))