Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help me to fix this code (in PYTHON) so that the output is: if { in { { = state = 0 location = 0

Help me to fix this code (in PYTHON) so that the output is:

if

{

in

{

{

=

state = 0

location = 0

instream = " if { in {{ ="

def getToken():

global state

global location

global instream

while True:

if state == 0:

if instream[location] == " ":

state = 0

location += 1

break

elif instream[location] == "i":

state = 1

location += 1

break

elif instream[location].isalpha():

state = 2

location += 1

break

elif instream[location] == "{":

location += 1

return "{"

break

else:

location += 1

return "Error"

elif state == 1:

if instream[location] == "f":

state = 3

location += 1

break

elif instream[location].isalpha():

state = 2

location += 1

break

else:

location += 1

return "Identifier"

elif state == 2:

if instream[location].isalnum() and instream[location].isalnum():

state = 2

location += 1

break

else:

location += 1

return "Identifier"

elif state == 3:

if instream[location].isalnum() and instream[location].isalnum():

state = 2

location += 1

break

else:

location += 1

return "if"

index = 0

while index < len(instream):

print(getToken())

index += 1

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

Step: 3

blur-text-image

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

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions