Starting from:

$25

COMP340 -  HW 5 - Solved

Lexer
*This homework is a programming assignment.  



*While working on the assignment, read this document THOROUGHLY.  

 

Homework 

In this homework, you need to complete lexer.py that we’ve worked on during the class. In our interpreter, a lexer needs to handle seven tokens as shown below.  

Token Type 
Token Value 
NUMBER 
0,1,2,3,….,9 
PLUS 

MINUS 

MULTIPLICATION 

DIVISION 

LPAREN 

RPAREN 

 

Currently, our lexer produces NUMBER token and PLUS token. Complete lexer.py so that our lexer also produces other tokens (MINUS, MULTIPLICATION, DIVISION, LPAREN, RPAREN).  

 

Assume that we run main.py with srcCode=“((12+3*5)+5/4)” with the completed lexer.py like the below.  

 

import lexer 

 srcCode = "((12+3*5)+5/4)" tokSeq = lexer.tokenize(srcCode) 

 for i in tokSeq: 

    print(i.type, i.value) 

 
main.py 

 

Then, it should print a sequence of tokens: 

 

LPAREN ( 

LPAREN ( 

NUMBER 12 

PLUS + 

NUMBER 3 

MULTIPLICATION * 

NUMBER 5 

RPAREN ) 

PLUS + 

NUMBER 5 

DIVISION / 

NUMBER 4 

RPAREN ) 

More products