Exemple #1
0
 def scan(self, pif: PIF, sym_table_identifiers: SymbolTable, sym_table_constants):
     global GLOBAL_ID
     f = open(self.file_path)
     number = 0
     while True:
         line = f.readline()
         if not line:
             break
         line = line[:-1]
         tokens = self.detect_tokens(line)
         print(tokens)
         for tok in tokens:
             if self.is_reserved_word_or_operator(tok):
                 pif = self.genPif(tok, 0, pif)
             elif self.is_identifier(tok):
                 id = sym_table_identifiers.add_value(tok).id
                 pif = self.genPif(tok, id, pif)
             elif self.is_numerical_constant(tok) or self.is_string_constant(tok) or self.is_boolean_constant(tok):
                 id = sym_table_constants.add_value(tok).id
                 pif = self.genPif(tok, id, pif)                    
             else:
               
                 raise ValueError('lexical error at line: ' + str(number) + ' from token: ' + tok)
         number+=1
     # if the function returns without any error, it means that the program is lexically correct
     return pif, sym_table_identifiers, sym_table_constants