[SOLVED] COMP340 -  HW 5 - Lexer

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

5/5 - (1 vote)

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 )

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.