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

o3imoua4  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(253)

我正在编写一个用于解析语法无关上下文项目的代码,它适用于某些测试用例(例如: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()

下面是命令提示符下的我的回溯错误。

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题