コード例 #1
0
 def analyze_sentencePrintf(self, overlookSet = [const.R_BRACE]):
     downOverlookSet = overlookSet[:]
     self.overlookSetAdd(downOverlookSet, const.R_PARENTHESIS)
     V_sentencePrintf = VN.create(const.SENTENCE_PRINTF)
     V_sentencePrintf.append(self.pointer)
     self.getsym()
     if self.pointer.isL_Parenthesis():
         V_sentencePrintf.append(self.pointer)
         self.getsym()
         if self.pointer.isString():
             self.emit(Instruction(Instruction.LOD, '"'+ self.pointer.text + '"'))
             V_sentencePrintf.append(self.pointer)
             self.getsym()
             if self.pointer.isComma():
                 V_sentencePrintf.append(self.pointer)
                 self.getsym()
             # 这里虽然没有逗号,但是为了后面的表达式,不要用overlook函数!
             elif not self.pointer.isR_Parenthesis() :
                 self.error(Error.GA_MISS_COMMA, self.pointer.previous)
         # 如果字符串后面不是有括号,那就解析表达式
         if not self.pointer.isR_Parenthesis():
             V_sentencePrintf.append(self.analyze_expression(downOverlookSet))
         if self.pointer.isR_Parenthesis():
             V_sentencePrintf.append(self.pointer)
             self.getsym()
         else:
             self.error(Error.GA_MISS_R_PARENTHESIS, self.pointer.previous)
             self.overlookToMarks(overlookSet)
     else :
         self.error(Error.GA_MISS_L_PARENTHESIS, self.pointer.previous)
         self.overlookToMarks(overlookSet)
     # 两部分都加载完成后调用printf函数
     self.emit(Instruction(Instruction.JSR, 'PRINTF'))
     self.instructionStream.setLab('ret_addr')
     return V_sentencePrintf
コード例 #2
0
 def analyze_sentenceAssign(self, overlookSet = [const.R_BRACE]):
     downOverlookSet = overlookSet[:]
     V_sentenceAssign = VN.create(const.SENTENCE_ASSIGN)
     symbolItem = self.symbolTable.getItem(self.pointer.text)
     if symbolItem:
         V_sentenceAssign.append(self.pointer)
         if symbolItem.isInt() or symbolItem.isParameter():
             # 此处是标识符,生成LDA指令
             self.emit(Instruction(Instruction.LDA, symbolItem.name))
             self.getsym()
         else:
             # 这里的给常量赋值错误虽然报错,但是仍然要读取下一个符号并继续分析,
             # 仅仅不再插入指令
             self.error(Error.ST_ASSIGN_CONST)
             self.getsym()
     else:
         self.error(Error.ST_UNDEFINED_ID)
         self.overlookToMarks(overlookSet)
     if self.pointer.isAssign():
         V_sentenceAssign.append(self.pointer)
         self.getsym()
         V_sentenceAssign.append(self.analyze_expression(overlookSet))
         # 表达式识别结束之后将赋值指令入栈
         self.emit(Instruction(Instruction.STN))
     else:
         self.error(Error.GA_MISS_ASSIGN)
         self.overlookToMarks(overlookSet)
     return V_sentenceAssign
コード例 #3
0
 def analyze_sentenceScanf(self, overlookSet = [const.R_BRACE]):
     downOverlookSet = overlookSet[:]
     self.overlookSetAdd(downOverlookSet, const.R_PARENTHESIS)
     V_sentenceScanf = VN.create(const.SENTENCE_SCANF)
     V_sentenceScanf.append(self.pointer)
     self.getsym()
     if self.pointer.isL_Parenthesis():
         V_sentenceScanf.append(self.pointer)
         self.getsym()
         if self.pointer.isID():
             id = self.pointer
             symbol = self.symbolTable.getItem(id.text)
             if not symbol:
                 self.error(Error.ST_UNDEFINED_ID)
             # 找到标识符,先加载
             else:
                 self.emit(Instruction(Instruction.LDA, id.text))
             V_sentenceScanf.append(self.pointer)
             self.getsym()
             if self.pointer.isR_Parenthesis():
                 V_sentenceScanf.append(self.pointer)
                 self.getsym()
                 # 添加赋值指令
                 self.emit(Instruction(Instruction.JSR, 'SCANF'))
                 self.instructionStream.setLab('ret_addr')
             else:
                 self.error(Error.GA_MISS_R_PARENTHESIS, self.pointer.previous)
                 self.overlookToMarks(overlookSet)
         else :
             self.error(Error.GA_MISS_IDENTIFIER)
             self.overlookToMarks(overlookSet)
     else :
         self.error(Error.GA_MISS_L_PARENTHESIS, self.pointer.previous)
         self.overlookToMarks(overlookSet)
     return V_sentenceScanf
コード例 #4
0
 def analyze_sentenceWhile(self, overlookSet = [const.R_BRACE]):
     downOverlookSet = [const.R_BRACE]
     self.overlookSetAdd(downOverlookSet, const.R_PARENTHESIS)
     V_sentenceWhile = VN.create(const.SENTENCE_LOOP)
     V_sentenceWhile.append(self.pointer)
     self.getsym()
     whileBRInstruction = None
     whileHeadLab = Instruction.generateLab()
     if self.pointer.isL_Parenthesis():
         V_sentenceWhile.append(self.pointer)
         self.getsym()
         # 为while头部设置lab标签
         self.instructionStream.setLab(whileHeadLab)
         V_sentenceWhile.append(self.analyze_condition(downOverlookSet))
         if self.pointer.isR_Parenthesis():
             V_sentenceWhile.append(self.pointer)
             self.getsym()
             # 条件分析结束后加入BRF指令
             whileBRInstruction = Instruction(Instruction.BRF, Instruction.generateLab())
             self.instructionStream.append(whileBRInstruction)
             V_sentenceWhile.append(self.analyze_sentence(overlookSet))
             # while中的语句分析结束后,加入BR指令,直接跳转回while头部
             self.emit(Instruction(Instruction.BR, whileHeadLab))
         else:
             self.error(Error.GA_MISS_R_PARENTHESIS)
             self.overlookToMarks(downOverlookSet)
     else:
         self.error(Error.GA_MISS_L_PARENTHESIS, self.pointer.previous)
         self.overlookToMarks(downOverlookSet)
     # while分析结束后,在其后的第一条语句添加lab标记
     if whileBRInstruction:
         self.instructionStream.setLab(whileBRInstruction.operator)
     return self.checkEmpty(V_sentenceWhile)
コード例 #5
0
 def analyze_sentenceIf(self, overlookSet = [const.R_BRACE]):
     downOverlookSet = overlookSet[:]
     self.overlookSetAdd(downOverlookSet, const.R_PARENTHESIS)
     self.overlookSetAdd(downOverlookSet, const.ELSE)
     ifSentenceOverlookSet = overlookSet[:]
     self.overlookSetAdd(ifSentenceOverlookSet, const.ELSE)
     V_sentenceIf = VN.create(const.SENTENCE_CONDITION)
     V_sentenceIf.append(self.pointer)
     self.getsym()
     if self.pointer.isL_Parenthesis():
         V_sentenceIf.append(self.pointer)
         self.getsym()
         V_sentenceIf.append(self.analyze_condition(downOverlookSet))
         if self.pointer.isR_Parenthesis():
             # 分析完条件,加入比较跳转指令
             headBRFInstruction = Instruction(Instruction.BRF, Instruction.generateLab())
             self.emit(headBRFInstruction)
             V_sentenceIf.append(self.pointer)
             self.getsym()
             if not self.pointer.isR_Else():
                 V_sentenceIf.append(self.analyze_sentence(ifSentenceOverlookSet))
             else:
                 self.error(Error.GA_MISS_SENTENCE, self.pointer.previous, 'Gramma Analysis Error: A sentence between IF and ELSE is expected.')
             if self.pointer.isR_Else():
                 V_sentenceIf.append(self.pointer)
                 self.getsym()
                 # 在if标记后的语句分析结束后,Else分析开始前,添加一条BR指令,
                 # 用于跳转到Else分析结束后的第一条指令
                 # 如果没有else部分,这条指令不存在
                 headBRInstruction = Instruction(Instruction.BR, Instruction.generateLab())
                 self.emit(headBRInstruction)
                 # if标记之后的语句分析结束后,加入lab标记,
                 # 使得在Else分析出的语句中的第一条语句获得lab标记
                 self.instructionStream.setLab(headBRFInstruction.operator)
                 V_sentenceIf.append(self.analyze_sentence(overlookSet))
                 # 对于存在Else分析的情况,在Else结束后的第一条语句后面添加BR标记
                 self.instructionStream.setLab(headBRInstruction.operator)
             else:
                 # if标记之后的语句分析结束后,加入lab标记,
                 # 在没有else标记的情况下,if分析后的第一条语句也要加入lab标记
                 self.instructionStream.setLab(headBRFInstruction.operator)
         else :
             self.error(Error.GA_MISS_R_PARENTHESIS)
             self.overlookToMarks(overlookSet)
     else:
         self.error(Error.GA_MISS_L_PARENTHESIS, self.pointer.previous)
         self.overlookToMarks(overlookSet)
     return V_sentenceIf
コード例 #6
0
 def analyze_factor(self, overlookSet):
     downOverlookSet = overlookSet[:]
     self.overlookSetAdd(downOverlookSet,const.R_PARENTHESIS)
     V_factor = VN.create(const.FACTOR)
     # 进入后先判断是哪种因子
     # 如果是ID,则可能是标识符或者调用函数
     if self.pointer.isID() and (not self.pointer.next.isL_Parenthesis()):
         symbolItem = self.symbolTable.getItem(self.pointer.text)
         # 去符号表中找这个ID,找不到记得报错
         if symbolItem:
             V_factor.append(self.pointer)
             self.emit(Instruction(Instruction.LOC, symbolItem.name))
             self.getsym()
         else:
             self.error(Error.ST_UNDEFINED_ID)
             self.overlookToMarks(overlookSet)
     elif self.pointer.isID() and self.pointer.next.isL_Parenthesis():
         symbolItem = self.symbolTable.getItem(self.pointer.text)
         # 去符号表中找这个ID,找不到记得报错
         if symbolItem:
             V_factor.append(self.analyze_sentenceFunctionCall(overlookSet))
         else:
             self.error(Error.ST_UNDEFINED_ID)
             self.overlookToMarks(overlookSet)
     # 处理表达式
     elif self.pointer.isL_Parenthesis():
         l_parenthesis = self.pointer
         self.getsym()
         # 接着要进行进入表达式解析
         expression = self.analyze_expression(downOverlookSet)
         if self.pointer.isR_Parenthesis():
             V_factor.append(l_parenthesis).append(expression).append(self.pointer)
             self.getsym()
         else:
             self.error(Error.GA_MISS_R_PARENTHESIS)
             self.overlookToMarks(overlookSet)
     elif self.pointer.isInteger():
         V_factor.append(self.pointer)
         self.emit(Instruction(Instruction.LOC, self.pointer.text))
         self.getsym()
     else:
         self.error(Error.GA_ILLEGAL_INPUT, self.pointer.previous, "Gramma Analysis Error: An expression, id, or integer is expeted")
         self.overlookToMarks(overlookSet)
     return self.checkEmpty(V_factor)
コード例 #7
0
 def run_functionDefine(self, Vn):
     if self.hasError() or Vn.isEmpty():
         return
     if Vn.isType(const.MAIN_FUNCTION):
         if Vn.hasChild(const.VOID):
             returnValue = SymbolTableItem.TYPE_VOID
         else:
             returnValue = SymbolTableItem.TYPE_INT
         id = Vn.findChild(const.MAIN)
     elif Vn.hasChild(const.VOID):
         returnValue = SymbolTableItem.TYPE_VOID
         id = Vn.findChild(const.ID)
     else :
         returnValue = SymbolTableItem.TYPE_INT
         id = Vn.findChild(const.DECLARE_HEAD).findChild(const.ID)
     no = self.symbolTable.addItem(id.text, None, SymbolTableItem.TYPE_FUNCTION, returnValue)
     if no == -1:
         self.error(Error.ST_REPEATED_ID, id)
         return
     else:
         self.symbolTable.addIndex(id.text)
     # 函数开始处指令
     self.emit(Instruction(Instruction.ALC, 0))
     # 第一条指令已经确定地址,将地址传回符号表
     self.symbolTable.table[no].addr = self.instructionStream.no
     self.emit(Instruction(Instruction.STO, 'ret_addr'))
     # 如果有参数表,记得加入参数到符号表中
     parameter = Vn.findChild(const.PARAMETER)
     if parameter.hasChild(const.PARAMETER_LIST):
         parameterList = parameter.findChild(const.PARAMETER_LIST)
         ids = parameterList.findChildren(const.ID)
         # 设置函数符号的变量个数
         self.symbolTable.table[no].paraNum = len(ids)
         for id in ids:
             self.symbolTable.addItem(id.text, None, SymbolTableItem.TYPE_PARAMETER)
             self.emit(Instruction(Instruction.STO, id.text))
     else:
         return
コード例 #8
0
 def analyze_sentenceFunctionCall(self, overlookSet = [const.R_BRACE]):
     downOverlookSet = overlookSet[:]
     self.overlookSetAdd(downOverlookSet, const.R_PARENTHESIS)
     V_sentenceFunctionCall = VN.create(const.SENTENCE_FUNCTION_CALL)
     func = self.pointer
     V_sentenceFunctionCall.append(func)
     symbolFunc = self.symbolTable.getItem(self.pointer.text)
     if not symbolFunc:
         self.error(Error.ST_UNDEFINED_ID)
         self.overlookToMarks(overlookSet)
     else:
         if not symbolFunc.isFunction():
             self.error(Error.ST_UNDEFINED_ID)
             self.overlookToMarks(overlookSet)
     self.getsym()
     if self.pointer.isL_Parenthesis():
         V_sentenceFunctionCall.append(self.pointer)
         self.getsym()
         # 下一个如果不是右括号,才进入参数表分析
         if not self.pointer.isR_Parenthesis():
             paraValue = self.analyze_valueParameterList(downOverlookSet)
             paras = paraValue.findChildren(const.EXPRESSION)
             # 这里检查参数数量是否正确,但是出错也不要 overlook
             if len(paras) > symbolFunc.paraNum:
                 self.error(Error.ST_CALL_PARANUM_EXCEED, func)
             elif len(paras) < symbolFunc.paraNum:
                 self.error(Error.ST_CALL_PARANUM_LACK, func)
             V_sentenceFunctionCall.append(paraValue)
         if self.pointer.isR_Parenthesis():
             V_sentenceFunctionCall.append(self.pointer)
             self.emit(Instruction(Instruction.JSR, func.text))
             self.getsym()
         else:
             self.error(Error.GA_MISS_R_PARENTHESIS)
             self.overlookToMarks(overlookSet)
     else:
         self.error(Error.GA_MISS_L_PARENTHESIS, self.pointer.previous)
         self.overlookToMarks(overlookSet)
     # 函数调用语句结束记得给下一条语句加上标记
     self.instructionStream.setLab('ret_addr')
     return V_sentenceFunctionCall
コード例 #9
0
 def analyze_expression(self, overlookSet):
     downOverlookSet = overlookSet[:]
     V_expression = VN.create(const.EXPRESSION)
     plus = False
     # 如果开头就有符号,那么肯定是 -x 或者 +x ,默认操作数为0
     if self.pointer.isPlusOperator():
         plus = self.pointer
         V_expression.append(plus)
         self.getsym()
         # 操作数 0 入栈
         self.emit(Instruction(Instruction.LOC, '0', False))
     term = self.analyze_term(overlookSet)
     # 如果有操作符,那么生成加或减指令
     self.emit(Instruction.create(plus))
     V_expression.append(term)
     while self.pointer.isPlusOperator():
         plus = self.pointer
         self.getsym()
         term = self.analyze_term(overlookSet)
         V_expression.append(plus).append(term)
         self.emit(Instruction.create(plus))
     # 此处结束后没有检查!!!直接返回
     return self.checkEmpty(V_expression)
コード例 #10
0
 def run_sentenceReturn(self):
     if self.hasError():
         return
     self.emit(Instruction(Instruction.LOD, "ret_addr"))
     self.emit(Instruction(Instruction.BR, "ret_addr"))