Python解析树索引器错误:字符串索引超出范围

Python解析树索引器错误:字符串索引超出范围,python,parsing,index-error,Python,Parsing,Index Error,我正在编写一个用于解析无语法上下文项目的代码,它适用于某些测试用例(例如:read(a)),但对于某些测试用例(例如,“writea+b”)则不起作用。上面说有一个 索引器错误:字符串索引超出范围 它通常会在识别字母和空格时出现这种错误。我在命令提示符下运行了这个命令,并将我的代码和test.txt保存在我cd要保存的文件夹中 import sys #Grammar #<program> → <stmt list> $$ #<stmt list> → <

我正在编写一个用于解析无语法上下文项目的代码,它适用于某些测试用例(例如:read(a)),但对于某些测试用例(例如,“writea+b”)则不起作用。上面说有一个

索引器错误:字符串索引超出范围

它通常会在识别字母和空格时出现这种错误。我在命令提示符下运行了这个命令,并将我的代码和
test.txt
保存在我
cd
要保存的文件夹中

import sys

#Grammar
#<program> → <stmt list> $$
#<stmt list> → <stmt> <stmt list> | 
#<stmt> → id assign <expr> | read id | write <expr>
#<expr> → <term> <term tail>
#<term tail> → <add op> <term> <term tail> | 
#<term> → <factor> <fact tail>
#<fact tail> → <mult op> <factor> <fact tail> | 
#<factor> → lparen <expr> rparen | id | number
#<add op> → plus | minus
#<mult op> → times | div


def scanner(x):
    output = ""
    h = ""
    o = 0
    for y in range(0, len(x)):
        #h = ""
        if x[y].isspace():
            y += 1
            o=y
        elif x[y] == "(":
            output += "<term>\n"
            output += "lparen\n"
            output += "</term>\n"
            y += 1
            o=y

        elif x[y] == ":":
            if x[o+1] == "=":
                output += "assign\n"
                o=y
            else:
                print("error")
                exit()

        elif x[y] == "/":
            o=y
            if x[o+1] == "*":
                while x[y] != "*/":
                    y+=1

                    if x[y] == "*/":
                        y+=1
            else:
                o=y
                output += "<div>\n"
                output += "div\n"
                output += "</div>\n"

        elif x[y] == ")":
            output += "<term_tail>\n"
            output += "rparen\n"
            output += "</term_tail>\n"
            y += 1
            o=y

        elif x[y] == "+":
            output += "<plus>\n"
            output += x[y]+"\n"
            output += "</plus>\n"
            y += 1
            o=y

        elif x[y] == ".":
            if x[o+1].isdigit():
                y+=1
                o=y
                if x[y].isdigit():
                    y+=1
                    o = y
                else:
                    output += "number\n"
            else:
                print("error")
                exit()

        elif x[y] == "-":
            output += "<minus>\n"
            output += "minus\n"
            output += "</minus>\n"
            y += 1
            o=y

        elif x[y] == "*":
            output += "<mult_op>"
            output += "times\n"
            y += 1
            o=y

        elif x[y] == "=":

            output += "assign\n"
            y += 1
            o=y

        elif x[y].isdigit():
            if x[o+1].isdigit():
                y+=1
                o=y
            else:
                output += "number,"

        elif x[y].isalpha():
            h += x[y]
            o=y
            if x[o+1].isalpha():
                pass
            elif h == "read":
                output+= "<read>\n"
                output += h+"\n"
                output += "</read>\n"
                h = ""
                

            elif h == "write":
                output += "<write>\n"
                output += h+"\n"
                output += "</write>\n"
                h = ""

            else:
                output += "<id>\n"
                output += h+"\n"
                output += "</id>\n"
                h=""

            y += 1
            o = y
        elif x[y] == "/*":
            while x[y] != "*/":
                y+=1
            pass
        else:
            print("error")

    print(output)


def printtree(x):
    print("<Program>")
    print("   <stmt_list>")
    print("      <stmt>")
    scanner(x)
    print("      </stmt>")
    print("   </stmt_list>")
    print("</Program>")


def main():
    with open(sys.argv[1], 'r') as f:
        x = f.read()
    #scanner(x)
    printtree(x)


main()
导入系统 #文法 # → $$ # → | # → id分配|读id |写 # → # → | # → # → | # → lparen rparen | id |编号 # → 正|负 # → 时代周刊 def扫描仪(x): output=“” h=“” o=0 对于范围(0,len(x))内的y: #h=“” 如果x[y].isspace(): y+=1 o=y elif x[y]==“(”: 输出+=“\n” 输出+=“lparen\n” 输出+=“\n” y+=1 o=y elif x[y]==“:”: 如果x[o+1]==”: 输出+=“分配\n” o=y 其他: 打印(“错误”) 退出() elif x[y]==“/”: o=y 如果x[o+1]==“*”: 而x[y]!=“*/”: y+=1 如果x[y]==“*/”: y+=1 其他: o=y 输出+=“\n” 输出+=“div\n” 输出+=“\n” elif x[y]==”): 输出+=“\n” 输出+=“rparen\n” 输出+=“\n” y+=1 o=y elif x[y]==“+”: 输出+=“\n” 输出+=x[y]+“\n” 输出+=“\n” y+=1 o=y elif x[y]==”: 如果x[o+1].isdigit(): y+=1 o=y 如果x[y].isdigit(): y+=1 o=y 其他: 输出+=“编号\n” 其他: 打印(“错误”) 退出() elif x[y]==“-”: 输出+=“\n” 输出+=“负\n” 输出+=“\n” y+=1 o=y elif x[y]==“*”: 输出+=“” 输出+=“次\n” y+=1 o=y elif x[y]==“=”: 输出+=“分配\n” y+=1 o=y elif x[y]。isdigit(): 如果x[o+1].isdigit(): y+=1 o=y 其他: 输出+=“数字,” elif x[y]。isalpha(): h+=x[y] o=y 如果x[o+1].isalpha(): 通过 elif h==“读取”: 输出+=“\n” 输出+=h+“\n” 输出+=“\n” h=“” elif h==“写入”: 输出+=“\n” 输出+=h+“\n” 输出+=“\n” h=“” 其他: 输出+=“\n” 输出+=h+“\n” 输出+=“\n” h=“” y+=1 o=y elif x[y]==“/*”: 而x[y]!="*/": y+=1 通过 其他: 打印(“错误”) 打印(输出) def打印树(x): 打印(“”) 打印(“”) 打印(“”) 扫描仪(x) 打印(“”) 打印(“”) 打印(“”) def main(): 将open(sys.argv[1],'r')作为f: x=f.read() #扫描仪(x) 打印树(x) main() 以下是命令提示符下的“我的回溯”错误

C:\Users\sbcar\Desktop\Concepts>python ProjectTest.py test1.txt
<Program>
   <stmt_list>
      <stmt>
Traceback (most recent call last):
  File "C:\Users\sbcar\Desktop\Concepts\ProjectTest.py", line 160, in <module>
    main()
  File "C:\Users\sbcar\Desktop\Concepts\ProjectTest.py", line 157, in main
    printtree(x)
  File "C:\Users\sbcar\Desktop\Concepts\ProjectTest.py", line 147, in printtree
    scanner(x)
  File "C:\Users\sbcar\Desktop\Concepts\ProjectTest.py", line 110, in scanner
    if x[o+1].isalpha():
IndexError: string index out of range
C:\Users\sbcar\Desktop\Concepts>python ProjectTest.py test1.txt
回溯(最近一次呼叫最后一次):
文件“C:\Users\sbcar\Desktop\Concepts\ProjectTest.py”,第160行,在
main()
文件“C:\Users\sbcar\Desktop\Concepts\ProjectTest.py”,第157行,主目录
打印树(x)
文件“C:\Users\sbcar\Desktop\Concepts\ProjectTest.py”,第147行,在printtree中
扫描仪(x)
文件“C:\Users\sbcar\Desktop\Concepts\ProjectTest.py”,第110行,在扫描仪中
如果x[o+1].isalpha():
索引器错误:字符串索引超出范围

请提供预期的(MRE)。我们应该能够复制和粘贴一个连续的代码块,执行该文件,并再现您的问题以及跟踪问题点的输出。这让我们可以根据您的测试数据和期望的输出来测试我们的建议。显示中间结果与您预期的不同之处。正如发布指南所告诉您的,“让他人更容易帮助您。”您在没有追踪问题、没有隔离问题、也没有提供自包含示例的情况下转储了代码。由于您通常使用无意义的变量名,因此您倾向于不鼓励使用150行代码的潜在助手。初始调试是您的责任。很抱歉,这是我第一次在这个网站上问问题,但这样可以吗@selcuki如果x[o+1].isalpha()行中的索引外有错误,则应使用
o
中作为索引的内容,并将
o+1
len(x)
进行比较,它必须是
o+1
。如果
o+1>=len(x)
o