-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondition_parser.py
203 lines (175 loc) · 6.04 KB
/
condition_parser.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- coding: utf-8 -*-
# This file is part of sf2dve.
#
# sf2dve is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# sf2dve is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with sf2dve. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Sun Nov 9 09:35:20 2014
@author: pavla
"""
from ply import lex, yacc
import os
tokens = ("RIGHT_OP", "LEFT_OP", "AND_OP", "OR_OP", "LE_OP", "GE_OP", "EQ_OP",
"NE_OP", "LBRACKET", "RBRACKET", "NUMBER", "IDENTIFIER")
literals = ";,:=()&!~-+*/%<>^|?"
t_ignore = " \t\r\f\v"
t_ignore_COMMENT = r"(//.*)|(/\*(.|\n)*?\*/)"
t_RIGHT_OP = r">>"
t_LEFT_OP = r"<<"
t_AND_OP = r"&&"
t_OR_OP = r"\|\|"
t_LE_OP = r"<="
t_GE_OP = r">="
t_EQ_OP = r"=="
t_NE_OP = r"!="
t_LBRACKET = r"(\[|<:)"
t_RBRACKET = r"(\]|:>)"
t_NUMBER = r"[0-9]+"
t_IDENTIFIER = r"[a-zA-Z_][a-zA-Z_0-9]*"
def t_newline(t):
r"\n+"
t.lexer.lineno += len(t.value)
def t_error(t):
raise TypeError("Unknown text '%s'" % t.value)
def p_start(p):
"""start : empty
| logical_or_expression"""
if p[1] is None:
p[0] = ""
else:
p[0] = p[1]
def p_empty(p):
"empty :"
pass
def p_logical_or_expression(p):
"""logical_or_expression : logical_and_expression
| logical_or_expression OR_OP logical_and_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' or ' + p[3]
def p_logical_and_expression(p):
"""logical_and_expression : inclusive_or_expression
| logical_and_expression AND_OP inclusive_or_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' and ' + p[3]
def p_inclusive_or_expression(p):
"""inclusive_or_expression : exclusive_or_expression
| inclusive_or_expression '|' exclusive_or_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_exclusive_or_expression(p):
"""exclusive_or_expression : and_expression
| exclusive_or_expression '^' and_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_and_expression(p):
"""and_expression : equality_expression
| and_expression '&' equality_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_equality_expression(p):
"""equality_expression : relational_expression
| equality_expression EQ_OP relational_expression
| equality_expression NE_OP relational_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_relational_expression(p):
"""relational_expression : shift_expression
| relational_expression '<' shift_expression
| relational_expression '>' shift_expression
| relational_expression LE_OP shift_expression
| relational_expression GE_OP shift_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_shift_expression(p):
"""shift_expression : additive_expression
| shift_expression LEFT_OP additive_expression
| shift_expression RIGHT_OP additive_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_additive_expression(p):
"""additive_expression : multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_multiplicative_expression(p):
"""multiplicative_expression : unary_expression
| multiplicative_expression '*' unary_expression
| multiplicative_expression '/' unary_expression
| multiplicative_expression '%' unary_expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + ' ' + p[2] + ' ' + p[3]
def p_unary_expression(p):
"""unary_expression : primary_expression
| unary_operator unary_expression
| primary_expression LBRACKET NUMBER RBRACKET"""
if len(p) == 2:
p[0] = p[1]
elif len(p) == 3 and p[1] == '+':
p[0] = p[2]
elif len(p) == 3:
p[0] = p[1] + p[2]
else:
p[0] = p[1] + p[2] + p[3] + p[4]
def p_unary_operator(p):
"""unary_operator : '+'
| '-'
| '~'
| '!'"""
if p[1] == '-' or p[1] == '~':
p[0] = p[1]
if p[1] == '!':
p[0] = ' not '
def p_primary_expression(p):
"""primary_expression : IDENTIFIER
| NUMBER
| '(' logical_or_expression ')'"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[1] + p[2] + p[3]
def p_error(p):
if p is None:
raise ValueError("Unknown error")
raise ValueError("Syntax error, line %s: %s" % (p.lineno, p.type))
directory = os.path.join(os.path.dirname(__file__),
"parser_tables",
os.path.basename(__file__).rsplit('.', 1)[0])
try:
os.makedirs(directory, exist_ok=True)
except OSError:
pass
lexer = lex.lex(debug=False, optimize=True, outputdir=directory)
parser = yacc.yacc(debug=False, optimize=True, outputdir=directory)
def parse(text, lexer=lexer):
return (parser.parse(text, lexer))