-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClReader.cup
82 lines (63 loc) · 2.6 KB
/
ClReader.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
// $Id$
// ClReader.cup
// (C) 1999 Will Portnoy and Greg J. Badros
// Needs JavaCup package
// http://www.cs.princeton.edu/~appel/modern/java/CUP/
// See the README file for details.
package EDU.Washington.grad.gjb.cassowary;
import java_cup.runtime.*;
parser code
{:
public java.util.Hashtable m_variable_name_object_hash;
public boolean m_debug_parse = false;
public void setVariableNameObjectHash(java.util.Hashtable variable_name_object_hash) {
m_variable_name_object_hash = variable_name_object_hash;
}
:}
terminal GEQ, LEQ, EQ, PLUS, MINUS, UMINUS, TIMES, DIVIDE, LPAREN, RPAREN;
terminal Double NUMBER;
terminal String VARIABLE;
non terminal constraint, equation, inequality, expr;
precedence left PLUS, MINUS;
precedence left TIMES, DIVIDE;
precedence left UMINUS;
constraint ::= equation:a {: RESULT = a; :}
| inequality:a {: RESULT = a; :}
;
equation ::= expr:a EQ expr:b {: RESULT = (new ClLinearEquation ((ClLinearExpression) a, (ClLinearExpression) b)); :}
;
inequality ::= expr:a GEQ expr:b {: RESULT = (new ClLinearInequality ((ClLinearExpression) a, CL.GEQ, (ClLinearExpression) b)); :}
| expr:a LEQ expr:b {: RESULT = (new ClLinearInequality ((ClLinearExpression) a, CL.LEQ, (ClLinearExpression) b)); :}
;
expr ::= NUMBER:a {: RESULT = (new ClLinearExpression(a.doubleValue())); :}
| VARIABLE:a {:
// look up variable first
if (parser.m_debug_parse) {
System.out.println("Grammar found variable: <" + a + ">");
}
if (parser.m_variable_name_object_hash == null) {
System.err.println(" Don't have hash.");
}
if (parser.m_debug_parse) {
if (! parser.m_variable_name_object_hash.containsKey(a)) {
System.out.println(" Unrecognized variable parsed: <" + a + ">");
} else {
System.out.println(" Found variable: <" + a + "> in hash.");
}
}
ClVariable variable_object = (ClVariable) parser.m_variable_name_object_hash.get(a);
if (variable_object == null) {
System.err.println(" Could not get hashed variable.");
}
RESULT = (new ClLinearExpression(variable_object));
:}
| expr:a PLUS expr:b {: RESULT = (CL.Plus ((ClLinearExpression) a, (ClLinearExpression) b)); :}
| expr:a MINUS expr:b {: RESULT = (CL.Minus ((ClLinearExpression) a, (ClLinearExpression) b)); :}
| expr:a TIMES expr:b {: RESULT = (CL.Times ((ClLinearExpression) a, (ClLinearExpression) b)); :}
| expr:a DIVIDE expr:b {: RESULT = (CL.Divide ((ClLinearExpression) a, (ClLinearExpression) b)); :}
| MINUS expr:a {: RESULT = (CL.Times (-1, (ClLinearExpression) a)); :}
%prec UMINUS
| LPAREN expr:a RPAREN {: RESULT = a; :}
;
// Local Variables:
// tab-width: 4