~ubuntu-branches/ubuntu/vivid/weka/vivid

« back to all changes in this revision

Viewing changes to build/classes/build/classes/weka/core/mathematicalexpression/Parser.cup

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg, Torsten Werner, Soeren Sonnenburg
  • Date: 2008-10-30 06:42:46 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081030064246-648zj038l155host
Tags: 3.5.8+cup1-1
[ Torsten Werner ]
* Update Section field in doc-base file.
* Add Class-Path attribute to jar file.

[ Soeren Sonnenburg ]
* Update my email address to sonne@debian.org.
* Update copyright.
* Remove build, java cup and jflex from orig.tar.gz.
* Add cup and jflex as build dependency.
* Patch weka source to use cup from debian.
* Patch weka shell wrapper to use java-6-sun or openjdk.
* Obtain documentation from svn.
* Build depend on texlive-latex-extra (required to generate documentation).
* Add javadoc as build target.
* Use java-wrappers to start weka.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * STANDARD ML OF NEW JERSEY COPYRIGHT NOTICE, LICENSE AND DISCLAIMER.
3
 
 * 
4
 
 * Copyright (c) 1989-1998 by Lucent Technologies
5
 
 * 
6
 
 * Permission to use, copy, modify, and distribute this software and its
7
 
 * documentation for any purpose and without fee is hereby granted, provided
8
 
 * that the above copyright notice appear in all copies and that both the
9
 
 * copyright notice and this permission notice and warranty disclaimer appear
10
 
 * in supporting documentation, and that the name of Lucent Technologies, Bell
11
 
 * Labs or any Lucent entity not be used in advertising or publicity pertaining
12
 
 * to distribution of the software without specific, written prior permission.
13
 
 *
14
 
 * Lucent disclaims all warranties with regard to this software, including all
15
 
 * implied warranties of merchantability and fitness. In no event shall Lucent
16
 
 * be liable for any special, indirect or consequential damages or any damages
17
 
 * whatsoever resulting from loss of use, data or profits, whether in an action
18
 
 * of contract, negligence or other tortious action, arising out of or in
19
 
 * connection with the use or performance of this software. 
20
 
 *
21
 
 * Taken from this URL:
22
 
 * http://www.smlnj.org/license.html
23
 
 * 
24
 
 * This license is compatible with the GNU GPL (see section "Standard ML of New
25
 
 * Jersey Copyright License"):
26
 
 * http://www.gnu.org/licenses/license-list.html#StandardMLofNJ
27
 
 */
28
 
 
29
 
/*
30
 
 * Copyright 1996-1999 by Scott Hudson, Frank Flannery, C. Scott Ananian
31
 
 */
32
 
 
33
 
package weka.core.mathematicalexpression;
34
 
 
35
 
import weka.core.parser.java_cup.runtime.*;
36
 
 
37
 
import java.io.*;
38
 
import java.util.*;
39
 
 
40
 
/**
41
 
 * A parser for parsing mathematical expressions.
42
 
 *
43
 
 * @author FracPete (fracpete at waikato dot ac dot nz)
44
 
 * @version $Revision: 1.2 $
45
 
 */
46
 
 
47
 
parser code {:
48
 
  /** variable - value relation. */
49
 
  protected HashMap m_Symbols = new HashMap();
50
 
 
51
 
  /** for storing the result of the expresion. */
52
 
  protected Double m_Result = null;
53
 
 
54
 
  /**
55
 
   * Sets the variable - value relation to use.
56
 
   * 
57
 
   * @param value the variable-value relation
58
 
   */
59
 
  public void setSymbols(HashMap value) {
60
 
    m_Symbols = value;
61
 
  }
62
 
 
63
 
  /**
64
 
   * Returns the current variable - value relation in use.
65
 
   * 
66
 
   * @return the variable-value relation
67
 
   */
68
 
  public HashMap getSymbols() {
69
 
    return m_Symbols;
70
 
  }
71
 
 
72
 
  /**
73
 
   * Sets the result of the evaluation.
74
 
   * 
75
 
   * @param value the result
76
 
   */
77
 
  public void setResult(Double value) {
78
 
    m_Result = value;
79
 
  }
80
 
 
81
 
  /**
82
 
   * Returns the result of the evaluation.
83
 
   * 
84
 
   * @return the result
85
 
   */
86
 
  public Double getResult() {
87
 
    return m_Result;
88
 
  }
89
 
 
90
 
  /**
91
 
   * Runs the parser from commandline. Either reads lines from System.in
92
 
   * or from a provided file (line by line). With 
93
 
   * <code>-symbols <semi-colon separated list of variable/value pairs></code>
94
 
   * as first parameter one can provide predefined variable values. E.g.:
95
 
   * <code>-symbols "Y=10;X=3" "X+Y"</code>
96
 
   * 
97
 
   * @param args the commandline arguments
98
 
   * @throws Exception if something goes wrong
99
 
   */
100
 
  public static void main(String args[]) throws Exception {
101
 
    // read symbols, if present
102
 
    HashMap symbols = new HashMap();
103
 
    if (args.length > 0) {
104
 
      if (args[0].equals("-symbols")) {
105
 
        // parse symbols
106
 
        String[] pairs = args[1].replaceAll(" ", "").split(";");
107
 
        for (int i = 0; i < pairs.length; i++) {
108
 
          String[] parts = pairs[i].split("=");
109
 
          symbols.put(parts[0], new Double(parts[1]));
110
 
        }
111
 
        // print symbols
112
 
        System.out.println("\nSymbols provided:");
113
 
        Iterator iter = symbols.keySet().iterator();
114
 
        while (iter.hasNext()) {
115
 
          String key = (String) iter.next();
116
 
          System.out.println(key + "=" + symbols.get(key));
117
 
        }
118
 
        // remove symbols from commandline
119
 
        String[] argsNew = new String[args.length - 2];
120
 
        System.arraycopy(args, 2, argsNew, 0, argsNew.length);
121
 
        args = argsNew;
122
 
      }
123
 
    }
124
 
 
125
 
    // setup input stream
126
 
    int index = -1;
127
 
    if (args.length == 1)
128
 
      index = 0;
129
 
    BufferedReader input = null;
130
 
    if (index == -1) {
131
 
      System.out.println("\nPlease type in expressions (and press <Enter>), exit with <Ctrl+D>:");
132
 
      input = new BufferedReader(new InputStreamReader(System.in));
133
 
    }
134
 
    else {
135
 
      System.out.println("\nReading expressions from file '" + args[index] + "':");
136
 
      input = new BufferedReader(new FileReader(args[index]));
137
 
    }
138
 
 
139
 
    // process stream
140
 
    SymbolFactory sf = new DefaultSymbolFactory();
141
 
    String line;
142
 
    while ((line = input.readLine()) != null) {
143
 
      ByteArrayInputStream parserInput = new ByteArrayInputStream(line.getBytes());
144
 
      Parser parser = new Parser(new Scanner(parserInput,sf), sf);
145
 
      parser.setSymbols(symbols);
146
 
      parser.parse();
147
 
      System.out.println(line + " = " + parser.getResult());
148
 
    }
149
 
  }
150
 
:}
151
 
 
152
 
terminal COMMA, LPAREN, RPAREN;
153
 
terminal MINUS, PLUS, TIMES, DIVISION;
154
 
terminal ABS, SQRT, LOG, EXP, SIN, COS, TAN, RINT, FLOOR, POW, CEIL, IFELSE;
155
 
terminal TRUE, FALSE, LT, LE, GT, GE, EQ, NOT, AND, OR;
156
 
terminal Double NUMBER;
157
 
terminal Boolean BOOLEAN;
158
 
terminal String VARIABLE;
159
 
 
160
 
non terminal expr_list, expr_part;
161
 
non terminal Double expr;
162
 
non terminal Double opexpr;
163
 
non terminal Double varexpr;
164
 
non terminal Double funcexpr;
165
 
non terminal Boolean boolexpr;
166
 
 
167
 
precedence left PLUS, MINUS;
168
 
precedence left TIMES, DIVISION;
169
 
precedence left LPAREN, RPAREN;
170
 
precedence left ABS, SQRT, LOG, EXP, SIN, COS, TAN, RINT, FLOOR, POW, CEIL;
171
 
precedence left AND, OR;
172
 
precedence left NOT;
173
 
 
174
 
expr_list ::= expr_list expr_part | expr_part;
175
 
expr_part ::= expr:e {: parser.setResult(e); :} ;
176
 
expr      ::=   NUMBER:n
177
 
                {: RESULT = n; :}
178
 
              | LPAREN expr:e RPAREN
179
 
                {: RESULT = e; :}
180
 
              | opexpr:o
181
 
                {: RESULT = o; :}
182
 
              | varexpr:v
183
 
                {: RESULT = v; :}
184
 
              | funcexpr:f
185
 
                {: RESULT = f; :}
186
 
              ;
187
 
 
188
 
opexpr    ::=   expr:l PLUS expr:r
189
 
                {: RESULT = new Double(l.doubleValue() + r.doubleValue()); :}
190
 
              | expr:l MINUS expr:r
191
 
                {: RESULT = new Double(l.doubleValue() - r.doubleValue()); :}
192
 
              | expr:l TIMES expr:r
193
 
                {: RESULT = new Double(l.doubleValue() * r.doubleValue()); :}
194
 
              | expr:l DIVISION expr:r
195
 
                {: RESULT = new Double(l.doubleValue() / r.doubleValue()); :}
196
 
              ;
197
 
 
198
 
varexpr  ::=    VARIABLE:v
199
 
                {: if (parser.getSymbols().containsKey(v)) 
200
 
                     RESULT = (Double) parser.getSymbols().get(v); 
201
 
                   else 
202
 
                     throw new IllegalStateException("Unknown symbol '" + v + "'!"); 
203
 
                :}
204
 
              ;
205
 
 
206
 
funcexpr ::=    ABS LPAREN expr:e RPAREN
207
 
                {: RESULT = new Double(Math.abs(e)); :}
208
 
              | SQRT LPAREN expr:e RPAREN
209
 
                {: RESULT = new Double(Math.sqrt(e)); :}
210
 
              | LOG LPAREN expr:e RPAREN
211
 
                {: RESULT = new Double(Math.log(e)); :}
212
 
              | EXP LPAREN expr:e RPAREN
213
 
                {: RESULT = new Double(Math.exp(e)); :}
214
 
              | SIN LPAREN expr:e RPAREN
215
 
                {: RESULT = new Double(Math.sin(e)); :}
216
 
              | COS LPAREN expr:e RPAREN
217
 
                {: RESULT = new Double(Math.cos(e)); :}
218
 
              | TAN LPAREN expr:e RPAREN
219
 
                {: RESULT = new Double(Math.tan(e)); :}
220
 
              | RINT LPAREN expr:e RPAREN
221
 
                {: RESULT = new Double(Math.rint(e)); :}
222
 
              | FLOOR LPAREN expr:e RPAREN
223
 
                {: RESULT = new Double(Math.floor(e)); :}
224
 
              | POW LPAREN expr:base COMMA expr:exponent RPAREN
225
 
                {: RESULT = new Double(Math.pow(base, exponent)); :}
226
 
              | CEIL LPAREN expr:e RPAREN
227
 
                {: RESULT = new Double(Math.ceil(e)); :}
228
 
              | IFELSE LPAREN boolexpr:b COMMA expr:e_true COMMA expr:e_false RPAREN
229
 
                {: if (b) 
230
 
                     RESULT = e_true; 
231
 
                   else
232
 
                     RESULT = e_false; 
233
 
                :}
234
 
              ;
235
 
 
236
 
boolexpr ::=    BOOLEAN:b 
237
 
                {: RESULT = b; :}
238
 
              | TRUE
239
 
                {: RESULT = new Boolean(true); :}
240
 
              | FALSE
241
 
                {: RESULT = new Boolean(false); :}
242
 
              | expr:l LT expr:r
243
 
                {: RESULT = new Boolean(l.doubleValue() < r.doubleValue()); :}
244
 
              | expr:l LE expr:r
245
 
                {: RESULT = new Boolean(l.doubleValue() <= r.doubleValue()); :}
246
 
              | expr:l GT expr:r
247
 
                {: RESULT = new Boolean(l.doubleValue() > r.doubleValue()); :}
248
 
              | expr:l GE expr:r
249
 
                {: RESULT = new Boolean(l.doubleValue() >= r.doubleValue()); :}
250
 
              | expr:l EQ expr:r
251
 
                {: RESULT = new Boolean(l.doubleValue() == r.doubleValue()); :}
252
 
              | LPAREN boolexpr:b RPAREN
253
 
                {: RESULT = b; :}
254
 
              | NOT boolexpr:b
255
 
                {: RESULT = !b; :}
256
 
              | boolexpr:l AND boolexpr:r
257
 
                {: RESULT = l && r; :}
258
 
              | boolexpr:l OR boolexpr:r
259
 
                {: RESULT = l || r; :}
260
 
              ;
261