ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

让我们搞个编译器,第二章的python实现

2020-12-14 14:58:40  阅读:192  来源: 互联网

标签:#-------------------------------------------------------------- R0 Look python 



# 编译一位数四则运算
# 原教材 第二篇。
# 
# 若改为x86,则需要调整输出语句。同时R0可设为eax,R1设为edx。
# x86 也差不多式微了,苹果都抛弃了它。不知说什么好。
#--------------------------------------------------------------
"""program Cradle"""

#--------------------------------------------------------------
# Constant Declarations 

TAB = '    ● '
src="1+2*4+5-6*(1+2)/((-3+4)+(5-6))" # 源代码脚本
#src='1-2'
print(src+"\n")
#___________________________
class source():
    def __init__(self,f):
        self.file=f
        self.step=0
        self.size=len(f)
    def read(self):
        if self.step < self.size:
            rt=self.file[self.step]
            self.step+=1
            print(rt)
            return rt
        else:
            return None
src=source(src)
#--------------------------------------------------------------
# Variable Declarations 

Look=""  # char              # Lookahead Character 
                              
#--------------------------------------------------------------
# Read New Character From Input Stream 

def  GetChar():
   global Look
   Look=src.read()

#--------------------------------------------------------------
# Report an Error 

def  Error(s: str):

   WriteLn=print
   WriteLn('Error: ', s, '.')

#--------------------------------------------------------------
# Report Error and Halt 

def  Abort(s: str):

   Error(s)
   quit()

#--------------------------------------------------------------
# Report What Was Expected 

def  Expected(s: str):

   Abort(s + ' Expected')

#--------------------------------------------------------------
# Match a Specific Input Character 

def  Match(x: chr):

   if Look == x :
     GetChar ()
   else: Expected('☞ ' + x + ' ')

#--------------------------------------------------------------
# Recognize an Alpha Character 

def IsAlpha(c: chr): #boolean

   return c.isalpha()

#--------------------------------------------------------------

# Recognize a Decimal Digit 

def  IsDigit(c: chr): # boolean

     return c.isdigit()

#--------------------------------------------------------------
# Get an Identifier 

def  GetName(): #char

   if not IsAlpha(Look) :
       Expected('Name')
   rt= Look.upper()
   GetChar()
   return rt

#--------------------------------------------------------------
# Get a Number 

def  GetNum(): #char

   if not IsDigit(Look) :
       Expected('Integer')
   rt = Look
   GetChar()
   return rt

#--------------------------------------------------------------
# Output a String with Tab 

def  Emit(s: str):
   Write=print
   Write(TAB, s)

#--------------------------------------------------------------
# Output a String with Tab and CRLF 

def  EmitLn(s: str):

   Emit(s)
   WriteLn=print
   WriteLn()


#--------------------------------------------------------------
# Initialize 

def  Init():

   GetChar()

#------------------------------------------------------
#def  Expression(): # 2.1
#def Term(): # 2.2
#------------------------------------------------------
def  Factor():
   global Look
   if Look == '(' :
      Match ('(')
      Expression()
      Match(')' )
   else:
      EmitLn('#' + GetNum() + ' --> R0')

#--------------------------------------------------------------
def IsAddop(c: chr): # boolean
     return  c in ['+', '-']
     
def  Expression(): # 2  expression ::= term [op term]*
    global Look
    if IsAddop(Look) : 
      EmitLn('CLR D0')
    else:
      Term()
    while Look in ['+', '-'] : 
      EmitLn('R0 --> PUSH')

      if Look =='+': Add()
      elif Look == '-': Subtract()
      else: Expected('加法操作符')
#--------------------------------------------------------------

def  Add():

   Match('+')
   Term()
   #EmitLn('ADD D1,D0')
  
   EmitLn ("POP  --> R1" )     #('ADD (SP)+,R0')
   EmitLn ('ADD R1, R0 --> R0')
   # x86
   #       ADD R0, R1 --> R0
   
#--------------------------------------------------------------
def  Subtract():

   Match('-')
   Term()
   EmitLn('POP -->R1')  # ('SUB (SP)+,R0')
   EmitLn('SUB R1, R0 --> R0 ')
   EmitLn('NEG R0')
   #  x86
   #         SUB    R1, R0  --> R1
   #         MOV  R0 , R1 --> R0

#--------------------------------------------------------------
# Recognize and Translate a Multiply 

def  Multiply():

    Match('*')
    Factor()
    EmitLn("POP --> R1")         # ('MULS (SP)+,R0')
    EmitLn("MULS R1, R0  --> R0")         
    # x86 
    #        MUL R0,R1 --> R0
#--------------------------------------------------------------
def  Divide():

    Match('/')
    Factor()
    EmitLn('POP  --> R1')
    EmitLn('DIVS R1, R0 --> R0')
    # x86
    #      DIV  R1, R0 --> R1
    #      MOV  R0, R1 --> R0
#--------------------------------------------------------------
def  Term():
    global Look
    Factor()
    while Look in ['*', '/'] :
       EmitLn('R0 --> PUSH')
      
       if Look == '*': Multiply()
       elif Look == '/': Divide()
       else: Expected('Mulop')
#--------------------------------------------------------------      
Init()
Expression()

标签:#--------------------------------------------------------------,R0,Look,python,
来源: https://blog.csdn.net/m0_52427882/article/details/111170299

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有