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