lex を試す。複数のクラスを記述
一つのファイルに複数のクラスを記述
#!/usr/bin/env python import ply.lex as lex class MyLexer1: tokens = ( 'A', 'PLUS', ) t_A = r'a' t_PLUS = r'\+' def t_error(self,t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) # Build the lexer def build(self,**kwargs): self.lexer = lex.lex(object=self, **kwargs) # Test it output def test(self,data): self.lexer.input(data) while 1: tok = self.lexer.token() if not tok: break print tok class MyLexer2: tokens = ( 'B', 'PLUS', ) t_B = r'b' t_PLUS = r'\+' def t_error(self,t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) # Build the lexer def build(self,**kwargs): self.lexer = lex.lex(object=self, **kwargs) # Test it output def test(self,data): self.lexer.input(data) while 1: tok = self.lexer.token() if not tok: break print tok # Build the lexer and try it out m1 = MyLexer1() m1.build() m1.test("a+a") m2 = MyLexer2() m2.build() m2.test("a+a")
で、
./20080324_ply00.py:37: Rule t_PLUS redefined. Previously defined on line 12 ./20080324_ply00.py:39: Rule t_error redefined. Previously defined on line 14
とエラーが出てダメだった。
クラス内だけを見るようになっていないの?(←ソース見ろ)