Mercurial > repo
comparison ply-3.8/example/calceof/calc.py @ 7267:343ff337a19b
<ais523> ` tar -xf ply-3.8.tar.gz
author | HackBot |
---|---|
date | Wed, 23 Mar 2016 02:40:16 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
7266:61a39a120dee | 7267:343ff337a19b |
---|---|
1 # ----------------------------------------------------------------------------- | |
2 # calc.py | |
3 # | |
4 # A simple calculator with variables. Asks the user for more input and | |
5 # demonstrates the use of the t_eof() rule. | |
6 # ----------------------------------------------------------------------------- | |
7 | |
8 import sys | |
9 sys.path.insert(0,"../..") | |
10 | |
11 if sys.version_info[0] >= 3: | |
12 raw_input = input | |
13 | |
14 tokens = ( | |
15 'NAME','NUMBER', | |
16 ) | |
17 | |
18 literals = ['=','+','-','*','/', '(',')'] | |
19 | |
20 # Tokens | |
21 | |
22 t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' | |
23 | |
24 def t_NUMBER(t): | |
25 r'\d+' | |
26 t.value = int(t.value) | |
27 return t | |
28 | |
29 t_ignore = " \t" | |
30 | |
31 def t_newline(t): | |
32 r'\n+' | |
33 t.lexer.lineno += t.value.count("\n") | |
34 | |
35 def t_eof(t): | |
36 more = raw_input('... ') | |
37 if more: | |
38 t.lexer.input(more + '\n') | |
39 return t.lexer.token() | |
40 else: | |
41 return None | |
42 | |
43 def t_error(t): | |
44 print("Illegal character '%s'" % t.value[0]) | |
45 t.lexer.skip(1) | |
46 | |
47 # Build the lexer | |
48 import ply.lex as lex | |
49 lex.lex() | |
50 | |
51 # Parsing rules | |
52 | |
53 precedence = ( | |
54 ('left','+','-'), | |
55 ('left','*','/'), | |
56 ('right','UMINUS'), | |
57 ) | |
58 | |
59 # dictionary of names | |
60 names = { } | |
61 | |
62 def p_statement_assign(p): | |
63 'statement : NAME "=" expression' | |
64 names[p[1]] = p[3] | |
65 | |
66 def p_statement_expr(p): | |
67 'statement : expression' | |
68 print(p[1]) | |
69 | |
70 def p_expression_binop(p): | |
71 '''expression : expression '+' expression | |
72 | expression '-' expression | |
73 | expression '*' expression | |
74 | expression '/' expression''' | |
75 if p[2] == '+' : p[0] = p[1] + p[3] | |
76 elif p[2] == '-': p[0] = p[1] - p[3] | |
77 elif p[2] == '*': p[0] = p[1] * p[3] | |
78 elif p[2] == '/': p[0] = p[1] / p[3] | |
79 | |
80 def p_expression_uminus(p): | |
81 "expression : '-' expression %prec UMINUS" | |
82 p[0] = -p[2] | |
83 | |
84 def p_expression_group(p): | |
85 "expression : '(' expression ')'" | |
86 p[0] = p[2] | |
87 | |
88 def p_expression_number(p): | |
89 "expression : NUMBER" | |
90 p[0] = p[1] | |
91 | |
92 def p_expression_name(p): | |
93 "expression : NAME" | |
94 try: | |
95 p[0] = names[p[1]] | |
96 except LookupError: | |
97 print("Undefined name '%s'" % p[1]) | |
98 p[0] = 0 | |
99 | |
100 def p_error(p): | |
101 if p: | |
102 print("Syntax error at '%s'" % p.value) | |
103 else: | |
104 print("Syntax error at EOF") | |
105 | |
106 import ply.yacc as yacc | |
107 yacc.yacc() | |
108 | |
109 while 1: | |
110 try: | |
111 s = raw_input('calc > ') | |
112 except EOFError: | |
113 break | |
114 if not s: continue | |
115 yacc.parse(s+'\n') |