class jZeroLexer: """ @Author: Adam Miller @Course: CS430 - Language Translators @Professor: Dr. Burris """ #grammar defined letter letter = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' #grammar defined digit digit = '0123456789' #grammar defined ops ops = '*/=<>,;+-(){}!' #symbolic white space wSpace = ' \n \t' #table to drive the lexical analyzer PDA lexTable = [ [5 , 3, 2, 7, 11, 14, 0, 17, 20, 21, 22, 23, 24, 25, 26, 27, 28], [1 , 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2 , 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [4 , 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [4 , 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [5 , 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [6 , 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [10, 10, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [8 , 8, 9, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [8 , 8, 8, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10], [12, 12, 12, 12, 13, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12], [12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12], [13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13], [15, 15, 15, 15, 16, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15], [15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15], [16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16], [18, 18, 18, 18, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18], [18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18], [19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19], [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20], [21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21], [22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22], [23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23], [24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24], [25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25], [26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26], [27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27], [ 1, 1, 1, 1, 29, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29], ] #lexTable column index relations lexRelations= {"letter":0,"digit":1,"*":2,"/":3,"=":4,"<":5,"wSpace":6,">":7, ",":8,";":9,"+":10,"-":11,"(":12,")":13,"{":14,"}":15,"!":16} lexFinalStates = [1,2,4,6,10,12,13,15,16,18,19,20,21,22,23,24,25,26,27,29] def __init__(self, fileName): """open a file and initialize the lexical analyzer""" self.f = open(fileName, 'r') self.charBuffer = [] self.lexState = 0 def scan(self): """read in line by line and then one character at a time""" for line in self.f.readlines(): for c in line: #let table drive state transitions if self.lexState not in self.lexFinalStates: self.lexState = self.lexTable[self.lexState][self.lex(c)] #add current character to the buffer if c not in self.wSpace: self.charBuffer.append(c) # 1 is the error state if self.lexState == 1: print "Error" self.lexState = 0 self.charBuffer = [] #check to see if current state is a final state elif self.lexState in self.lexFinalStates: print ''.join(self.charBuffer) self.lexState = 0 self.charBuffer = [] #print self.lexState #for debugging """print final character buffer because the loop will end execution and not let the last success state be checked""" print ''.join(self.charBuffer) def lex(self, c): """return the correct relational index to the type of c""" if c in self.letter: return self.lexRelations["letter"] elif c in self.digit: return self.lexRelations["digit"] elif c in self.wSpace: return self.lexRelations["wSpace"] elif c in self.ops: return self.lexRelations[c] else: return 0 fn = raw_input("Please enter file name: ") lexer = jZeroLexer(fn) lexer.scan()