-
Notifications
You must be signed in to change notification settings - Fork 1
/
termcount.cup
165 lines (122 loc) · 3.54 KB
/
termcount.cup
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
package edu.wpi.termcount;
import java_cup.runtime.*;
import kodkod.ast.*;
import java.util.*;
parser code {:
public void report_fatal_error(String message, Object info)
throws Exception
{
System.err.println(message);
if(info instanceof Symbol)
{
Symbol tok = (Symbol)info;
// Don't try to recover. Just report the error to the user.
// Docs say line# = cur_token.left
// col# = cur_token.right
throw new FormulaParserException(tok.left,
tok.right, tok.value);
}
super.report_fatal_error(message, info);
}
List<Variable> makeSingleVector(Variable v)
{
List<Variable> result = new ArrayList<Variable>();
result.add(v);
return result;
}
List<Variable> addToVector(List<Variable> vv, Variable v2)
{
List<Variable> result = new ArrayList<Variable>(vv);
result.add(v2);
return result;
}
Set<Relation> relations = new HashSet<Relation>();
Formula makeAtom(List<Variable> vec, String pname)
{
try
{
Expression thevec = MFormulaManager.makeVarTupleV(vec);
Relation pred = MFormulaManager.makeRelation(pname, vec.size());
relations.add(pred);
return MFormulaManager.makeAtom(thevec, pred);
}
catch(MGEManagerException e)
{
// impossible
return null;
}
}
Formula makeForAll(Formula f, String s, Variable v)
{
try
{
Expression sort = MFormulaManager.makeRelation(s, 1);
Decls d = MFormulaManager.makeOneOfDecl(v, sort);
return MFormulaManager.makeForAll(f, d);
}
catch(MGEManagerException e)
{
// impossible
return null;
}
}
Formula makeExists(Formula f, String s, Variable v)
{
try
{
Expression sort = MFormulaManager.makeRelation(s, 1);
Decls d = MFormulaManager.makeOneOfDecl(v, sort);
return MFormulaManager.makeExists(f, d);
}
catch(MGEManagerException e)
{
// impossible
return null;
}
}
:}
/* Preliminaries to set up and use the scanner. */
init with {: /* No longer an init method? */ :};
scan with {: return getScanner().next_token(); :};
/* Terminals (tokens returned by the scanner). */
terminal AND, OR, NOT, BAR, COLON, LPAREN, RPAREN, FORSOME, FORALL, EQUALS, ARROW, IN;
terminal Variable VAR;
terminal String SORTORPRED;
/* Non-terminals */
non terminal Formula FORMULA;
non terminal List<Variable> VARVECTOR;
precedence left OR;
precedence left AND;
precedence nonassoc NOT;
/* The grammar */
FORMULA ::=
VAR:v1 EQUALS VAR:v2
{: RESULT = MFormulaManager.makeEqAtom(v1, v2); :}
|
VARVECTOR:vec IN SORTORPRED:pname
{: RESULT = parser.makeAtom(vec, pname); :}
|
LPAREN FORMULA:f RPAREN
{: RESULT = f; :}
|
FORMULA:f AND FORMULA:g
{: RESULT = MFormulaManager.makeAnd(f, g); :}
|
FORMULA:f OR FORMULA:g
{: RESULT = MFormulaManager.makeOr(f, g); :}
|
NOT FORMULA:f
{: RESULT = MFormulaManager.makeNegation(f); :}
|
FORALL VAR:v COLON SORTORPRED:s BAR FORMULA:f
{: RESULT = parser.makeForAll(f, s, v); :}
|
FORSOME VAR:v COLON SORTORPRED:s BAR FORMULA:f
{: RESULT = parser.makeExists(f, s, v); :}
;
VARVECTOR ::= VAR:v
{: RESULT = parser.makeSingleVector(v); :}
|
VARVECTOR:vv ARROW VAR:v2
{: RESULT = parser.addToVector(vv, v2); :}
;