Mercurial > repo
comparison ply-3.8/test/yacc_unicode_literals.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 # yacc_unicode_literals | |
3 # | |
4 # Test for unicode literals on Python 2.x | |
5 # ----------------------------------------------------------------------------- | |
6 from __future__ import unicode_literals | |
7 | |
8 import sys | |
9 | |
10 if ".." not in sys.path: sys.path.insert(0,"..") | |
11 import ply.yacc as yacc | |
12 | |
13 from calclex import tokens | |
14 | |
15 # Parsing rules | |
16 precedence = ( | |
17 ('left','PLUS','MINUS'), | |
18 ('left','TIMES','DIVIDE'), | |
19 ('right','UMINUS'), | |
20 ) | |
21 | |
22 # dictionary of names | |
23 names = { } | |
24 | |
25 def p_statement_assign(t): | |
26 'statement : NAME EQUALS expression' | |
27 names[t[1]] = t[3] | |
28 | |
29 def p_statement_expr(t): | |
30 'statement : expression' | |
31 print(t[1]) | |
32 | |
33 def p_expression_binop(t): | |
34 '''expression : expression PLUS expression | |
35 | expression MINUS expression | |
36 | expression TIMES expression | |
37 | expression DIVIDE expression''' | |
38 if t[2] == '+' : t[0] = t[1] + t[3] | |
39 elif t[2] == '-': t[0] = t[1] - t[3] | |
40 elif t[2] == '*': t[0] = t[1] * t[3] | |
41 elif t[2] == '/': t[0] = t[1] / t[3] | |
42 | |
43 def p_expression_uminus(t): | |
44 'expression : MINUS expression %prec UMINUS' | |
45 t[0] = -t[2] | |
46 | |
47 def p_expression_group(t): | |
48 'expression : LPAREN expression RPAREN' | |
49 t[0] = t[2] | |
50 | |
51 def p_expression_number(t): | |
52 'expression : NUMBER' | |
53 t[0] = t[1] | |
54 | |
55 def p_expression_name(t): | |
56 'expression : NAME' | |
57 try: | |
58 t[0] = names[t[1]] | |
59 except LookupError: | |
60 print("Undefined name '%s'" % t[1]) | |
61 t[0] = 0 | |
62 | |
63 def p_error(t): | |
64 print("Syntax error at '%s'" % t.value) | |
65 | |
66 yacc.yacc() | |
67 | |
68 | |
69 | |
70 |