# Character constant 'c' or L'c' t_CCONST = r'(L)?\'([^\\\n]|(\\.))*?\'' # Comments def t_comment_1(t): r' /\*(.|\n)*?\*/' t.lineno += t.value.count('\n') def t_comment_2(t): r' //.*\n' t.lineno += 1 # Preprocessor directive (ignored) def t_preprocessor(t): r'\#(.)*?\n' t.lineno += 1 def t_error(t): print "Illegal character %s" % repr(t.value[0]) t.skip(1) lexer = lex.lex(optimize=1) if __name__ == "__main__": lex.runmain(lexer)
r'\n+' t.lexer.lineno += len(t.value) # A string containing ignored characters (spaces and tabs) t_ignore = ' \t' # Error handling rule def t_error(t): print "BoxesLexerError: Illegal character '%s'" % t.value[0] t.lexer.skip(1) exit(1) lexer = lex.lex() if __name__ == "__main__": lex.runmain(lexer) #LEXER SOLO # Build the lexer #lexer = lex.lex() # Test it out #data = ''' #program hola_2; #var hola_1,hola_2:float; #hola_3,hola_4:int; #{ # prueba_prueba=10; # if(7>9){ # prueba_prueba2=5;{{{{{{ # }else{
r'/\*(.|\n)*?\*/' t.lexer.lineno += t.value.count('\n') # Preprocessor directive (ignored) def t_preprocessor(self, t): r'\#(.)*?\n' t.lexer.lineno += 1 def t_error(self, t): print("Illegal character %s" % repr(t.value[0])) t.lexer.skip(1) # use class method, supply build 使用类的方法需要解决的问题 def build(self, **kwargs): self.lexer = lex.lex(module=self, **kwargs) def test(self, data): self.lexer.input(data) while True: tok = lexer.token() if not tok: break print tok if __name__ == "__main__": # a simple unit test 一个简单的单元测试 cm = {} mylex = clex(cm) #mylex.build(lextab="clextab") lex.runmain(mylex.lexer) print(cm)
# Build the lexer lexer = lex.lex() ############################################################################### if __name__ == '__main__': import sys import os if len(sys.argv) == 1: print "Just handle one simple verilog netlist in os.get_cwd() dir" while (1): fname = raw_input("plz enter the file name:") console = sys.stdout lexerlog = open(os.path.splitext(fname)[0] + "_lexer.log", 'w') sys.stdout = lexerlog with open(fname, 'r') as fobj: all_lines = fobj.read() lex.runmain(lexer, all_lines) sys.stdout = console lexerlog.close() print "Note: %s_lexer.log has been generated succfully " % fname #elif sys.argv[1]=='many': # parent_dir=os.getcwd() # while(1): # tmp1=raw_input('Plz enter the verilog source sub dir:') # input_file_dir=parent_dir+"\\test_input_netlist\\"+tmp1 # if os.path.exists(input_file_dir)==False: # print 'Error : this dir dont exists!' # continue # else: # break # for eachFile in os.listdir(input_file_dir): # print eachFile
# Build the lexer lexer = lex.lex() ############################################################################### if __name__=='__main__': import sys import os if len(sys.argv)==1: print "Just handle one simple verilog netlist in os.get_cwd() dir" while(1): fname=raw_input("plz enter the file name:") console = sys.stdout lexerlog = open(os.path.splitext(fname)[0]+"_lexer.log",'w') sys.stdout = lexerlog with open(fname,'r') as fobj: all_lines=fobj.read() lex.runmain(lexer,all_lines) sys.stdout = console lexerlog.close() print "Note: %s_lexer.log has been generated succfully " % fname #elif sys.argv[1]=='many': # parent_dir=os.getcwd() # while(1): # tmp1=raw_input('Plz enter the verilog source sub dir:') # input_file_dir=parent_dir+"\\test_input_netlist\\"+tmp1 # if os.path.exists(input_file_dir)==False: # print 'Error : this dir dont exists!' # continue # else: # break # for eachFile in os.listdir(input_file_dir): # print eachFile