Advertisement
janschneida

PES Praktikum 2 richtig

Mar 30th, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.97 KB | None | 0 0
  1. %{
  2. /***********************************************************************/
  3. /* A MiniB interpreter with parse tree (c) 2013 by D. Pawelczak        */
  4. /* =================================================================== */
  5. /***********************************************************************/
  6.  
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "parsetree.h"
  10. #include "gsymbols.h"
  11. #include "error.h"
  12.  
  13. #define YYSTYPE tParseTree*
  14. //#define YYERROR_VERBOSE
  15. //#define YYDEBUG 1
  16.  
  17. int yyerror(char* s);
  18. int yylex(void);
  19. int32_t value; /* index in symbol table */
  20.  
  21. %}
  22.  
  23. %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
  24. %start interpreter
  25.  
  26. %%
  27. interpreter : program TOKEN_EOF                                    { return 0; /* leave the parser */ }
  28.     | program testcall interpreter
  29.     | testcall interpreter
  30.     | TOKEN_EOF                                                    { return 0; /* leave the parser */ }
  31.     ;
  32.  
  33. testcall: TOKEN_COND function TOKEN_CLOSE_BRACKET                  { printf("Executing %s() = %f\n", GetIdentifierName($2->value)+1, EvaluateFunction($2->value,NULL)); }
  34.     | TOKEN_COND function expression TOKEN_CLOSE_BRACKET           { printf("Executing %s(%f) = %f\n", GetIdentifierName($2->value)+1, EvaluateTree($3, NULL) , EvaluateFunction($2->value,$3)); }
  35.     ;
  36.  
  37. identifier : TOKEN_IDENTIFIER                                      { $$=CreateNode(TOKEN_IDENTIFIER, value); }
  38.     ;
  39.  
  40. function   : TOKEN_FUNCTION                                        { $$=CreateNode(TOKEN_FUNCTION, value); }
  41.     ;
  42.  
  43. number : TOKEN_FLOAT_LITERAL                                       { $$=CreateNode(TOKEN_FLOAT_LITERAL, value); }
  44.     ;                                        
  45.  
  46.    
  47. program : program definition
  48.     | definition       
  49.     ;
  50.  
  51. definition : function TOKEN_CLOSE_BRACKET TOKEN_BEGIN expression TOKEN_SEMICOLON TOKEN_END     {  CreateAndAddFunctionNode($1->value,$4, NULL ); }
  52.     | function identifier TOKEN_CLOSE_BRACKET TOKEN_BEGIN expression TOKEN_SEMICOLON TOKEN_END {  CreateAndAddFunctionNode($1->value,$5, $2 ); }
  53.     ;
  54.  
  55. expression : condition { $$ = $1 }
  56.     ;
  57.  
  58. condition : sum        { $$ = $1 }
  59.     | comparisson TOKEN_COND expression TOKEN_COLON expression { $$ = CreateNode(TOKEN_COND, NULL);$$->left=$1; $$->right=CreateNode(TOKEN_COLON, NULL); $$->right->right=$5; $$->right->left=$3;  }
  60.     ;
  61.  
  62. comparisson : sum TOKEN_GT sum  { $$ = CreateNode(TOKEN_GT, NULL); $$->left=$1; $$->right=$3; }
  63.     |sum TOKEN_GE sum  { $$ = CreateNode(TOKEN_GE, NULL); $$->left=$1; $$->right=$3; }
  64.     |sum TOKEN_EQ sum  { $$ = CreateNode(TOKEN_EQ, NULL); $$->left=$1; $$->right=$3; }
  65.     |sum TOKEN_LS sum  { $$ = CreateNode(TOKEN_LS, NULL); $$->left=$1; $$->right=$3; }
  66.     |sum TOKEN_LE sum  { $$ = CreateNode(TOKEN_LE, NULL); $$->left=$1; $$->right=$3; }
  67.     |sum TOKEN_UNEQUAL sum { $$ = CreateNode(TOKEN_UNEQUAL,NULL); $$->left=$1; $$->right=$3; }
  68.     ;
  69.  
  70. sum : sum TOKEN_ADD term { $$ = CreateNode(TOKEN_ADD, NULL); $$->left=$1; $$->right=$3; }
  71.     | sum TOKEN_SUB term { $$ = CreateNode(TOKEN_SUB, NULL); $$->left=$1; $$->right=$3; }
  72.     | term               { $$ = $1 }
  73.     ;
  74.  
  75. term : term TOKEN_MUL factor { $$ = CreateNode(TOKEN_MUL, NULL); $$->left=$1; $$->right=$3; }
  76.     | term TOKEN_DIV factor  { $$ = CreateNode(TOKEN_DIV, NULL); $$->left=$1; $$->right=$3; }
  77.     | term TOKEN_MOD factor  { $$ = CreateNode(TOKEN_MOD, NULL); $$->left=$1; $$->right=$3; }
  78.     | factor                 { $$ = $1 }
  79.     ;
  80.  
  81. factor : number             { $$ = $1 }
  82.     | TOKEN_ADD number      { $$ = $2 }
  83.     | TOKEN_SUB number     
  84.     | identifier            { $$ = $1 }
  85.     | functionCall          { $$ = $1 }
  86.     | TOKEN_OPEN_BRACKET expression TOKEN_CLOSE_BRACKET { $$ = $2 }
  87.     ;
  88.  
  89. functionCall : function TOKEN_CLOSE_BRACKET   { $$ = $1 }
  90.     | function expression TOKEN_CLOSE_BRACKET { $$ = $1; $$->right=$2; }
  91. %%
  92.  
  93. int yyerror(char* s)
  94. {
  95.   error(s);
  96.   return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement