1 | %{ |
---|
2 | #include "simple_node_expr.hpp" |
---|
3 | #include <string> |
---|
4 | #include <iostream> |
---|
5 | #include "exception.hpp" |
---|
6 | |
---|
7 | using namespace std ; |
---|
8 | using namespace xios ; |
---|
9 | |
---|
10 | extern "C" |
---|
11 | { |
---|
12 | int yyparse(void); |
---|
13 | int yylex(void); |
---|
14 | int yyerror(const char *s) ; |
---|
15 | } |
---|
16 | |
---|
17 | CSimpleNodeExpr* parsed ; |
---|
18 | std::string globalInputText; |
---|
19 | int globalReadOffset=0; |
---|
20 | |
---|
21 | int readInputForLexer( char *buffer, int *numBytesRead, int maxBytesToRead ) |
---|
22 | { |
---|
23 | int numBytesToRead = maxBytesToRead; |
---|
24 | int bytesRemaining = globalInputText.length()-globalReadOffset; |
---|
25 | int 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 | |
---|
35 | %union |
---|
36 | { |
---|
37 | std::string* str ; /* symbol table index */ |
---|
38 | CSimpleNodeExpr* node ; |
---|
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 <node> Line Expression Field_expr |
---|
53 | %start Line |
---|
54 | %% |
---|
55 | |
---|
56 | |
---|
57 | Line: |
---|
58 | END { } |
---|
59 | | Field_expr END { parsed=$1 ;} |
---|
60 | ; |
---|
61 | |
---|
62 | Expression: |
---|
63 | NUMBER { $$=new CSimpleNodeExpr(CSimpleNodeExpr::scalarDouble,$1); delete $1 } |
---|
64 | | VAR { $$=new CSimpleNodeExpr(CSimpleNodeExpr::scalarVariable,$1) ; delete $1} |
---|
65 | | Expression PLUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
66 | | Expression MINUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
67 | | Expression TIMES Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
68 | | Expression DIVIDE Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
69 | | MINUS Expression %prec NEG { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalar,"neg") ; $$->addChild($2); } |
---|
70 | | Expression POWER Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"pow") ; $$->addChild($1) ; $$->addChild($3); } |
---|
71 | | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$=$2 ; } |
---|
72 | | ID LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalar,$1) ; $$->addChild($3) ; delete $1 } |
---|
73 | ; |
---|
74 | |
---|
75 | Field_expr: |
---|
76 | ID { $$=new CSimpleNodeExpr(CSimpleNodeExpr::fieldInstant,$1); delete $1} |
---|
77 | | AVERAGE { $$=new CSimpleNodeExpr(CSimpleNodeExpr::fieldAverage,$1); delete $1} |
---|
78 | | Field_expr PLUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
79 | | Field_expr MINUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
80 | | Field_expr TIMES Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
81 | | Field_expr DIVIDE Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
82 | | MINUS Field_expr %prec NEG { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opField,"neg") ; $$->addChild($2);} |
---|
83 | | Field_expr POWER Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"pow") ; $$->addChild($1) ; $$->addChild($3); } |
---|
84 | | LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$=$2 ;} |
---|
85 | | Field_expr PLUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
86 | | Expression PLUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
87 | | Field_expr MINUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
88 | | Expression MINUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
89 | | Field_expr TIMES Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
90 | | Expression TIMES Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
91 | | Field_expr DIVIDE Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
92 | | Expression DIVIDE Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
93 | | Field_expr POWER Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"pow") ; $$->addChild($1) ; $$->addChild($3); } |
---|
94 | | ID LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opField,$1) ; $$->addChild($3) ; delete $1} |
---|
95 | ; |
---|
96 | %% |
---|
97 | |
---|
98 | extern "C" |
---|
99 | { |
---|
100 | int yyerror(const char *s) |
---|
101 | { |
---|
102 | ERROR("int yyerror(const char *s)", <<"Parsing error :"<<s<<endl) ; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | namespace xios |
---|
107 | { |
---|
108 | CSimpleNodeExpr* parseExpr(const string& strExpr) |
---|
109 | { |
---|
110 | globalInputText=strExpr ; |
---|
111 | globalReadOffset=0 ; |
---|
112 | yyparse(); |
---|
113 | return parsed ; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | |
---|