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

Last change on this file since 643 was 643, checked in by rlacroix, 9 years ago

Use the filter infrastructure to handle the temporal operations.

Add a temporal filter to do so.

  • 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: 4.3 KB
RevLine 
[458]1%{
[642]2#include "filter_expr_node.hpp"
[458]3#include <string>
4#include <iostream>
[493]5#include "exception.hpp"
6
[642]7using namespace std;
8using namespace xios;
[493]9
[458]10extern "C"
11{
12  int yyparse(void);
13  int yylex(void);
[642]14  int yyerror(const char *s);
[458]15}
16
[642]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];
[458]28    *numBytesRead = numBytesToRead;
29    globalReadOffset += numBytesToRead;
30    return 0;
[642]31  }
[458]32%}
33
34%union
35{
[642]36  std::string* str;                /* symbol table index */
37  xios::IScalarExprNode* scalarNode;
38  xios::IFilterExprNode* filterNode;
[458]39};
40
41%token <str> NUMBER
[642]42%token <str> VAR ID AVERAGE
[458]43%token PLUS MINUS TIMES DIVIDE POWER
44%token LEFT_PARENTHESIS RIGHT_PARENTHESIS
45%token <str> END
46
47%left PLUS MINUS
48%left TIMES DIVIDE
49%nonassoc NEG
50%right POWER
51
[642]52%type <scalarNode> Expression
53%type <filterNode> Line Field_expr
[458]54%start Line
55%%
56
57
58Line:
[642]59     END            { /* Nothing to do */ }
60   | Field_expr END { parsed = $1; }
61  ;
[458]62
63Expression:
[642]64            NUMBER { $$ = new CScalarValExprNode(*$1); delete $1; }
65          | VAR    { $$ = new CScalarVarExprNode(*$1); delete $1; }
66          | Expression PLUS Expression   { $$ = new CScalarBinaryOpExprNode($1, "add", $3); }
67          | Expression MINUS Expression  { $$ = new CScalarBinaryOpExprNode($1, "minus", $3); }
68          | Expression TIMES Expression  { $$ = new CScalarBinaryOpExprNode($1, "mult", $3); }
69          | Expression DIVIDE Expression { $$ = new CScalarBinaryOpExprNode($1, "div", $3); }
70          | MINUS Expression %prec NEG   { $$ = new CScalarUnaryOpExprNode("neg", $2); }
71          | Expression POWER Expression  { $$ = new CScalarBinaryOpExprNode($1, "pow", $3); }
72          | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS    { $$ = $2; }
73          | ID LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$ = new CScalarUnaryOpExprNode(*$1, $3); delete $1; }
[458]74          ;
75
76Field_expr:
[642]77            ID      { $$ = new CFilterFieldExprNode(*$1); delete $1; }
[643]78          | AVERAGE { $$ = new CFilterTemporalFieldExprNode(*$1); delete $1; }
[642]79          | Field_expr PLUS Field_expr   { $$ = new CFilterFieldFieldOpExprNode($1, "add", $3); }
80          | Field_expr MINUS Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "minus", $3); }
81          | Field_expr TIMES Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "mult", $3); }
82          | Field_expr DIVIDE Field_expr { $$ = new CFilterFieldFieldOpExprNode($1, "div", $3); }
83          | MINUS Field_expr %prec NEG   { $$ = new CFilterUnaryOpExprNode("neg", $2); }
84          | Field_expr POWER Field_expr  { $$ = new CFilterFieldFieldOpExprNode($1, "pow", $3); }
85          | LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS       { $$ = $2; }
86          | Field_expr PLUS Expression   { $$ = new CFilterFieldScalarOpExprNode($1, "add", $3); }
87          | Expression PLUS Field_expr   { $$ = new CFilterScalarFieldOpExprNode($1, "add", $3); }
88          | Field_expr MINUS Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "minus", $3); }
89          | Expression MINUS Field_expr  { $$ = new CFilterScalarFieldOpExprNode($1, "minus", $3); }
90          | Field_expr TIMES Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "mult", $3); }
91          | Expression TIMES Field_expr  { $$ = new CFilterScalarFieldOpExprNode($1, "mult", $3); }
92          | Field_expr DIVIDE Expression { $$ = new CFilterFieldScalarOpExprNode($1, "div", $3); }
93          | Expression DIVIDE Field_expr { $$ = new CFilterScalarFieldOpExprNode($1, "div", $3); }
94          | Field_expr POWER Expression  { $$ = new CFilterFieldScalarOpExprNode($1, "pow", $3); }
95          | ID LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$ = new CFilterUnaryOpExprNode(*$1, $3); delete $1; }
[458]96          ;
97%%
98
99extern "C"
100{
[642]101  int yyerror(const char *s)
[458]102  {
[642]103    ERROR("int yyerror(const char *s)", << "Parsing error: " << s << endl);
[458]104  }
105}
106
107namespace xios
108{
[642]109  IFilterExprNode* parseExpr(const string& strExpr)
[458]110  {
[642]111    globalInputText = strExpr;
112    globalReadOffset = 0;
[458]113    yyparse();
[642]114    return parsed;
[458]115  }
116}
117
118
Note: See TracBrowser for help on using the repository browser.