Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!