My elif/else doesnt work

I am an intermediate python programmer, and then i wanted to take it a a step further by making my own programming language, so i was making the lexical analysis and for some reason, the elif/else doesnt work because of expected expression, can anyone help? This is the code

###########

# \Tokens/

############

########

# ERRORS

########

class Error:

    def __init__(self, error_name, details):

     self.error_name = error_name

     self.details = details

     def as_string(self):

         result = f'{self.error_name}: self.details'

         return result

        ##Errors##

         class IllegalCharError(Error):

            def __init__(self, details):

                super().__init__('Illegal Character', details)

               

DIGITS = '0123456789'

TT_INT = 'TT_INT'

TT_FLOAT = 'TT_FLOAT'

TT_PLUS = 'PLUS'

TT_MINUS = 'MINUS'

TT_MULTI = 'MULTI'

TT_DIVID = 'DIVID'

TT_RPT = 'RPT'

TT_LPT = 'LPT'

class Token:

     def __init__(self, type_, value=None):

         self.type = type_

         self.value = value

         

         def __repr__(self, type_, value):

             if self.value: return f'{self.type}:{self.value}'

             return f'{self.type}'

                      

             ##LEXER

             

             class Lexer:

                 def __init__(self, text):

                     self.text = text

                     self.pos = -1

                     self.current_char = None

                     self.advance( )

                     def advance(self):

                         self.pos += 1

                         self.current_char = self.text[self.pos] if self.pos < len(self.Text) else None

                         

                         def make_tokens(self):

                             tokens = []

                             

                             while self.current_char != none:

                                 if self.current_char in '/tab':

                                     self.advance( )

                                 elif self.currrent_char in DIGITS :

                                      tokens.append(self.make_number)

                                 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_MULTI))

                             self.advance( )

                         elif self.current_char == '/':

                         tokens.append(token(TT_DIVID))

                         self.advance()

                         elif self.current_char == ')':

                         tokens.append(token(TT_LPT))

                         self.advance( )

                         elif self.current_char == '(':

                         tokens.append(token(TT_RPT))

                         self.advance( )

                 else:

                 char = self.current_char

                 self.advance( )

                 return [], IllegalCharError("'", char, "'")

                 return tokens, None    

                                                                                                                                                 

                 return token                                        

                 def make_number(self):

                         num_string = ' '

                         period_count = 0

                         while self.current_char != None and self.current_char in DIGITS:

                             if self.current_char == '.':

                                 if period_count == 1: break

                                 period_count += 1  

                                 else:          

                                 num_string += self.current_char

                                 

                                 def run (text):

                                     lexer = Lexer()

                                     tokens, error = lexer.make_tokens

                                     return tokens, error

I don’t if its the editor/IDE you use, or that something went wrong when you attempted to post your code

if the former, take some time to over your editor settings

if the latter, please re-post your code (or use something repl.it).

The indention looks off almost everywhere, nested methods, classes and other blocks in a lot of places

i fixed the indentation

Now the problem is (i use pylance and vscode) 5 elifs have the expected expression error even though its correct

Assuming you did, even looking at this part of your code:


class Error:

    def __init__(self, error_name, details):

     self.error_name = error_name

     self.details = details

     def as_string(self):

         result = f'{self.error_name}: self.details'

         return result

        ##Errors##

         class IllegalCharError(Error):

            def __init__(self, details):

                super().__init__('Illegal Character', details)

               

the as_string method is nested in your __init__ method, the whole IllegalCharError is then nested with your as_string method.

Go ahead and try to instantiate IllegalCharError class. You will find it very challenging

the elif syntax error thrown by the interpreter is just the tip of the iceberg

1 Like