COMP340 Homework 5- Lexer Solved

35.00 $

Category:

Description

Rate this product

 

Homework Description

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.

COMP 340 – HW 5 Due date: 04/20/2020 11:59 pm Total Points: 20 pts

Token Type

NUMBER
PLUS
MINUS MULTIPLICATION DIVISION LPAREN

RPAREN

Token Value

0,1,2,3,….,9 +

*

/ ( )

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

Page 1 of 3

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 )

 

  • hw_5-hr9axp.zip