[458] | 1 | %{ |
---|
| 2 | #include "simple_node_expr.hpp" |
---|
| 3 | #include <string> |
---|
| 4 | #include <iostream> |
---|
| 5 | using namespace std ; |
---|
| 6 | extern "C" |
---|
| 7 | { |
---|
| 8 | int yyparse(void); |
---|
| 9 | int yylex(void); |
---|
| 10 | int yyerror(const char *s) ; |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | CSimpleNodeExpr* parsed ; |
---|
| 14 | std::string globalInputText; |
---|
| 15 | int globalReadOffset=0; |
---|
| 16 | |
---|
| 17 | int readInputForLexer( char *buffer, int *numBytesRead, int maxBytesToRead ) |
---|
| 18 | { |
---|
| 19 | int numBytesToRead = maxBytesToRead; |
---|
| 20 | int bytesRemaining = globalInputText.length()-globalReadOffset; |
---|
| 21 | int i; |
---|
| 22 | if ( numBytesToRead > bytesRemaining ) numBytesToRead = bytesRemaining; |
---|
| 23 | for ( i = 0; i < numBytesToRead; i++ ) buffer[i] = globalInputText.c_str()[globalReadOffset+i]; |
---|
| 24 | *numBytesRead = numBytesToRead; |
---|
| 25 | globalReadOffset += numBytesToRead; |
---|
| 26 | return 0; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | %} |
---|
| 30 | |
---|
| 31 | %union |
---|
| 32 | { |
---|
| 33 | std::string* str ; /* symbol table index */ |
---|
| 34 | CSimpleNodeExpr* node ; |
---|
| 35 | }; |
---|
| 36 | |
---|
| 37 | %token <str> NUMBER |
---|
| 38 | %token <str> VAR ID AVERAGE |
---|
| 39 | %token PLUS MINUS TIMES DIVIDE POWER |
---|
| 40 | %token LEFT_PARENTHESIS RIGHT_PARENTHESIS |
---|
| 41 | %token <str> END |
---|
| 42 | |
---|
| 43 | %left PLUS MINUS |
---|
| 44 | %left TIMES DIVIDE |
---|
| 45 | %nonassoc NEG |
---|
| 46 | %right POWER |
---|
| 47 | |
---|
| 48 | %type <node> Line Expression Field_expr |
---|
| 49 | %start Line |
---|
| 50 | %% |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | Line: |
---|
| 54 | END { cout<<"The end: \n"; } |
---|
| 55 | | Field_expr END { cout<<"Parsed END..."<<endl ; parsed=$1 ;} |
---|
| 56 | ; |
---|
| 57 | |
---|
| 58 | Expression: |
---|
| 59 | NUMBER { $$=new CSimpleNodeExpr(CSimpleNodeExpr::scalarDouble,$1); delete $1 } |
---|
| 60 | | VAR { $$=new CSimpleNodeExpr(CSimpleNodeExpr::scalarVariable,$1) ; delete $1} |
---|
| 61 | | Expression PLUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 62 | | Expression MINUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 63 | | Expression TIMES Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 64 | | Expression DIVIDE Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 65 | | MINUS Expression %prec NEG { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalar,"neg") ; $$->addChild($2); } |
---|
| 66 | | Expression POWER Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarScalar,"pow") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 67 | | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$=$2 ; } |
---|
| 68 | | ID LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalar,$1) ; $$->addChild($3) ; delete $1 } |
---|
| 69 | ; |
---|
| 70 | |
---|
| 71 | Field_expr: |
---|
| 72 | ID { $$=new CSimpleNodeExpr(CSimpleNodeExpr::fieldInstant,$1); delete $1} |
---|
| 73 | | AVERAGE { $$=new CSimpleNodeExpr(CSimpleNodeExpr::fieldAverage,$1); delete $1} |
---|
| 74 | | Field_expr PLUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 75 | | Field_expr MINUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 76 | | Field_expr TIMES Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 77 | | Field_expr DIVIDE Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 78 | | MINUS Field_expr %prec NEG { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opField,"neg") ; $$->addChild($2);} |
---|
| 79 | | Field_expr POWER Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldField,"pow") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 80 | | LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$=$2 ;} |
---|
| 81 | | Field_expr PLUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 82 | | Expression PLUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"add") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 83 | | Field_expr MINUS Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 84 | | Expression MINUS Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"minus") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 85 | | Field_expr TIMES Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 86 | | Expression TIMES Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"mult") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 87 | | Field_expr DIVIDE Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 88 | | Expression DIVIDE Field_expr { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opScalarField,"div") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 89 | | Field_expr POWER Expression { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opFieldScalar,"pow") ; $$->addChild($1) ; $$->addChild($3); } |
---|
| 90 | | ID LEFT_PARENTHESIS Field_expr RIGHT_PARENTHESIS { $$=new CSimpleNodeExpr(CSimpleNodeExpr::opField,$1) ; $$->addChild($3) ; delete $1} |
---|
| 91 | ; |
---|
| 92 | %% |
---|
| 93 | |
---|
| 94 | extern "C" |
---|
| 95 | { |
---|
| 96 | int yyerror(const char *s) |
---|
| 97 | { |
---|
| 98 | cout<<"Parsing error :"<<s<<endl ; |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | namespace xios |
---|
| 103 | { |
---|
| 104 | CSimpleNodeExpr* parseExpr(const string& strExpr) |
---|
| 105 | { |
---|
| 106 | cout<<strExpr<<endl ; |
---|
| 107 | globalInputText=strExpr ; |
---|
| 108 | globalReadOffset=0 ; |
---|
| 109 | yyparse(); |
---|
| 110 | return parsed ; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|