130242014012+雷鸣=实验二

 

 

  

一、实验目的

1.熟悉体系结构的风格的概念

2.理解和应用管道过滤器型的风格。

3、理解解释器的原理

4、理解编译器模型

 

使用python来实现这个功能呢,核心的理念就是递归

130242014012+雷鸣=实验二_第1张图片

130242014012+雷鸣=实验二_第2张图片

130242014012+雷鸣=实验二_第3张图片

  1 INTEGER, PLUS, MINUS, MUL, DIV, LPAREN, RPAREN, EOF = (
  2     'INTEGER', 'PLUS', 'MINUS', 'MUL', 'DIV', 'LPAREN', 'RPAREN', 'EOF')
  3 
  4 
  5 class Token:
  6     def __init__(self, type, value):
  7         self.type = type
  8         self.value = value
  9 
 10     def __str__(self):
 11         return 'Token(%s, %s)' % (self.type, self.value)
 12 
 13     __repr__ = __str__
 14 
 15 
 16 class Lexer:
 17     def __init__(self, text):
 18         self.text = text.strip()
 19         self.pos = 0
 20         self.current_char = self.text[0]
 21 
 22     def error(self):
 23         raise Exception('Invalid character')
 24 
 25     def step_forward(self):
 26         self.pos += 1
 27         while self.pos < len(self.text):
 28             if self.text[self.pos].isspace():
 29                 self.pos += 1
 30                 continue
 31             else:
 32                 self.current_char = self.text[self.pos]
 33                 return
 34         self.current_char = None
 35 
 36     def get_next_token(self):
 37         if self.current_char == None:
 38             return Token(EOF, None)
 39 
 40         if self.current_char.isdigit():
 41             value = ''
 42             while True:
 43                 value += self.current_char
 44                 self.step_forward()
 45                 if (self.current_char == None) or (not self.current_char.isdigit()):
 46                     return Token(INTEGER, value)
 47 
 48         if self.current_char == '+':
 49             self.step_forward()
 50             return Token(PLUS, '+')
 51 
 52         if self.current_char == '-':
 53             self.step_forward()
 54             return Token(MINUS, '-')
 55 
 56         if self.current_char == '*':
 57             self.step_forward()
 58             return Token(MUL, '*')
 59 
 60         if self.current_char == '/':
 61             self.step_forward()
 62             return Token(DIV, '/')
 63 
 64         if self.current_char == '(':
 65             self.step_forward()
 66             return Token(LPAREN, '(')
 67 
 68         if self.current_char == ')':
 69             self.step_forward()
 70             return Token(RPAREN, ')')
 71 
 72         self.error()
 73 
 74 
 75 class Interpreter:
 76     def __init__(self, lexer):
 77         self.lexer = lexer
 78         self.current_token = self.lexer.get_next_token()
 79 
 80     def error(self):
 81         raise Exception('Invalid Syntax')
 82 
 83     def check_token(self, token_type):
 84         if self.current_token.type == token_type:
 85             self.current_token = self.lexer.get_next_token()
 86         else:
 87             self.error()
 88 
 89     def factor(self):
 90         token = self.current_token
 91 
 92         if token.type == INTEGER:
 93             self.check_token(INTEGER)
 94             return float(token.value)
 95         elif token.type == LPAREN:
 96             self.check_token(LPAREN)
 97             result = self.expr()
 98             self.check_token(RPAREN)
 99             return float(result)
100 
101         self.error()
102 
103     def term(self):
104         result = self.factor()
105         while self.current_token.type in (MUL, DIV):
106             if self.current_token.type == MUL:
107                 self.check_token(MUL)
108                 result *= self.factor()
109             elif self.current_token.type == DIV:
110                 self.check_token(DIV)
111                 result /= self.factor()
112 
113         return float(result)
114 
115     def expr(self):
116         result = self.term()
117         while self.current_token.type in (PLUS, MINUS):
118             if self.current_token.type == PLUS:
119                 self.check_token(PLUS)
120                 result += self.term()
121             elif self.current_token.type == MINUS:
122                 self.check_token(MINUS)
123                 result -= self.term()
124         return float(result)
125 
126 
127 if __name__ == '__main__':
128     while True:
129         try:
130             text = input('Calc--> ')
131             if text:
132                 interpreter = Interpreter(Lexer(text))
133                 result = interpreter.expr()
134                 if interpreter.current_token.type == EOF:
135                     print(result)
136                 else:
137                     raise Exception('Invalid Syntax')
138         except Exception as e:
139             print(e)
140             continue

运行结果如下:

130242014012+雷鸣=实验二_第4张图片

 

 还可以

你可能感兴趣的:(130242014012+雷鸣=实验二)