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

Last change on this file since 642 was 642, checked in by rlacroix, 6 years ago

Use the filter infrastructure to handle the expressions

Parse the expressions to get a new tree representation that can be converted to a filter graph based on new arithmetic filters.

Temporal operations are still unsupported.

  • 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
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 LEFT_PARENTHESIS RIGHT_PARENTHESIS
45%token <str> END
46
47%left PLUS MINUS
48%left TIMES DIVIDE
49%nonassoc NEG
50%right POWER
51
52%type <scalarNode> Expression
53%type <filterNode> Line Field_expr
54%start Line
55%%
56
57
58Line:
59     END            { /* Nothing to do */ }
60   | Field_expr END { parsed = $1; }
61  ;
62
63Expression:
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; }
74          ;
75
76Field_expr:
77            ID      { $$ = new CFilterFieldExprNode(*$1); delete $1; }
78          | AVERAGE { /* TODO: Use temporal operation */ $$ = new CFilterFieldExprNode(*$1); delete $1; }
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; }
96          ;
97%%
98
99extern "C"
100{
101  int yyerror(const char *s)
102  {
103    ERROR("int yyerror(const char *s)", << "Parsing error: " << s << endl);
104  }
105}
106
107namespace xios
108{
109  IFilterExprNode* parseExpr(const string& strExpr)
110  {
111    globalInputText = strExpr;
112    globalReadOffset = 0;
113    yyparse();
114    return parsed;
115  }
116}
117
118
Note: See TracBrowser for help on using the repository browser.