summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/javacc/RankingExpressionParser.jj
blob: a800028d00b2b61a972372de911134c18e670b06 (plain) (blame)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * When this file is changed, do "mvn generate-sources" to rebuild the parser.
 *
 * @author  bratseth
 */
options {
    CACHE_TOKENS       = true;
    STATIC             = false;
    DEBUG_PARSER       = false;
    USER_TOKEN_MANAGER = false;
    ERROR_REPORTING    = true;
    USER_CHAR_STREAM   = false;
}

PARSER_BEGIN(RankingExpressionParser)

package com.yahoo.searchlib.rankingexpression.parser;

import com.yahoo.searchlib.rankingexpression.rule.*;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
import com.yahoo.searchlib.rankingexpression.evaluation.StringValue;
import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue;
import com.yahoo.tensor.MapTensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.functions.*;
import java.util.Collections;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class RankingExpressionParser {

}

PARSER_END(RankingExpressionParser)

SKIP :
{
    <[" ","\n","\r","\t"]>
}

TOKEN :
{
    <INTEGER: <DECIMAL> (["l","L"])? | <HEX> (["l","L"])? | <OCTAL> (["l","L"])?> |
        <#DECIMAL: ["1"-"9"] (["0"-"9"])*> |
        <#HEX: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+> |
        <#OCTAL: "0" (["0"-"7"])*> |
    <FLOAT: (["0"-"9"])+ ("." (["0"-"9"])*)? (<EXPONENT>)? (["f","F","d","D"])?> |
        <#EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+>
}

TOKEN :
{
    <LBRACE: "("> |
    <RBRACE: ")"> |
    <LSQUARE: "["> |
    <RSQUARE: "]"> |
    <LCURLY: "{"> |
    <RCURLY: "}"> |
    <ADD: "+"> |
    <SUB: "-"> |
    <DIV: "/"> |
    <MUL: "*"> |
    <DOT: "."> |
    <DOLLAR: "$"> |
    <COMMA: ","> |
    <COLON: ":"> |
    <LE: "<="> |
    <LT: "<"> |
    <EQ: "=="> |
    <AQ: "~="> |
    <GE: ">="> |
    <GT: ">"> |
    <STRING: ("\"" (~["\""] | "\\\"")* "\"") |
             ("'" (~["'"] | "\\'")* "'")> |
    <IF: "if"> |
    <COSH: "cosh"> |
    <SINH: "sinh"> |
    <TANH: "tanh"> |
    <COS: "cos"> |
    <SIN: "sin"> |
    <TAN: "tan"> |
    <ACOS: "acos"> |
    <ASIN: "asin"> |
    <ATAN2: "atan2"> |
    <ATAN: "atan"> |
    <EXP: "exp"> |
    <LDEXP: "ldexp"> |
    <LOG10: "log10"> |
    <LOG: "log"> |
    <POW: "pow"> |
    <SQRT: "sqrt"> |
    <CEIL: "ceil"> |
    <FABS: "fabs"> |
    <FLOOR: "floor"> |
    <FMOD: "fmod"> |
    <ISNAN: "isNan"> |
    <IN: "in"> |
    <REDUCE: "reduce"> |
    <AVG: "avg" > |
    <COUNT: "count"> |
    <PROD: "prod"> |
    <SUM: "sum"> |
    <MAX: "max"> |
    <MIN: "min"> |
    <RELU: "relu"> |
    <SIGMOID: "sigmoid"> |
    <IDENTIFIER: (["A"-"Z","a"-"z","0"-"9","_","@"](["A"-"Z","a"-"z","0"-"9","_","@","$"])*)>
}

// Declare a special skip token for comments.
SPECIAL_TOKEN :
{
  <SINGLE_LINE_COMMENT: "#" (~["\n","\r"])* >
}

List<ReferenceNode> featureList() :
{
    List<ReferenceNode> ret = new ArrayList<ReferenceNode>();
    ReferenceNode exp;
}
{
    ( ( exp = feature() { ret.add(exp); } )+ <EOF> )
    { return ret; }
}

ExpressionNode rankingExpression() :
{
    ExpressionNode ret;
}
{
    ( ret = expression() <EOF> )
    { return ret; }
}

ExpressionNode expression() :
{
    ExpressionNode left, right;
    List<ExpressionNode> rightList;
    TruthOperator      comparatorOp;
}
{
    ( left = arithmeticExpression()
      (
          ( comparatorOp = comparator() right = arithmeticExpression() { left = new ComparisonNode(left, comparatorOp, right); } ) |
          ( <IN>           rightList = expressionList() { left =  new SetMembershipNode(left, rightList); }           )
      ) *
    )
    { return left; }
}

ExpressionNode arithmeticExpression() :
{
    ExpressionNode left, right = null;
    ArithmeticOperator arithmeticOp;
}
{
    ( left = value()
      ( arithmeticOp = arithmetic() right = value() { left = ArithmeticNode.resolve(left, arithmeticOp, right); } ) *
    )
    { return left; }
}

ArithmeticOperator arithmetic() : { }
{
    ( <ADD> { return ArithmeticOperator.PLUS;     } |
      <SUB> { return ArithmeticOperator.MINUS;    } |
      <DIV> { return ArithmeticOperator.DIVIDE;   } |
      <MUL> { return ArithmeticOperator.MULTIPLY; } )
    { return null; }
}

TruthOperator comparator() : { }
{
     ( <LE> { return TruthOperator.SMALLEREQUAL; } |
       <LT> { return TruthOperator.SMALLER;      } |
       <EQ> { return TruthOperator.EQUAL;        } |
       <AQ> { return TruthOperator.APPROX_EQUAL; } |
       <GE> { return TruthOperator.LARGEREQUAL;  } |
       <GT> { return TruthOperator.LARGER;       } )
     { return null; }
}

ExpressionNode value() :
{
    ExpressionNode ret;
    boolean neg = false;
}
{
    ( [ LOOKAHEAD(2) <SUB>                                  { neg = true;     }   ]
      (            ret = constantPrimitive()                                      |
                   LOOKAHEAD(2) ret = ifExpression()                              |
                   LOOKAHEAD(2) ret = function()                                  |
                   ret = feature()                                                |
                   ret = queryFeature()                                           |
        ( <LBRACE> ret = expression() <RBRACE> { ret = new EmbracedNode(ret); } ) ) )
    { return neg ? new NegativeNode(ret) : ret; }
}

IfNode ifExpression() :
{
    ExpressionNode condition, ifTrue, ifFalse;
    Double trueProbability = null;
}
{
    ( <IF> <LBRACE> ( condition =  expression() )
      <COMMA> ifTrue = expression() <COMMA> ifFalse = expression() ( <COMMA> trueProbability = number() )? <RBRACE> )
    {
        return new IfNode(condition, ifTrue, ifFalse, trueProbability);
    }
}

ReferenceNode queryFeature() :
{
    String name;
}
{
    ( <DOLLAR> name = identifier() )
    { return new ReferenceNode("query", Arrays.asList((ExpressionNode)new NameNode(name)), null); }
}

ReferenceNode feature() :
{
    List<ExpressionNode> args = null;
    String name, out = null;
}
{
    ( name = identifier() [ <LBRACE> args = args() <RBRACE> ] [ <DOT> out = outs() ] )
    { return new ReferenceNode(name, args, out); }
}

String outs() :
{
    StringBuilder ret = new StringBuilder();
    String str;
}
{
    (   str = out() { ret.append(str);         }
      ( <DOT>       { ret.append(token.image); }
        str = out() { ret.append(str);         } )* )
    { return ret.toString(); }
}

String out() :
{
    Function fnc;
    String name;
}
{
    ( <INTEGER>                  { return token.image;    } |
      <FLOAT>                    { return token.image;    } |
      name = identifier()        { return name; }           )
    { return null; }
}

List<ExpressionNode> args() :
{
    List<ExpressionNode> arguments = new ArrayList<ExpressionNode>();
    ExpressionNode argument;
}
{
    ( argument = arg() { arguments.add(argument); } ( <COMMA> argument = arg() { arguments.add(argument); } )* )
    { return arguments; }
}

// TODO: Replace use of this for macro arguments with value()
// For that to work with the current search execution framework
// we need to generate another macro for the argument such that we can replace
// instances of the argument with the reference to that macro in the same way
// as we replace by constants/names today (this can make for some fun combinatorial explosion).
// Simon also points out that we should stop doing macro expansion in the toString of a macro.
// - Jon 2014-05-02
ExpressionNode arg() :
{
    ExpressionNode ret;
    String name;
    Function fnc;
}
{
    ( ret = constantPrimitive()                                          |
      LOOKAHEAD(2) ret = feature()                                       |
      name = identifier()            { ret = new NameNode(name);       } )
    { return ret; }
}

ExpressionNode function() :
{
    ExpressionNode function;
}
{
    ( function = scalarFunction() | function = tensorFunction() )
    { return function; }
}

FunctionNode scalarFunction() :
{
    Function function;
    ExpressionNode arg1, arg2;
}
{
    (
      ( function = unaryFunctionName() <LBRACE> arg1 = expression() <RBRACE> )
      { return new FunctionNode(function, arg1); }
    ) |
    (
      ( function = binaryFunctionName() <LBRACE> arg1 = expression() <COMMA> arg2 = expression() <RBRACE> )
      { return new FunctionNode(function, arg1, arg2); }
    )
}

ExpressionNode tensorFunction() :
{
    ExpressionNode tensorExpression;
}
{
    ( tensorExpression = tensorPrimitiveReduce() | tensorExpression = tensorReduce() )
    { return tensorExpression; }
}

ExpressionNode tensorPrimitiveReduce() :
{
    ExpressionNode tensor1;
    ReduceFunction.Aggregator aggregator;
    List<String> dimensions = null;
}
{
    <REDUCE> <LBRACE> tensor1 = expression() <COMMA> aggregator = tensorReduceAggregator() dimensions = tagCommaLeadingList() <RBRACE>
    { return new TensorReduceNode(tensor1, aggregator, dimensions); }
}

ExpressionNode tensorReduce() :
{
    ExpressionNode tensor1;
    ReduceFunction.Aggregator aggregator;
    List<String> dimensions = null;
}
{
    aggregator = tensorReduceAggregator()
    <LBRACE> tensor1 = expression() dimensions = tagCommaLeadingList() <RBRACE>
    { return new TensorReduceNode(tensor1, aggregator, dimensions); }
}

ReduceFunction.Aggregator tensorReduceAggregator() :
{
}
{
    ( <AVG> | <COUNT> | <PROD> | <SUM> | <MAX> | <MIN> )
    { return ReduceFunction.Aggregator.valueOf(token.image); }
}

// This is needed not to parse tensor functions but for the "reserved names as literals" workaround cludge
String tensorFunctionName() :
{
    ReduceFunction.Aggregator aggregator;
}
{
    ( <REDUCE> { return token.image; } ) 
    |
    ( aggregator = tensorReduceAggregator() { return aggregator.toString(); } )
}

Function unaryFunctionName() : { }
{
    <COS>     { return Function.cos;     } |
    <SIN>     { return Function.sin;     } |
    <TAN>     { return Function.tan;     } |
    <COSH>    { return Function.cosh;    } |
    <SINH>    { return Function.sinh;    } |
    <TANH>    { return Function.tanh;    } |
    <ACOS>    { return Function.acos;    } |
    <ASIN>    { return Function.asin;    } |
    <ATAN>    { return Function.atan;    } |
    <EXP>     { return Function.exp;     } |
    <LOG10>   { return Function.log10;   } |
    <LOG>     { return Function.log;     } |
    <SQRT>    { return Function.sqrt;    } |
    <CEIL>    { return Function.ceil;    } |
    <FABS>    { return Function.fabs;    } |
    <FLOOR>   { return Function.floor;   } |
    <ISNAN>   { return Function.isNan;   } |
    <RELU>    { return Function.relu;    } |
    <SIGMOID> { return Function.sigmoid; }
}

Function binaryFunctionName() : { }
{
    <ATAN2> { return Function.atan2; } |
    <LDEXP> { return Function.ldexp; } |
    <POW>   { return Function.pow;   } |
    <FMOD>  { return Function.fmod;  } |
    <MIN>   { return Function.min;   } |
    <MAX>   { return Function.max;   }
}

List<ExpressionNode> expressionList() :
{
    List<ExpressionNode> list = new ArrayList<ExpressionNode>();
    ExpressionNode expression;
}
{
    <LSQUARE>
    expression=expression() { list.add(expression); }
    ( LOOKAHEAD(2) <COMMA> expression=expression() { list.add(expression); } ) *
    <RSQUARE>
    { return list; }
}

double number() :
{
    String sign = "";
}
{
    ( <SUB> { sign = "-";} )? ( <FLOAT> | <INTEGER> )
    { return Double.parseDouble(sign + token.image); }
}

String identifier() :
{
    String name;
    Function func;
}
{
    name = tensorFunctionName() { return name; }            |
    func = unaryFunctionName()  { return func.toString(); } |
    func = binaryFunctionName() { return func.toString(); } |
    <IF>                        { return token.image; }     |
    <IN>                        { return token.image; }     |
    <IDENTIFIER>                { return token.image; }
}

// An identifier or integer
String tag() :
{
    String name;
}
{
    name = identifier() { return name; } |
    <INTEGER> { return token.image; }
}

List<String> tagCommaLeadingList() :
{
    List<String> list = new ArrayList<String>();
    String element;
}
{
    ( <COMMA> element = tag() { list.add(element); } ) *
    { return list; }
}

ConstantNode constantPrimitive() :
{
    String sign = "";
}
{
    ( <SUB> { sign = "-";} ) ?
    ( <INTEGER> | <FLOAT> | <STRING> )
    { return new ConstantNode(Value.parse(sign + token.image),sign + token.image); }
}

Value primitiveValue() :
{
    String sign = "";
}
{
    ( <SUB> { sign = "-";} ) ?
    ( <INTEGER> | <FLOAT> | <STRING> )
    { return Value.parse(sign + token.image); }
}