-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexam2.py
executable file
·67 lines (49 loc) · 1000 Bytes
/
exam2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
# -*- coding: utf-8 -*-
import ply.lex as lex
tokens = ["WORD","FILENAME","QUOTE","OBRACE","EBRACE","SEMICOLON"]
def t_WORD(t):
r'[a-zA-Z][a-zA-Z0-9-]*'
print "WORD ",
return t
def t_FILENAME(t):
r'[a-zA-Z0-9/.-]+'
print "FILENAME ",
return t
def t_QUOTE(t):
r'"'
print "QUOTE ",
return t
def t_OBRACE(t):
r'{'
print "OBRACE ",
return t
def t_EBRACE(t):
r'}'
print "EBRACE ",
return t
def t_SEMICOLON(t):
r';'
print "SEMICOLON ",
return t
# 不做处理的符号 空格与tab
t_ignore = " \t"
# 行号统计
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
print ''
# 出错处理
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
file_object = open('test.conf')
s = file_object.read()
print s
# Give the lexer some input
lexer.input(s)
while True:
tok = lexer.token()
if not tok: break