-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexam4.py
executable file
·81 lines (61 loc) · 1.34 KB
/
exam4.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# -*- coding: utf-8 -*-
# exam4.py
import ply.lex as lex
import ply.yacc as yacc
tokens = ['NUMBER','TOKHEAT','STATE','TOKTARGET','TOKTEMPRATURE']
def t_NUMBER(t):
r'[0-9]+'
return t;
def t_TOKHEAT(t):
r'heat'
return t
def t_STATE(t):
r'on|off'
return t
def t_TOKTARGET(t):
r'target'
return t
def t_TOKTEMPRATURE(t):
r'temprature'
return t
# 不做处理的符号 空格与tab
t_ignore = " \t"
# 行号统计
def t_newline(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
# 出错处理
def t_error(t):
print "Illegal character '%s'" % t.value[0]
t.lexer.skip(1)
def p_commands(p):
'''commands : empty
| commands command
'''
def p_command(p):
'''command : heat_switch
| target_set'''
def p_heatswitch(p):
'heat_switch : TOKHEAT STATE'
print "Heat turned " + p[2]
def p_targetset(p):
'target_set : TOKTARGET TOKTEMPRATURE NUMBER'
print "temprature set " + p[3]
def p_empty(p):
'empty :'
pass
# Error rule for syntax errors
def p_error(p):
print "Syntax error in input!"
# Build the lexer
lexer = lex.lex()
# Build the parser
parser = yacc.yacc()
while True:
try:
s = raw_input('input > ')
except EOFError:
break
if not s: continue
result = parser.parse(s)