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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started