source: XIOS/dev/branch_yushan_merged/src/parse_expr/yacc_parser.yacc @ 1134

Last change on this file since 1134 was 1134, checked in by yushan, 7 years ago

branch merged with trunk r1130

  • 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.9 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  static IFilterExprNode* parsed;
18  static std::string globalInputText;
19  static std::string *globalInputText_ptr = 0;
20  static size_t globalReadOffset = 0;
21  #pragma omp threadprivate(parsed, globalInputText_ptr, globalReadOffset)
22 
23  int readInputForLexer(char* buffer, size_t* numBytesRead, size_t maxBytesToRead)
24  {
25    if(globalInputText_ptr == 0) globalInputText_ptr = new std::string;
26    size_t numBytesToRead = maxBytesToRead;
27    size_t bytesRemaining = (*globalInputText_ptr).length()-globalReadOffset;
28    size_t i;
29    if (numBytesToRead > bytesRemaining) numBytesToRead = bytesRemaining;
30    for (i = 0; i < numBytesToRead; i++) buffer[i] = (*globalInputText_ptr).c_str()[globalReadOffset + i];
31    *numBytesRead = numBytesToRead;
32    globalReadOffset += numBytesToRead;
33    return 0;
34  }
35%}
36
37%union
38{
39  std::string* str;                /* symbol table index */
40  xios::IScalarExprNode* scalarNode;
41  xios::IFilterExprNode* filterNode;
42};
43
44%token <str> NUMBER
45%token <str> VAR ID AVERAGE
46%token PLUS MINUS TIMES DIVIDE POWER
47%token EQ LT GT LE GE NE
48%token LEFT_PARENTHESIS RIGHT_PARENTHESIS
49%token QUESTION_MARK COLON
50%token <str> END
51
52%nonassoc QUESTION_MARK COLON
53%left EQ LT GT LE GE NE
54%left PLUS MINUS
55%left TIMES DIVIDE
56%nonassoc NEG
57%right POWER
58
59%type <scalarNode> Expression
60%type <filterNode> Line Field_expr
61%start Line
62%%
63
64
65Line:
66     END            { /* Nothing to do */ }
67   | Field_expr END { parsed = $1; }
68  ;
69
70Expression:
71            NUMBER { $$ = new CScalarValExprNode(*$1); delete $1; }
72          | VAR    { $$ = new CScalarVarExprNode(*$1); delete $1; }
73          | Expression PLUS Expression   { $$ = new CScalarBinaryOpExprNode($1, "add", $3); }
74          | Expression MINUS Expression  { $$ = new CScalarBinaryOpExprNode($1, "minus", $3); }
75          | Expression TIMES Expression  { $$ = new CScalarBinaryOpExprNode($1, "mult", $3); }
76          | Expression DIVIDE Expression { $$ = new CScalarBinaryOpExprNode($1, "div", $3); }
77          | MINUS Expression %prec NEG   { $$ = new CScalarUnaryOpExprNode("neg", $2); }
78          | Expression POWER Expression  { $$ = new CScalarBinaryOpExprNode($1, "pow", $3); }
79          | Expression EQ Expression  { $$ = new CScalarBinaryOpExprNode($1, "eq", $3); }
80          | Expression LT Expression  { $$ = new CScalarBinaryOpExprNode($1, "lt", $3); }
81          | Expression GT Expression  { $$ = new CScalarBinaryOpExprNode($1, "gt", $3); }
82          | Expression LE Expression  { $$ = new CScalarBinaryOpExprNode($1, "le", $3); }
83          | Expression GE Expression  { $$ = new CScalarBinaryOpExprNode($1, "ge", $3); }
84          | Expression NE Expression  { $$ = new CScalarBinaryOpExprNode($1, "ne", $3); }
85          | Expression QUESTION_MARK Expression COLON Expression {$$ = new CScalarTernaryOpExprNode($1, "cond", $3, $5);}
86          | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS    { $$ = $2; }
87          | ID LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$ = new CScalarUnaryOpExprNode(*$1, $3); delete $1; }
88          ;
89
90Field_expr:
91            ID      { $$ = new CFilterFieldExprNode(*$1); delete $1; }
92          | AVERAGE { $$ = new CFilterTemporalFieldExprNode(*$1); delete $1; }
93          | Field_expr PLUS Field_expr   { $$ = new CFilterFieldFieldOpExprNode($1, "add", $3); }
94          | Field_expr MINUS Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "minus", $3); }
95          | Field_expr TIMES Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "mult", $3); }
96          | Field_expr DIVIDE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "div", $3); }
97          | MINUS Field_expr %prec NEG   { $$ = new CFilterUnaryOpExprNode("neg", $2); }
98          | Field_expr POWER Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "pow", $3); }
99          | Field_expr EQ Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "eq", $3); }
100          | Field_expr LT Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "lt", $3); }
101          | Field_expr GT Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "gt", $3); }
102          | Field_expr LE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "le", $3); }
103          | Field_expr GE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "ge", $3); }
104          | Field_expr NE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "ne", $3); }
105          | LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS       { $$ = $2; }
106          | Expression QUESTION_MARK Expression COLON Field_expr {$$ = new CFilterScalarScalarFieldOpExprNode($1, "cond",$3, $5);}
107          | Expression QUESTION_MARK Field_expr COLON Expression {$$ = new CFilterScalarFieldScalarOpExprNode($1, "cond",$3, $5);}
108          | Expression QUESTION_MARK Field_expr COLON Field_expr {$$ = new CFilterScalarFieldFieldOpExprNode($1, "cond",$3, $5);}
109          | Field_expr QUESTION_MARK Expression COLON Expression {$$ = new CFilterFieldScalarScalarOpExprNode($1, "cond",$3, $5);}
110          | Field_expr QUESTION_MARK Expression COLON Field_expr {$$ = new CFilterFieldScalarFieldOpExprNode($1, "cond",$3, $5);}
111          | Field_expr QUESTION_MARK Field_expr COLON Expression {$$ = new CFilterFieldFieldScalarOpExprNode($1, "cond",$3, $5);}
112          | Field_expr QUESTION_MARK Field_expr COLON Field_expr {$$ = new CFilterFieldFieldFieldOpExprNode($1, "cond",$3, $5);}
113          | Field_expr PLUS Expression   { $$ = new CFilterFieldScalarOpExprNode($1, "add", $3); }
114          | Expression PLUS Field_expr   { $$ = new CFilterScalarFieldOpExprNode($1, "add", $3); }
115          | Field_expr MINUS Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "minus", $3); }
116          | Expression MINUS Field_expr  { $$ = new CFilterScalarFieldOpExprNode($1, "minus", $3); }
117          | Field_expr TIMES Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "mult", $3); }
118          | Expression TIMES Field_expr  { $$ = new CFilterScalarFieldOpExprNode($1, "mult", $3); }
119          | Field_expr DIVIDE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "div", $3); }
120          | Expression DIVIDE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "div", $3); }
121          | Field_expr POWER Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "pow", $3); }
122          | Field_expr EQ Expression { $$ = new CFilterFieldScalarOpExprNode($1, "eq", $3); }
123          | Expression EQ Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "eq", $3); }
124          | Field_expr LT Expression { $$ = new CFilterFieldScalarOpExprNode($1, "lt", $3); }
125          | Expression LT Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "lt", $3); }
126          | Field_expr GT Expression { $$ = new CFilterFieldScalarOpExprNode($1, "gt", $3); }
127          | Expression GT Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "gt", $3); }
128          | Field_expr LE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "le", $3); }
129          | Expression LE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "le", $3); }
130          | Field_expr GE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "ge", $3); }
131          | Expression GE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "ge", $3); }
132          | Field_expr NE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "ne", $3); }
133          | Expression NE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "ne", $3); }
134          | ID LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$ = new CFilterUnaryOpExprNode(*$1, $3); delete $1; }
135          ;
136%%
137
138extern "C"
139{
140  int yyerror(const char *s)
141  {
142    ERROR("int yyerror(const char *s)", << "Parsing error: " << s << endl);
143  }
144}
145
146namespace xios
147{
148  IFilterExprNode* parseExpr(const string& strExpr)
149  {
150    #pragma omp critical (_parser)
151    {
152      if(globalInputText_ptr == 0) globalInputText_ptr = new std::string;
153      (*globalInputText_ptr).assign (strExpr);
154      globalReadOffset = 0;
155      yyparse();
156    }
157    return parsed;
158  }
159}
160
161
Note: See TracBrowser for help on using the repository browser.