Darcey Mckelvey
Darcey Mckelvey

Reputation: 556

Need help fixing python error

This is the basic.py file for a programming language I am making. At the moment it is throwing an error.

 from sys import *

tokens = []

def open_file(filename):
    data = open(filename, "r").read()
    data += "<EOF>"
    return data

def lex(filecontents):
    tok = ""
    state = 0
    isexpr = 0
    string = ""
    expr = ""
    n = ""
    filecontents = list(filecontents)
    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:
                #print(expr + "EXPR")
                tokens.append("EXPR:" + expr)
                expr = ""
            elif expr != "" and isexpr == 0:
                #print(expr + "NUM")
                tokens.append("NUM:" + expr)
                expr = ""
            tok = ""
        elif tok.lower() == "print":
            tokens.append("PRINT")
            tok = ""
        elif tok.isnumeric():
            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()

here is the test.vil file(my programming language is called villar) that I am passing data through:

STRING "HELLO WORLD"
string "Hey world!"
17 + 3 

As a result, I get an IndexError: List index out of range in line 62.

Can you anyone help me help here? I'd love advice on how to improve it to if its allowed here.

Upvotes: 0

Views: 63

Answers (1)

MasterOdin
MasterOdin

Reputation: 7886

You've got the line:

while(i < len(toks)):

in the parse function. However, within this while loop, you access toks[i+1] element, which'll be out of bounds on the final iteration of the while loop (as i == len(toks)-1 and i+1 == len(toks) which is out of bounds and throwing an error). You need to change that above line to:

while(i < len(toks)-1):

so that on the final iteration i == len(toks) - 2 and i+1 == len(toks) - 1 which are both in bounds.

Upvotes: 3

Related Questions