0

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.

3
  • 4
    Your list index is out of range. You're using i + 1 so you will go beyond the end of the list. Commented Apr 26, 2015 at 21:52
  • 3
    There's no reason to post your error as a screenshot. Just copy and paste the text here. Commented Apr 26, 2015 at 21:54
  • Thanks Daniel Roseman for that, new to stackoverflow, and thanks L3viathan for the edit. Commented Apr 26, 2015 at 22:01

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.