# interpreter for sqrt(4) or sqrt(1+3) -> 2 # should not fail if…
Question # interpreter for sqrt(4) or sqrt(1+3) -> 2 # should not fail if… # interpreter for sqrt(4) or sqrt(1+3) -> 2# should not fail if sqrt(4) + 2 -> 4import mathclass TokenType(): # single-character token types PLUS = ‘+’ MINUS = ‘-‘ MUL = ‘*’ DIV = ‘/’ LPAREN = ‘(‘ RPAREN = ‘)’ COMMA = ‘,’ PROGRAM = ‘PROGRAM’ INTEGER = ‘INTEGER’ FLOAT = ‘FLOAT’ PROCEDURE = ‘PROCEDURE’ ID = ‘ID’ EOF = ‘EOF’ SQRT = ‘SQRT’class Token(object): def __init__(self, type, value): self.type = type self.value = value def __str__(self): return ‘Token({type}, {value})’.format( type=self.type, value=repr(self.value) ) def __repr__(self): return self.__str__()class Interpreter(object): def __init__(self, text): self.text = text self.pos = 0 self.current_token = None def error(self): raise Exception(‘Error parsing input’) def get_next_token(self): text = self.text if self.pos > len(text) – 1: return Token(TokenType.EOF, None) self.current_char = text[self.pos] if self.current_char.isdigit(): token = Token(TokenType.INTEGER, int(self.current_char)) self.pos += 1 return token if self.current_char == ‘+’: token = Token(TokenType.PLUS, self.current_char) self.pos += 1 return token if self.current_char.isalpha(): value = self._id() token = Token(TokenType.SQRT, value) return token if self.current_char == ‘(‘: token = Token(TokenType.LPAREN, self.current_char) self.pos += 1 return token if self.current_char == ‘)’: token = Token(TokenType.RPAREN, self.current_char) self.pos += 1 return token self.error() def eat(self, token_type): if self.current_token.type == token_type: self.current_token = self.get_next_token() else: self.error() def squart_expr(self): sqaurt = self.current_token if(sqaurt.value !=’sqrt’): self.error() self.eat(TokenType.SQRT) lparen = self.current_token self.eat(TokenType.LPAREN) value_token = self.current_token value = value_token.value self.eat(TokenType.INTEGER) next_token = self.current_token if(next_token.type == TokenType.PLUS): self.eat(TokenType.PLUS) value += self.current_token.value self.eat(TokenType.INTEGER) rparen = self.current_token self.eat(TokenType.RPAREN) result = math.sqrt(value) return result def expr(self): self.current_token = self.get_next_token() if(self.current_token.type == TokenType.SQRT): return self.squart_expr() left = self.current_token self.eat(TokenType.INTEGER) op = self.current_token self.eat(TokenType.PLUS) right = self.current_token self.eat(TokenType.INTEGER) result = left.value + right.value return result def _id(self): token = Token(type=None, value=None) value = ” while self.current_char is not None and self.current_char.isalpha(): value += self.current_char self.advance() return value def advance(self): self.pos += 1 if self.pos > len(self.text) – 1: self.current_char = None # Indicates end of input else: self.current_char = self.text[self.pos]def main(): while True: try: text = input(‘calc> ‘) except EOFError: break if not text: continue interpreter = Interpreter(text) result = interpreter.expr() print(result)if __name__ == ‘__main__’: main() Computer Science Engineering & Technology Python Programming CS 455 Share QuestionEmailCopy link Comments (0)


