def B(parent, table, enrty=None): #<分程序> child = Node('<分程序>') parent.add(child) C(child, table) E(child, table) if SYM[p] == KEYWORDS['procedure']: F(child, table) startAddr = len(code) if enrty != None: enrty.adr = startAddr code.append(Code('INT', 0, table.getSize())) H(child, table) code.append(Code('OPR', 0, 0)) return startAddr
def S(parent, table): #<过程调用语句> global Error child = Node('<过程调用语句>') parent.add(child) if SYM[p] == KEYWORDS['call']: child.add(Node('call')) advance() if SYM[p] == ident: name1 = Ident(child, table) advance() if SYM[p] == DELIMITERS['(']: child.add(Node('(')) advance() if SYM[p] == ident or SYM[p] == number: temp = 3 L(child, table) code.append(Code('OPR', temp, 29)) while SYM[p] == DELIMITERS[',']: advance() temp += 1 L(child, table) code.append(Code('OPR', temp, 29)) if SYM[p] == DELIMITERS[')']: child.add(Node(')')) advance() else: print(Position[p], "缺少右括号") Error = 1 elif SYM[p] == DELIMITERS[')']: child.add(Node(')')) advance() else: print(Position[p], "缺少右括号") Error = 1 else: print(Position[p], "call后面缺少括号") Error = 1 (l, a, _) = table.find(name1) if a == 0: print(Position[p], '错误,未定义的标识符', name1) Error = 1 code.append(Code('CAL', l, a)) else: print(Position[p], "call后面缺少ident") Error = 1 else: print(Position[p], "递归错误!") exit(-1)
def I(parent, table): #<赋值语句> global Error if SYM[p] == ident: child = Node('<赋值语句>') parent.add(child) name = Ident(child, table) advance() if SYM[p] != OPERATORS[':=']: print(Position[p], "赋值号错误!") Error = 1 child.add(Node(':=')) advance() L(child, table) (l, a, flag) = table.find(name) if a == 0: print(Position[p], '错误,未定义的标识符', name) Error = 1 if flag == isConst: print(Position[p], '对常量的非法赋值:' + name) Error = 1 else: code.append(Code('STO', l, a)) else: print(Position[p], '递归错误') error()
def K(parent, table): #<条件> child = Node('<条件>') parent.add(child) if SYM[p] == OPERATORS['+'] or SYM[p] == OPERATORS['-'] or SYM[ p] == ident or SYM[p] == number or SYM[p] == DELIMITERS['(']: L(child, table) opr = Q(child, table) L(child, table) code.append(Code('OPR', 0, opr)) elif SYM[p] == OPERATORS['odd']: child.add(Node('odd')) advance() L(child, table) code.append(Code('OPR', 0, OPERATORS['odd'])) else: error()
def M(parent, table): #<项> child = Node('<项>') parent.add(child) N(child, table) while SYM[p] == OPERATORS['*'] or SYM[p] == OPERATORS['/']: # 先乘除,后加减 opr = SYM[p] P(child, table) N(child, table) code.append(Code('OPR', 0, opr))
def K(parent, table): #<条件> child = Node('<条件>') parent.add(child) if SYM[p] == OPERATORS['+'] or SYM[p] == OPERATORS['-'] or SYM[ p] == ident or SYM[p] == number or SYM[p] == DELIMITERS['(']: L(child, table) opr = Q(child, table) L(child, table) code.append(Code('OPR', 0, opr)) elif SYM[p] == OPERATORS['odd']: child.add(Node('odd')) advance() L(child, table) code.append(Code('OPR', 0, OPERATORS['odd'])) else: print(Position[p], "无法识别的条件判断") global Error Error = 1
def R(parent, table): #<条件语句> child = Node('<条件语句>') parent.add(child) if SYM[p] == KEYWORDS['if']: child.add(Node('if')) advance() K(child, table) ret = Code('JPC', 0, None) code.append(ret) if SYM[p] == KEYWORDS['then']: child.add(Node('then')) advance() H(child, table) ret.a = len(code) else: error() else: error()
def U(parent, table): #<读语句> child = Node('<读语句>') parent.add(child) if SYM[p] == OPERATORS['read']: child.add(Node('read')) advance() if SYM[p] == DELIMITERS['(']: child.add(Node('(')) advance() if SYM[p] == ident: name = X(child, table) advance() code.append(Code('OPR', 0, OPERATORS['read'])) (l, a, flag) = table.find(name) if flag == isConst: print('对常量的非法赋值:' + name) else: code.append(Code('STO', l, a)) while SYM[p] == DELIMITERS[',']: child.add(Node(',')) advance() if SYM[p] == ident: name = X(child, table) advance() code.append(Code('OPR', 0, OPERATORS['read'])) (l, a, flag) = table.find(name) if flag == isConst: print('对常量的非法赋值:' + name) else: code.append(Code('STO', l, a)) else: error() if SYM[p] == DELIMITERS[')']: child.add(Node(')')) advance() else: error() else: error() else: error() else: error()
def T(parent, table): #<当型循环语句> child = Node('<当型循环语句>') parent.add(child) if SYM[p] == KEYWORDS['while']: child.add(Node('while')) advance() ret = len(code) K(child, table) fret = Code('JPC', 0, None) code.append(fret) if SYM[p] == KEYWORDS['do']: child.add(Node('do')) advance() H(child, table) code.append(Code('JMP', 0, ret)) fret.a = len(code) else: error() else: error()
def L(parent, table): #<表达式> child = Node('<表达式>') parent.add(child) if SYM[p] == OPERATORS['+']: child.add(Node('+')) advance() M(child, table) elif SYM[p] == OPERATORS['-']: child.add(Node('-')) advance() M(child, table) code.append(Code('LIT', 0, -1)) code.append(Code('OPR', 0, OPERATORS['*'])) #如果是-号做取负运算 else: M(child, table) while SYM[p] == OPERATORS['+'] or SYM[p] == OPERATORS['-']: opr = SYM[p] O(child, table) M(child, table) code.append(Code('OPR', 0, opr))
def B(parent, table, enrty=None): #<分程序> global p if SYM[p] != KEYWORDS['const'] and SYM[p] != KEYWORDS['var'] and SYM[ p] != KEYWORDS['procedure'] and SYM[p] != KEYWORDS['begin']: print(Position[p], 'block中需要关键词const or var or procedure or begin') global Error Error = 1 while p < len(SYM) - 1 and SYM[p] != KEYWORDS['const'] and SYM[ p] != KEYWORDS['var'] and SYM[p] != KEYWORDS['procedure']: p += 1 child = Node('<分程序>') parent.add(child) Const(child, table) E(child, table) if SYM[p] == KEYWORDS['procedure']: F(child, table) startAddr = len(code) if enrty != None: enrty.adr = startAddr code.append(Code('INT', 0, table.getSize())) H(child, table) code.append(Code('OPR', 0, 0)) return startAddr
def T(parent, table): #<当型循环语句> child = Node('<当型循环语句>') parent.add(child) if SYM[p] == KEYWORDS['while']: child.add(Node('while')) advance() ret = len(code) K(child, table) fret = Code('JPC', 0, None) code.append(fret) if SYM[p] == KEYWORDS['do']: child.add(Node('do')) advance() H(child, table) code.append(Code('JMP', 0, ret)) fret.a = len(code) else: print(Position[p], "while后面缺少do") global Error Error = 1 else: print(Position[p], "递归错误") error()
def S(parent, table): #<过程调用语句> child = Node('<过程调用语句>') parent.add(child) if SYM[p] == KEYWORDS['call']: child.add(Node('call')) advance() if SYM[p] == ident: name = X(child, table) advance() (l, a, _) = table.find(name) if l > 1: #当调用超出范围时出错 print('非法的过程调用') exit(-1) code.append(Code('CAL', l, a)) else: error() else: error()
def I(parent, table): #<赋值语句> if SYM[p] == ident: child = Node('<赋值语句>') parent.add(child) name = X(child, table) advance() if SYM[p] == OPERATORS[':=']: child.add(Node(':=')) advance() L(child, table) (l, a, flag) = table.find(name) if flag == isConst: print('对常量的非法赋值:' + name) error() else: code.append(Code('STO', l, a)) else: error() else: error()
from symbol import KEYWORDS, OPERATORS, DELIMITERS, SYM, ident, number, IDi, NUM, Position from node import Node from table import Table, Entry, isConst, KIND from inst import Code p = 0 pid = 0 pnum = 0 Error = 0 root = Node('<程序>') table = Table() #table表 code = [] #CODE数组 startCode = Code('JMP', 0, None) code.append(startCode) code.append(Code('JMP', 0, 2)) tableList = [] def error(a=0): # 出错 case = ['0 '] # print("Error:",p) exit(-1) def advance(): global p p = p + 1 def program(): if SYM[p] == KEYWORDS['program']:
from symbol import KEYWORDS, OPERATORS, DELIMITERS, SYM, ident, number, IDi, NUM from node import Node from table import Table, Entry, isConst, KIND from inst import Code p = 0 pid = 0 pnum = 0 root = Node('<程序>') table = Table() #table表 code = [] #CODE数组 startCode = Code('JMP', 0, None) code.append(startCode) code.append(Code) tableList = [] def error(): # 出错 print("Error:", p) exit(-1) def advance(): global p p = p + 1 def block(): tableList.append(table) A(root, table)