making a language interpreter in python but ran through this error, it only shows 2 strings then shows the error. why is that? it worked fine before printing all NUMs and EXPRs, the problem seems to be mostly in the parse function.
from sys import *
tokens = []
def open_file(filename):
data = open(filename, 'r').read()
data += "<EOF>"
return data
def lex(filecontents):
tok = ""
state = 0
string = ""
expr = ""
n = ""
isexpr = 0
for char in filecontents:
tok += char
if tok == " ":
if state == 0:
tok = ""
else:
tok = " "
elif tok == "\n" or tok =="<EOF>":
if expr != "" and isexpr == 1:
tokens.append("EXPR:" + expr)
expr = ""
elif expr != "" and isexpr == 0:
tokens.append("NUM:" + expr)
expr = ""
tok = ""
elif tok == "PRINT" or tok == "print":
tokens.append("PRINT")
tok = ""
elif tok == "0" or tok == "1" or tok == "2" or tok == "3" or tok == "4" or tok == "5" or tok == "6" or tok == "7" or tok == "8" or tok == "9":
expr += tok
tok = ""
elif tok == "+":
isexpr = 1
expr += tok
tok = ""
elif tok == "\"":
if state == 0:
state = 1
elif state == 1:
tokens.append("STRING:" + string + "\"")
string = ""
state = 0
tok = ""
elif state == 1:
string += tok
tok = ""
#print(tokens)
return tokens
def parse(toks):
i = 0
while(i < len(toks)):
if toks[i] + " " + toks[i+1][0:6] == "PRINT STRING" or toks[i] + " " + toks[i+1][0:3] == "PRINT NUM" or toks[i] + " " + toks[i+1][0:4] == "PRINT EXPR":
if toks[i+1][0:6] == "STRING":
print(toks[i+1][7:])
elif toks[i+1][0:3] == "NUM":
print(toks[i+1][4:])
elif toks[i+1][0:4] == "EXPR":
print(toks[i+1][5:])
i+= 2
def run():
data = open_file(argv[1])
toks = lex(data)
parse(toks)
run()
PRINT "HELLO WORLD"
print "string"
566755664645454
5 + 6
print 55
print 5 + 8
PS C:\Users\essam\Desktop\spl> python basic.py test.lang
"HELLO WORLD"
"string"
Traceback (most recent call last):
File "C:\Users\essam\Desktop\spl\basic.py", line 73, in <module>
run()
File "C:\Users\essam\Desktop\spl\basic.py", line 71, in run
parse(toks)
File "C:\Users\essam\Desktop\spl\basic.py", line 58, in parse
while(i < len(toks)):
^^^^^^^^^
KeyboardInterrupt
PS C:\Users\essam\Desktop\spl>
I tried changing the parse function but my attempts ended up printing 566755664645454 but not 5 + 6
def parse(toks):
i = 0
while(i < len(toks)):
if toks[i] + " " + toks[i+1][0:6] == "PRINT STRING" or toks[i] + " " + toks[i+1][0:3] == "PRINT NUM" or toks[i] + " " + toks[i+1][0:4] == "PRINT EXPR":
...
i+= 2
If that first if
condition is not true, the while
loop will run forever, testing the same condition over and over, because i
does not change.
I think maybe you wanted the i += 2
to be at the same indentation level as the if
, not nested inside of it.