-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquestion_lex.py
More file actions
76 lines (61 loc) · 1.13 KB
/
Copy pathquestion_lex.py
File metadata and controls
76 lines (61 loc) · 1.13 KB
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
68
69
70
71
72
73
74
75
76
import ply.lex as lex
import re
tokens = ( 'SECTION',
'IDENTIFIER',
'STRING',
'LBRACE',
'RBRACE',
'SEMI',
'EQU',
'TRUE',
'FALSE' )
def t_SECTION(t):
r'section'
return t
def t_TRUE(t):
r'(true)'
t.value = True
return t
def t_FALSE(t):
r'(false)'
t.value = False
return t
def t_IDENTIFIER(t):
r'[a-zA-Z\-0-9]+'
return t
def t_STRING(t):
r'(\".*\"|\'.*\')'
t.value = t.value[1:-1]
return t
def t_LBRACE(t):
r'{'
return t
def t_EQU(t):
r'='
return t
def t_RBRACE(t):
r'}'
return t
def t_SEMI(t):
r';'
return t
def t_NEWLINE(t):
r'\n+'
t.lexer.lineno += len(t.value)
return t
t_ignore = ' \t\n'
# Error handling rule
def t_error(t):
print("Illegal character '{0}' at line {1}".format(t.value[0], t.lineno))
t.lexer.skip(1)
f = open('paragraph.txt','r')
i = f.read()
lexer = lex.lex()
lexer.input(i)
while True:
tok = lexer.token()
if not tok:
break # No more input
if not tok is "IDENTIFIER":
print(tok)
f.close()