def make_number(self): num_str = '' decimal_count = 0 while self.current_char is not None and self.current_char in DIGITS + '.': if self.current_char == '.': if decimal_count == 1: return Token(TT_INT, float(num_str)), 'error' decimal_count += 1 num_str += self.current_char self.advance() if decimal_count == 0: return Token(TT_INT, int(num_str)), None else: return Token(TT_FLOAT, float(num_str)), None
def getRelationalOperator(self): op = RelationalOperator tok = self.getNextToken() if (tok.getTokType() is TokenType.EQ_TOK): op = RelationalOperator.EQ_OP elif (tok.getTokType() is TokenType.NE_TOK): op = RelationalOperator.NE_OP elif (tok.getTokType() is TokenType.GT_TOK): op = RelationalOperator.GT_OP elif (tok.getTokType() is TokenType.GE_TOK): op = RelationalOperator.GE_OP elif (tok.getTokType() is TokenType.LT_TOK): op = RelationalOperator.LT_OP elif (tok.getTokType() is TokenType.LE_TOK): op = RelationalOperator.LE_OP else: raise ParserException("relational operator expected at row " + Token.getRowNumber() + "and column " + Token.getColumnNumber()) return op
def make_tokens(self): tokens = [] while self.current_char is not None: if self.current_char in ' \t': self.advance() # numbers elif self.current_char in DIGITS: pos_start = self.pos.copy() num, error = self.make_number() if error is not None: return [], IllegalNumberError(pos_start, self.pos, "'" + str(num) + "'") tokens.append(num) # operators elif self.current_char == '+': tokens.append(Token(TT_PLUS)) self.advance() elif self.current_char == '-': tokens.append(Token(TT_MINUS)) self.advance() elif self.current_char == '*': tokens.append(Token(TT_MUL)) self.advance() elif self.current_char == '/': tokens.append(Token(TT_DIV)) self.advance() # parens elif self.current_char == '(': tokens.append(Token(TT_LPAREN)) self.advance() elif self.current_char == ')': tokens.append(Token(TT_RPAREN)) self.advance() else: pos_start = self.pos.copy() char = self.current_char self.advance() return [], IllegalCharError(pos_start, self.pos, "'" + char + "'") return tokens, None
def getStatement(self): tok = self.getLookaheadToken() if(tok.getTokType() == TokenType.ID_TOK): s = self.getAssignmentStatement() elif (tok.getTokType() == TokenType.IF_TOK): s = self.getIfStatement() elif (tok.getTokType() == TokenType.PRINT_TOK): s = self.getPrintStatement() elif (tok.getTokType() == TokenType.FROM_TOK): s = self.getLoopStatement() else: #raise ParserException(tok.getTokType) raise ParserException(str(tok.getTokType()) + " statement is expected at row " + str(Token.getRowNumber()) + "and column " + str(Token.getColumnNumber())) return s