[SOLVED] COMP340 - HW 6

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 parserr.py (and modify other files if necessary) so that our parser builds a tree from the below three cases.

  1. Grouped Expression (ex. 1 * (2 + 5))
  2. Single Number Expression (ex. 25)
  3. Expression with Negative Sign (ex. -25 * 3)

Test Cases

This section provides several test cases to check whether you wrote a parser correctly. In the main.py, you will change srcCode with the below test cases and see whether your output results are matched with the ones given in the table.

import lexer import parserr

 

srcCode = “1 * (2 + 5)” tokSeq = lexer.tokenize(srcCode) rootNode = parserr.parse(tokSeq) parserr.printTree(rootNode) print()

main.py

 

Test Case (srcCode) Output Result
1 * ( 2 + 5 ) (1 * ( 2 + 5 ))
(1 + 2) * 5 + 4 (((1 + 2) * 5) + 4)
23 * ((1 + 5) * 33) (23 * ((1 + 5) * 33))
24 24
125 125

Page 1 of 2

 

-5 ((0 – 1) * 5)
– -5 (((0-1)*(0-1))*5)
– (-5) ((0-1)*((0-1)*5))

 

 

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.