示例#1
0
def E(parent, table):  #<变量说明部分>
    if SYM[p] == KEYWORDS['var']:
        child = Node('<变量说明部分>')
        parent.add(child)
        child.add(Node('var'))
        advance()
        if SYM[p] == ident:
            name = X(child, table)
            entry = Entry(name, KIND.VARIABLE)
            table.add(entry)
            advance()
            while SYM[p] == DELIMITERS[',']:
                child.add(Node(','))
                advance()
                if SYM[p] == ident:
                    name = X(child, table)
                    entry = Entry(name, KIND.VARIABLE)
                    table.add(entry)
                    advance()
                else:
                    error()
            if SYM[p] == DELIMITERS[';']:
                child.add(Node(';'))
                advance()
            else:
                error()
        else:
            error()
示例#2
0
def G(parent, table):  #<过程首部>
    global Error
    if SYM[p] == KEYWORDS['procedure']:
        child = Node('<过程首部>')
        parent.add(child)
        child.add(Node('procedure'))
        advance()
        if SYM[p] != ident:
            print(Position[p], 'procedure后面缺少ident')
            Error = 1
        name = Ident(child, table)
        entry = Entry(name, KIND.PROCEDURE)
        t = table.add(entry)
        if t == 1:
            print(Position[p], entry.name + ' 重定义')
            Error = 1
        childTable = Table(table)
        advance()
        if SYM[p] != DELIMITERS['(']:
            print(Position[p], "procedure后面缺少()")
            Error = 1
        advance()
        while (SYM[p] == ident):
            name = Ident(child, table)
            entry1 = Entry(name, KIND.VARIABLE)
            childTable.add(entry1)
            advance()
            while SYM[p] == DELIMITERS[',']:
                child.add(Node(','))
                advance()
                if SYM[p] == ident:
                    name = Ident(child, table)
                    entry1 = Entry(name, KIND.VARIABLE)
                    childTable.add(entry1)
                    advance()
                else:
                    print(Position[p], "传参只能传ident")
                    Error = 1
        if SYM[p] == DELIMITERS[')']:
            advance()
        else:
            print(Position[p], "缺少‘)’")
            Error = 1
        if SYM[p] == DELIMITERS[';']:
            child.add(Node(';'))
            advance()
            return childTable, entry
        else:
            print(Position[p], "缺少分号")
            Error = 1
    else:
        print(Position[p], "递归错误!")
        Error = 1
        error()
def AddVarNode(parent, table):  #<变量说明部分>
    global Error, treeid

    treeid = treeid + 1
    child = Node('<变量说明部分>', treeid)
    parent.AddChild(child)
    t = Tree(parent.treeid, parent.Name, treeid, '<Variable description>')
    treelist.append(t)

    treeid = treeid + 1
    child.AddChild(Node('var', treeid))
    t = Tree(child.treeid, child.Name, treeid, 'var')
    treelist.append(t)

    GoNext()
    if MYSYM[p] != ident:
        print(Location[p], "变量类型定义错误,只能是ident")
        Error = 1
    name = AddIdentNode(child, table)
    entry = Entry(name, KIND.VARIABLE)
    t = table.add(entry)
    if t == 1:
        print(Location[p], entry.name + ' 重定义')
        Error = 1
    GoNext()
    while MYSYM[p] == Border[',']:
        treeid = treeid + 1
        child.AddChild(Node(',', treeid))
        t = Tree(child.treeid, child.Name, treeid, ',')
        treelist.append(t)

        GoNext()
        if MYSYM[p] != ident:
            print(Location[p], "变量类型定义错误,只能是ident")
            Error = 1
        name = AddIdentNode(child, table)
        entry = Entry(name, KIND.VARIABLE)
        t = table.add(entry)
        if t == 1:
            print(Location[p], entry.name + ' 重定义')
            Error = 1
        GoNext()
    if MYSYM[p] == Border[';']:
        treeid = treeid + 1
        child.AddChild(Node(';', treeid))
        t = Tree(child.treeid, child.Name, treeid, ';')
        treelist.append(t)
        GoNext()
    else:
        print(Location[p], "行末缺少分号")
        Error = 1
示例#4
0
def G2(table):  #<过程首部>
    global Error
    if SYM[p] != KEYWORDS['program']:
        print(Position[p], "缺少程序开始标志program")
        exit(-1)
    advance()
    if SYM == '(':
        print(Position[p], 'program后面缺少ident')
        Error = 1
    elif SYM[p] != ident:
        print(Position[p], "类型错误program后面定义为ident类型")
        Error = 1
    child1 = Node('program')
    name = Ident(child1, table)
    entry = Entry(name, KIND.PROCEDURE)
    t = table.add(entry)
    if t == 1:
        print(Position[p], entry.name + ' 重定义')
        Error = 1
    advance()
    if SYM[p] == DELIMITERS[';']:
        child1.add(Node(';'))
        advance()
        return table, entry
    else:
        child1.add(Node(';'))
        print(Position[p], "行末缺少分号")
        Error = 1
        return table, entry
def AddDefine_constNode(parent, table):  #<常量定义>
    global Error, treeid
    treeid = treeid + 1
    child = Node('<常量定义>', treeid)
    parent.AddChild(child)
    t = Tree(parent.treeid, parent.Name, treeid, '<Constants defined>')
    treelist.append(t)
    if MYSYM[p] != ident:  # const 后面需要跟着标识符
        print("<常量定义>错误,缺少标识符")
        exit(-1)
    name = AddIdentNode(child, table)  # 添加<标识符>节点
    GoNext()
    if MYSYM[p] != Operation[':=']:  # 标识符后面需要跟着 :=
        print(Location[p], "缺少赋值符号")
        exit(-1)
    treeid = treeid + 1
    child.AddChild(Node(':=', treeid))
    t = Tree(child.treeid, child.Name, treeid, ':=')
    treelist.append(t)
    GoNext()
    if MYSYM[p] != number:  # := 后面需要跟着 <常数>节点
        print(Location[p], "常量赋值错误,只能用常数赋值")
        Error = 1
    val = AddUnintNode(child, table)  # 添加<常数>节点
    GoNext()
    entry = Entry(name, KIND.CONSTANT, val)  # 新增常量或者变量的时候需要在字符表中新增一条目录
    t = table.add(entry)  # 目录entry添加到字符表的时候需要进行判断是否重定义
    if t == 1:
        print(Location[p], entry.name + ' 重定义')
        Error = 1
示例#6
0
def E(parent, table):  #<变量说明部分>
    global Error
    if SYM[p] == KEYWORDS['var']:
        child = Node('<变量说明部分>')
        parent.add(child)
        child.add(Node('var'))
        advance()
        if SYM[p] != ident:
            print(Position[p], "变量类型定义错误,只能是ident")
            Error = 1
        name = Ident(child, table)
        entry = Entry(name, KIND.VARIABLE)
        t = table.add(entry)
        if t == 1:
            print(Position[p], entry.name + ' 重定义')
            Error = 1
        advance()
        while SYM[p] == DELIMITERS[',']:
            child.add(Node(','))
            advance()
            if SYM[p] != ident:
                print(Position[p], "变量类型定义错误,只能是ident")
                Error = 1
            name = Ident(child, table)
            entry = Entry(name, KIND.VARIABLE)
            t = table.add(entry)
            if t == 1:
                print(Position[p], entry.name + ' 重定义')
                Error = 1
            advance()
        if SYM[p] == DELIMITERS[';']:
            child.add(Node(';'))
            advance()
        else:
            print(Position[p], "行末缺少分号")
            Error = 1
示例#7
0
def D(parent, table):  #<常量定义>
    child = Node('<常量定义>')
    parent.add(child)
    if SYM[p] == ident:
        name = X(child, table)
        advance()
        if SYM[p] == OPERATORS['=']:
            child.add(Node('='))
            advance()
            if SYM[p] == number:
                val = W(child, table)
                advance()
                entry = Entry(name, KIND.CONSTANT, val)
                table.add(entry)
            else:
                error()
        else:
            error()
    else:
        error()
示例#8
0
def G(parent, table):  #<过程首部>
    if SYM[p] == KEYWORDS['procedure']:
        child = Node('<过程首部>')
        parent.add(child)
        child.add(Node('procedure'))
        advance()
        if SYM[p] == ident:
            name = X(child, table)
            entry = Entry(name, KIND.PROCEDURE)
            table.add(entry)
            childTable = Table(table)
            advance()
            if SYM[p] == DELIMITERS[';']:
                child.add(Node(';'))
                advance()
                return childTable, entry
            else:
                error()
        else:
            error()
    else:
        error()
示例#9
0
def Define_const(parent, table):  #<常量定义>
    global Error
    child = Node('<常量定义>')
    parent.add(child)
    if SYM[p] != ident:
        print(Position[p], "常量定义类型错误,应为ident")
        Error = 1
    name = Ident(child, table)
    advance()
    if SYM[p] != OPERATORS[':=']:
        print(Position[p], "赋值号错误,赋值号为:=")
        Error = 1
    child.add(Node(':='))
    advance()
    if SYM[p] != number:
        print(Position[p], "常量赋值错误,只能用常数赋值")
        Error = 1
    val = W(child, table)
    advance()
    entry = Entry(name, KIND.CONSTANT, val)
    t = table.add(entry)
    if t == 1:
        print(Position[p], entry.name + ' 重定义')
        Error = 1
def AddprocedureHeadNode(parent, table):  #<过程首部>!!!!!!!!!!!!!!!!!!!!!!!!!!!1
    global Error, treeid
    if MYSYM[p] == Key['procedure']:
        treeid = treeid + 1
        child = Node('<过程首部>', treeid)
        parent.AddChild(child)
        t = Tree(parent.treeid, parent.Name, treeid, '<The process first>')
        treelist.append(t)

        treeid = treeid + 1
        child.AddChild(Node('procedure', treeid))
        t = Tree(child.treeid, child.Name, treeid, 'procedure')
        treelist.append(t)

        GoNext()
        if MYSYM[p] != ident:
            print(Location[p], 'procedure后面缺少ident')
            Error = 1
        name = AddIdentNode(child, table)
        entry = Entry(name, KIND.PROCEDURE)
        t = table.add(entry)
        if t == 1:
            print(Location[p], entry.name + ' 重定义')
            Error = 1
        childTable = Table(table)  # level 加1
        GoNext()
        '''
        if MYSYM[p] != Border['(']:
            print(Location[p],"procedure后面缺少()")
            Error = 1
        GoNext()
        while (MYSYM[p] == ident):
            name = AddIdentNode(child, table)
            entry1 = Entry(name, KIND.VARIABLE)
            childTable.add(entry1)
            GoNext()
            while MYSYM[p] == Border[',']:
                treeid=treeid+1
                child.AddChild(Node(',',treeid))
                t=Tree(child.treeid,child.Name,treeid,',')
                treelist.append(t)

                GoNext()
                if MYSYM[p] == ident:
                    name = AddIdentNode(child, table)
                    entry1 = Entry(name, KIND.VARIABLE)
                    childTable.add(entry1)
                    GoNext()
                else:
                    print(Location[p],"传参只能传ident")
                    Error = 1
        if MYSYM[p] == Border[')']:
            GoNext()
        else:
            print(Location[p],"缺少‘)’")
            Error = 1
            '''
        if MYSYM[p] == Border[';']:
            treeid = treeid + 1
            child.AddChild(Node(';', treeid))
            t = Tree(child.treeid, child.Name, treeid, ';')
            treelist.append(t)

            GoNext()
            return childTable, entry, table
        else:
            print(Location[p], "缺少分号")
            Error = 1
    else:
        print(Location[p], "递归错误!")
        Error = 1
        error()