source: XIOS/trunk/src/parse_expr/yacc_parser.yacc @ 1038

Last change on this file since 1038 was 1038, checked in by ymipsl, 7 years ago
  • Add generic ternary arithmetic operators and filters
  • Add conditional operator "(cond) ? x : y" for arithmetic filters.

YM

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 7.6 KB
Line 
1%{
2#include "filter_expr_node.hpp"
3#include <string>
4#include <iostream>
5#include "exception.hpp"
6
7using namespace std;
8using namespace xios;
9
10extern "C"
11{
12  int yyparse(void);
13  int yylex(void);
14  int yyerror(const char *s);
15}
16
17  IFilterExprNode* parsed;
18  std::string globalInputText;
19  size_t globalReadOffset = 0;
20
21  int readInputForLexer(char* buffer, size_t* numBytesRead, size_t maxBytesToRead)
22  {
23    size_t numBytesToRead = maxBytesToRead;
24    size_t bytesRemaining = globalInputText.length()-globalReadOffset;
25    size_t i;
26    if (numBytesToRead > bytesRemaining) numBytesToRead = bytesRemaining;
27    for (i = 0; i < numBytesToRead; i++) buffer[i] = globalInputText.c_str()[globalReadOffset + i];
28    *numBytesRead = numBytesToRead;
29    globalReadOffset += numBytesToRead;
30    return 0;
31  }
32%}
33
34%union
35{
36  std::string* str;                /* symbol table index */
37  xios::IScalarExprNode* scalarNode;
38  xios::IFilterExprNode* filterNode;
39};
40
41%token <str> NUMBER
42%token <str> VAR ID AVERAGE
43%token PLUS MINUS TIMES DIVIDE POWER
44%token EQ LT GT LE GE NE
45%token LEFT_PARENTHESIS RIGHT_PARENTHESIS
46%token QUESTION_MARK COLON
47%token <str> END
48
49%nonassoc QUESTION_MARK COLON
50%left EQ LT GT LE GE NE
51%left PLUS MINUS
52%left TIMES DIVIDE
53%nonassoc NEG
54%right POWER
55
56%type <scalarNode> Expression
57%type <filterNode> Line Field_expr
58%start Line
59%%
60
61
62Line:
63     END            { /* Nothing to do */ }
64   | Field_expr END { parsed = $1; }
65  ;
66
67Expression:
68            NUMBER { $$ = new CScalarValExprNode(*$1); delete $1; }
69          | VAR    { $$ = new CScalarVarExprNode(*$1); delete $1; }
70          | Expression PLUS Expression   { $$ = new CScalarBinaryOpExprNode($1, "add", $3); }
71          | Expression MINUS Expression  { $$ = new CScalarBinaryOpExprNode($1, "minus", $3); }
72          | Expression TIMES Expression  { $$ = new CScalarBinaryOpExprNode($1, "mult", $3); }
73          | Expression DIVIDE Expression { $$ = new CScalarBinaryOpExprNode($1, "div", $3); }
74          | MINUS Expression %prec NEG   { $$ = new CScalarUnaryOpExprNode("neg", $2); }
75          | Expression POWER Expression  { $$ = new CScalarBinaryOpExprNode($1, "pow", $3); }
76          | Expression EQ Expression  { $$ = new CScalarBinaryOpExprNode($1, "eq", $3); }
77          | Expression LT Expression  { $$ = new CScalarBinaryOpExprNode($1, "lt", $3); }
78          | Expression GT Expression  { $$ = new CScalarBinaryOpExprNode($1, "gt", $3); }
79          | Expression LE Expression  { $$ = new CScalarBinaryOpExprNode($1, "le", $3); }
80          | Expression GE Expression  { $$ = new CScalarBinaryOpExprNode($1, "ge", $3); }
81          | Expression NE Expression  { $$ = new CScalarBinaryOpExprNode($1, "ne", $3); }
82          | Expression QUESTION_MARK Expression COLON Expression {$$ = new CScalarTernaryOpExprNode($1, "cond", $3, $5);}
83          | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS    { $$ = $2; }
84          | ID LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$ = new CScalarUnaryOpExprNode(*$1, $3); delete $1; }
85          ;
86
87Field_expr:
88            ID      { $$ = new CFilterFieldExprNode(*$1); delete $1; }
89          | AVERAGE { $$ = new CFilterTemporalFieldExprNode(*$1); delete $1; }
90          | Field_expr PLUS Field_expr   { $$ = new CFilterFieldFieldOpExprNode($1, "add", $3); }
91          | Field_expr MINUS Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "minus", $3); }
92          | Field_expr TIMES Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "mult", $3); }
93          | Field_expr DIVIDE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "div", $3); }
94          | MINUS Field_expr %prec NEG   { $$ = new CFilterUnaryOpExprNode("neg", $2); }
95          | Field_expr POWER Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "pow", $3); }
96          | Field_expr EQ Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "eq", $3); }
97          | Field_expr LT Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "lt", $3); }
98          | Field_expr GT Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "gt", $3); }
99          | Field_expr LE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "le", $3); }
100          | Field_expr GE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "ge", $3); }
101          | Field_expr NE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "ne", $3); }
102          | LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS       { $$ = $2; }
103          | Expression QUESTION_MARK Expression COLON Field_expr {$$ = new CFilterScalarScalarFieldOpExprNode($1, "cond",$3, $5);}
104          | Expression QUESTION_MARK Field_expr COLON Expression {$$ = new CFilterScalarFieldScalarOpExprNode($1, "cond",$3, $5);}
105          | Expression QUESTION_MARK Field_expr COLON Field_expr {$$ = new CFilterScalarFieldFieldOpExprNode($1, "cond",$3, $5);}
106          | Field_expr QUESTION_MARK Expression COLON Expression {$$ = new CFilterFieldScalarScalarOpExprNode($1, "cond",$3, $5);}
107          | Field_expr QUESTION_MARK Expression COLON Field_expr {$$ = new CFilterFieldScalarFieldOpExprNode($1, "cond",$3, $5);}
108          | Field_expr QUESTION_MARK Field_expr COLON Expression {$$ = new CFilterFieldFieldScalarOpExprNode($1, "cond",$3, $5);}
109          | Field_expr QUESTION_MARK Field_expr COLON Field_expr {$$ = new CFilterFieldFieldFieldOpExprNode($1, "cond",$3, $5);}
110          | Field_expr PLUS Expression   { $$ = new CFilterFieldScalarOpExprNode($1, "add", $3); }
111          | Expression PLUS Field_expr   { $$ = new CFilterScalarFieldOpExprNode($1, "add", $3); }
112          | Field_expr MINUS Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "minus", $3); }
113          | Expression MINUS Field_expr  { $$ = new CFilterScalarFieldOpExprNode($1, "minus", $3); }
114          | Field_expr TIMES Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "mult", $3); }
115          | Expression TIMES Field_expr  { $$ = new CFilterScalarFieldOpExprNode($1, "mult", $3); }
116          | Field_expr DIVIDE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "div", $3); }
117          | Expression DIVIDE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "div", $3); }
118          | Field_expr POWER Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "pow", $3); }
119          | Field_expr EQ Expression { $$ = new CFilterFieldScalarOpExprNode($1, "eq", $3); }
120          | Expression EQ Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "eq", $3); }
121          | Field_expr LT Expression { $$ = new CFilterFieldScalarOpExprNode($1, "lt", $3); }
122          | Expression LT Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "lt", $3); }
123          | Field_expr GT Expression { $$ = new CFilterFieldScalarOpExprNode($1, "gt", $3); }
124          | Expression GT Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "gt", $3); }
125          | Field_expr LE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "le", $3); }
126          | Expression LE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "le", $3); }
127          | Field_expr GE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "ge", $3); }
128          | Expression GE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "ge", $3); }
129          | Field_expr NE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "ne", $3); }
130          | Expression NE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "ne", $3); }
131          | ID LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$ = new CFilterUnaryOpExprNode(*$1, $3); delete $1; }
132          ;
133%%
134
135extern "C"
136{
137  int yyerror(const char *s)
138  {
139    ERROR("int yyerror(const char *s)", << "Parsing error: " << s << endl);
140  }
141}
142
143namespace xios
144{
145  IFilterExprNode* parseExpr(const string& strExpr)
146  {
147    globalInputText = strExpr;
148    globalReadOffset = 0;
149    yyparse();
150    return parsed;
151  }
152}
153
154
Note: See TracBrowser for help on using the repository browser.