lex_state_try.py (690B)
1# lex_state2.py 2# 3# Declaration of a state for which no rules are defined 4 5import sys 6sys.path.insert(0,"..") 7 8import ply.lex as lex 9 10tokens = [ 11 "PLUS", 12 "MINUS", 13 "NUMBER", 14 ] 15 16comment = 1 17states = (('comment', 'exclusive'),) 18 19t_PLUS = r'\+' 20t_MINUS = r'-' 21t_NUMBER = r'\d+' 22 23t_ignore = " \t" 24 25# Comments 26def t_comment(t): 27 r'/\*' 28 t.lexer.begin('comment') 29 print "Entering comment state" 30 31def t_comment_body_part(t): 32 r'(.|\n)*\*/' 33 print "comment body", t 34 t.lexer.begin('INITIAL') 35 36def t_error(t): 37 pass 38 39t_comment_error = t_error 40t_comment_ignore = t_ignore 41 42import sys 43 44lex.lex() 45 46data = "3 + 4 /* This is a comment */ + 10" 47 48lex.runmain(data=data)