def readCharByChar(inputfile,outputfile): global RC_counter #--------posicionar el contador inputfile.seek(syntacticMachine.char_counter) # lectura del fichero en un solo caracter. La lectura no avanza el cursor. (char_counter) # para registrar la lectura se llamará a syntacticMachine.leer() c =inputfile.read(1) if c is '': #EOF tokenG = genToken(outputfile,"EOF","") syntacticMachine.stop_reading_file() #¡¡esto solo es para EOF!! return tokenG elif c is '\n': tokenG = genToken(outputfile,"CR","") #DELIMITADOR syntacticMachine.leer() RC_counter +=0.5 #para corregir el doble RC if c is '\n': syntacticMachine.leer() RC_counter +=0.5 return tokenG elif c is '{': tokenG = genToken(outputfile,"K1","") #LLAVE APERTURA syntacticMachine.leer() return tokenG elif c is '}': tokenG = genToken(outputfile,"K2","") #LLAVE CIERRE syntacticMachine.leer() return tokenG elif c is '!': tokenG = genToken(outputfile,"NOP","") #OPERADOR NEGACIÓN syntacticMachine.leer() return tokenG elif c is '+': #OPERADOR SUMA tokenG = genToken(outputfile,"ADD","") syntacticMachine.leer() return tokenG elif c is '=': syntacticMachine.leer() c = inputfile.read(1) if c is '=': tokenG = genToken(outputfile,"EOP", "") #COMPARACION syntacticMachine.leer() return tokenG else: tokenG = genToken(outputfile,"ASIG", "") #ASIGNACION return tokenG elif c is '-': syntacticMachine.leer() c = inputfile.read(1) if c is '-': error("LEXIC",RC_counter,"PRE-DECREMENT IS NOT RECOGNISED BY THIS PROGRAM VERSION") else: tokenG = genToken(outputfile,"PRE", "") #PREDECREMENTO return tokenG elif c is '"': #STRING (O CADENA) pal = '' fin = False while True: pal = pal + c c = inputfile.read(1) syntacticMachine.leer() if c is '\n': error("LEXIC",RC_counter,"STR: Expected closing '\"'") fin = True break if c is '"': break if not fin: pal = pal + c tokenG = genToken(outputfile,"STR", pal) syntacticMachine.leer() return tokenG elif c is '/': #COMENTARIO syntacticMachine.leer() c = inputfile.read(1) if c is '/': pal = '/' while c is not '\n': pal = pal + c c = inputfile.read(1) syntacticMachine.leer() return tokenG else: error("LEXIC",RC_counter,"COM: Expected '//'") elif c.isdigit(): #ENTERO d = 0 fail_digit = False while c.isdigit() or c is ',' or c is '.': if c is '.' or c is ',': d = d*10 + 0 fail_digit = True else: d = d*10 + int(c) c = inputfile.read(1) syntacticMachine.leer() if fail_digit: error("LEXIC",RC_counter,"INT: Expected Integer") else: tokenG = genToken(outputfile,"INT", d) return tokenG elif c.isalpha(): #PALABRA RESERVADA pal = '' #IDENTIFICADOR while c.isalnum() or c is '_': pal = pal + c c = inputfile.read(1) syntacticMachine.leer() if pal in palabras_reservadas: tokenG = genToken(outputfile,"RW", pal) return tokenG else: tokenG = genToken(outputfile,"ID", pal) return tokenG else: #Aqui vienen los espacios en blanco syntacticMachine.leer()
def readCharByChar(self): global RC_counter self.inputfile.seek(syntacticMachine.char_counter) c = self.inputfile.read(1) #un solo carácter if c is '': #EOF token2 = self.genToken("EOF","") syntacticMachine.stop_reading_file() #¡¡esto solo es para EOF!! return token2 elif c is '\n': token2 = self.genToken("RC","") #DELIMITADOR syntacticMachine.next() RC_counter +=0.5 return token2 elif c is '{': token2 = self.genToken("LLA","") #LLAVE APERTURA syntacticMachine.next() return token2 elif c is '}': token2 = self.genToken("LLC","") #LLAVE CIERRE syntacticMachine.next() return token2 elif c is '!': token2 = self.genToken("NEG","") #OPERADOR NEGACIÓN syntacticMachine.next() return token2 elif c is '+': #OPERADOR SUMA token2 = self.genToken("SUMA","") syntacticMachine.next() return token2 elif c is '=': syntacticMachine.next() c = self.inputfile.read(1) if c is '=': token2 = self.genToken("COMP", "") #COMPARACION syntacticMachine.next() return token2 else: token2 = self.genToken("ASIG", "") #ASIGNACION return token2 elif c is '-': syntacticMachine.next() c = self.inputfile.read(1) if c is '-': token2 = self.genToken("PRE", "") #PREDECREMENTO syntacticMachine.next() return token2 else: error("LEXIC",RC_counter,"PRE: Expected '-'") elif c is '"': #STRING (O CADENA) pal = '' fin = False while True: pal = pal + c c = self.inputfile.read(1) syntacticMachine.next() if c is '\n': error("LEXIC",RC_counter,"STR: Expected closing '\"'") fin = True break if c is '"': break if not fin: pal = pal + c token2 = self.genToken("STR", pal) syntacticMachine.next() return token2 elif c is '/': #COMENTARIO syntacticMachine.next() c = self.inputfile.read(1) if c is '/': pal = '/' while c is not '\n': pal = pal + c c = self.inputfile.read(1) syntacticMachine.next() token2 = self.genToken("COM", pal) return token2 else: error("LEXIC",RC_counter,"COM: Expected '//'") elif c.isdigit(): #ENTERO d = 0 fail_digit = False while c.isdigit() or c is ',' or c is '.': if c is '.' or c is ',': d = d*10 + 0 fail_digit = True else: d = d*10 + int(c) c = self.inputfile.read(1) syntacticMachine.next() if fail_digit: error("LEXIC",RC_counter,"INT: Expected Integer") else: token2 = self.genToken("INT", d) return token2 elif c.isalpha(): #PALABRA RESERVADA pal = '' #IDENTIFICADOR while c.isalnum() or c is '_': pal = pal + c c = self.inputfile.read(1) syntacticMachine.next() if pal in palabras_reservadas: token2 = self.genToken("PR", pal) return token2 else: token2 = self.genToken("ID", pal) return token2 else: #Aqui vienen los espacios en blanco syntacticMachine.next()