Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1:16 Il LTE Homework 2 (Template at the bo... Motivation Idea ---> Editor ---> Source File -> Scanner --> Lexer -> Parser -> Interpreter -->

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
1:16 Il LTE Homework 2 (Template at the bo... Motivation Idea ---> Editor ---> Source File -> Scanner --> Lexer -> Parser -> Interpreter --> Intermediate Code Generator --> --> Code Generator ---> Executable det eval(exprn): tokens = exprn.split('') List of Tokens Pa rser+Interpreter-----> Integer tokenize() . Special instructions File name must be 'H02_work.py". Your name must appear at the top of the file. The class name must be 102 The signatures and names of the following MUST not b e changed: _init__(self) tokenize(self, expen) interpret(self, tokens) eval(self, exprn) DO NOT modify this Define utility functions inside Ho2 to help you. Use print() generously to help debugging, comment th em before submitting the file IMPORTANT: MUST NOT use any of the following: split() for splitting strings into chunks strip) for removing white spaces replace() for replacing characters int() for parsing an integer try-except construct any Python's built-in method of parsing and evalua external libraries Milestones 1:16 Il LTE X Homework 2 (Template at the bo... Assignment Create a class 102 that contains the functionality of Lexer Parser + Interpreter to process additions and subtractions of integers. Expression -----Lexer---> List of Tokens rser+Interpreter-----> Integer tokenize() - Pa 1 Special instructions File name must be 'H02_work.py'. Your name must appear at the top of the file. The class name must be 102 The signatures and names of the following Must not b e changed: _init__(self) tokenize(self, exprn) interpret (self, tokens) eval(self, exprn) ---- DO NOT modify this Define utility functions inside H02 to help you. Use print() generously to help debugging, comment th em before submitting the file IMPORTANT: MUST NOT use any of the following: split() for splitting strings into chunks strip) for removing white spaces replace() for replacing characters int() for parsing an integer try-except construct any Python's built-in method of parsing and evalua external libraries . Milestones Milestone 1: Edit the method tokenize() to tokenize an expression co nsisting of integers, plus and minus signs with white spaces. Thus tokenize() a containing digits, plus and minus signs. It returns as is an error. **["4" => ["2 34' 1 2 + 34 + 5 --> I '2 '34' 1 2 - 3 + 4 I '2', '3, 2 + + 3 + '2',' [ '57', '600', '+' 57 600 + 4 + 1 - 2 + 76 47 76"] 47' 1 1 1:16 + IE X Homework 2 (Template at the bo... 1'3' 5891 67 69 *328 + 4+ 589 3 + + 69 67 - ++ 328 90 + 5 90', '', '5'1 4070 - 12-56139 > 56139) .814\t33- 9 ' 56+ \t 2 -Ing --> '12 4070 1814', '33 1'56 0 0 U 11 U U 5.7 5.3 hello 2/17 5. 3 1 + (2+4) 3.66 * 2 + 7 + 4 3-( 6 + 8) 5 + 3x U 1 *126. Milestone 2 and 3: Edit the method interpret() to convert a meaningful lis t of tokens to an integer. Do not modify the method eval(). Examples of expected result of eval(), the intermediate token lists are NOT part of the result, they are included here for your reference. 1 -> ['1') --> '1' *1+2' => '1', '2] => 3 1+ 2 + 3 ==> '1','', '2', '+ 3) ==> '6' 1345 + 278 + 627 + 142 => 11345, +, 278, *627', '', '142') -> 2392 567 - 67-12+ 9 - 1'567', 'a', '67', '12'. '9') -> 497 13423 - 457 - 10714 +0+ 126 -2378' => (13423 . *457', 'a', '10714', '', 0, '+ 237 8' 1 - 0 9\t +4 -\t 33 5-> '9', '', '4', '', '33'*+ *5') >-15 891 - 152 -0- 56139 -> 1'891', '', *152', '', 0 '-', '56139 -55400 9+8-7+6- 33 ==> 1'9', '', '8', '', '7', '', 1 '-', '33' => '-17 56+ \t\t26 -9' 1'56','', 26', '91 ww>'73 -> ] > TERROR > 0 => 'ERROR ==> '1', '3'] => ERROR" 15 378 => 1'15' 378) > ERROR 3.66 > > "ERROR 3 + + 5 '5') -> ERROR +34 +5 -> 1'3', '4', 5')> ERROR 2 + 7 4 > 1] => ERROR 3- (6 + 8) ==> 0 -> ERROR 5 + 3x * --> 01 -> ERROR Milestone 2: Handle plus signs only 1:16 + IE X Homework 2 (Template at the bo... -->) -> ERROR => ) -> ERROR '13 ==> 1'i', '3'] ==> 'ERROR 15 378 ==> I'15,378'] ==> 'ERROR 3.66 --> 11 > ERROR 3 + + 5 -> ('3','' '5') -> ERROR 3 4 + 5 -> 1'3', '4', '5')-> ERROR 2 + 7.4' => 11 ==> ERROR 3- (6 + 8) ==> [ ] -> ERROR 5 + 3x ==> [ ] ==> 'ERROR . Milestone 2: Handle plus signs only [ 4 ] [2','','3' 1 4 Milestone 3: Handle plus and minus sign. *2" I '59', *200', 'a', '31' 1 '5', '3' 1 I'7 228 --> 2 Template Use the following codes with color-background) templat es as HO2_work.py name: 7222 class H02: det init_(self): pass def tokenize(self, expen): return ['1', '*, *200', 'F', '5'1 def interpret(self, tokens): return 206 def eval(self, exprn): print('exprn --> "exprn)"') #debugging message tokens - self.tokenize(exprn) print("tokens --> {tokens') debugging message value = self. interpret(tokens return value h - HO2) val = h.evale print(val) 1+ 200

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Excel As Your Database

Authors: Paul Cornell

1st Edition

1590597516, 978-1590597514

More Books

Students also viewed these Databases questions

Question

What are the potential downsides to firm growth?

Answered: 1 week ago