Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- /***********************************************************************/
- /* A MiniB interpreter with parse tree (c) 2013 by D. Pawelczak */
- /* =================================================================== */
- /***********************************************************************/
- #include <stdlib.h>
- #include <string.h>
- #include "parsetree.h"
- #include "gsymbols.h"
- #include "error.h"
- #define YYSTYPE tParseTree*
- //#define YYERROR_VERBOSE
- //#define YYDEBUG 1
- int yyerror(char* s);
- int yylex(void);
- int32_t value; /* index in symbol table */
- %}
- %token TOKEN_OPEN_BRACKET TOKEN_CLOSE_BRACKET TOKEN_SUB TOKEN_ADD TOKEN_MUL TOKEN_DIV TOKEN_MOD TOKEN_GT TOKEN_GE TOKEN_EQ TOKEN_LS TOKEN_LE TOKEN_UNEQUAL TOKEN_COND TOKEN_ASSIGN TOKEN_COLON TOKEN_SEMICOLON TOKEN_COMMA TOKEN_BEGIN TOKEN_END TOKEN_FLOAT_LITERAL TOKEN_IDENTIFIER TOKEN_FUNCTION TOKEN_EOF
- %start interpreter
- %%
- interpreter : program TOKEN_EOF { return 0; /* leave the parser */ }
- | program testcall interpreter
- | testcall interpreter
- | TOKEN_EOF { return 0; /* leave the parser */ }
- ;
- testcall: TOKEN_COND function TOKEN_CLOSE_BRACKET { printf("Executing %s() = %f\n", GetIdentifierName($2->value)+1, EvaluateFunction($2->value,NULL)); }
- | TOKEN_COND function expression TOKEN_CLOSE_BRACKET { printf("Executing %s(%f) = %f\n", GetIdentifierName($2->value)+1, EvaluateTree($3, NULL) , EvaluateFunction($2->value,$3)); }
- ;
- identifier : TOKEN_IDENTIFIER { $$=CreateNode(TOKEN_IDENTIFIER, value); }
- ;
- function : TOKEN_FUNCTION { $$=CreateNode(TOKEN_FUNCTION, value); }
- ;
- number : TOKEN_FLOAT_LITERAL { $$=CreateNode(TOKEN_FLOAT_LITERAL, value); }
- ;
- program : program definition
- | definition
- ;
- definition : function TOKEN_CLOSE_BRACKET TOKEN_BEGIN expression TOKEN_SEMICOLON TOKEN_END { CreateAndAddFunctionNode($1->value,$4, NULL ); }
- | function identifier TOKEN_CLOSE_BRACKET TOKEN_BEGIN expression TOKEN_SEMICOLON TOKEN_END { CreateAndAddFunctionNode($1->value,$5, $2 ); }
- ;
- expression : condition { $$ = $1 }
- ;
- condition : sum { $$ = $1 }
- | comparisson TOKEN_COND expression TOKEN_COLON expression { $$ = CreateNode(TOKEN_COND, NULL);$$->left=$1; $$->right=CreateNode(TOKEN_COLON, NULL); $$->right->right=$5; $$->right->left=$3; }
- ;
- comparisson : sum TOKEN_GT sum { $$ = CreateNode(TOKEN_GT, NULL); $$->left=$1; $$->right=$3; }
- |sum TOKEN_GE sum { $$ = CreateNode(TOKEN_GE, NULL); $$->left=$1; $$->right=$3; }
- |sum TOKEN_EQ sum { $$ = CreateNode(TOKEN_EQ, NULL); $$->left=$1; $$->right=$3; }
- |sum TOKEN_LS sum { $$ = CreateNode(TOKEN_LS, NULL); $$->left=$1; $$->right=$3; }
- |sum TOKEN_LE sum { $$ = CreateNode(TOKEN_LE, NULL); $$->left=$1; $$->right=$3; }
- |sum TOKEN_UNEQUAL sum { $$ = CreateNode(TOKEN_UNEQUAL,NULL); $$->left=$1; $$->right=$3; }
- ;
- sum : sum TOKEN_ADD term { $$ = CreateNode(TOKEN_ADD, NULL); $$->left=$1; $$->right=$3; }
- | sum TOKEN_SUB term { $$ = CreateNode(TOKEN_SUB, NULL); $$->left=$1; $$->right=$3; }
- | term { $$ = $1 }
- ;
- term : term TOKEN_MUL factor { $$ = CreateNode(TOKEN_MUL, NULL); $$->left=$1; $$->right=$3; }
- | term TOKEN_DIV factor { $$ = CreateNode(TOKEN_DIV, NULL); $$->left=$1; $$->right=$3; }
- | term TOKEN_MOD factor { $$ = CreateNode(TOKEN_MOD, NULL); $$->left=$1; $$->right=$3; }
- | factor { $$ = $1 }
- ;
- factor : number { $$ = $1 }
- | TOKEN_ADD number { $$ = $2 }
- | TOKEN_SUB number
- | identifier { $$ = $1 }
- | functionCall { $$ = $1 }
- | TOKEN_OPEN_BRACKET expression TOKEN_CLOSE_BRACKET { $$ = $2 }
- ;
- functionCall : function TOKEN_CLOSE_BRACKET { $$ = $1 }
- | function expression TOKEN_CLOSE_BRACKET { $$ = $1; $$->right=$2; }
- %%
- int yyerror(char* s)
- {
- error(s);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement