Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. %{
  2. #include "compilator.hpp"
  3. #include "compilator.cpp"
  4. #include "arithmetics.cpp"
  5. #include "conditions.cpp"
  6. #include "in-out.cpp"
  7. #include "loops.cpp"
  8. #include "utilities.cpp"
  9. #include "variables.cpp"
  10.  
  11. #include<string>
  12. #include<iostream>
  13. #include<cmath>
  14.  
  15. using namespace std;
  16.  
  17. extern int yylex();
  18. extern int yylineno;
  19. extern FILE *yyin;
  20. int yyerror(string);
  21.  
  22. %}
  23.  
  24. %union{
  25. char* str;
  26. long long int NUMBER;
  27. }
  28.  
  29.  
  30. %token <str> NUMBER MINUS
  31. %token <str> DECLARE _BEGIN END IF THEN ELSE ENDIF
  32. %token <str> WHILE DO ENDWHILE ENDDO FOR FROM ENDFOR
  33. %token <str> COL WRITE READ PIDENTIFER SEM TO DOWNTO COMA
  34. %token <str> LB RB ASSIGN EQ LE GE LEQ GEQ NEQ ADD SUB MUL DIV MOD
  35.  
  36. %type <str> commands
  37. %type <str> value
  38. %type <str> identifier
  39. %type <str> expression
  40. /// returns how many comands are in condition (for while loops)
  41. %type <str> condition
  42.  
  43.  
  44. %%
  45.  
  46. program:
  47. DECLARE declarations _BEGIN commands END {__end();}
  48. | _BEGIN commands END {__end();}
  49. ;
  50.  
  51. declarations:
  52. declarations COMA PIDENTIFER {__declare_variable($3,yylineno);}
  53. | declarations COMA PIDENTIFER LB NUMBER COL NUMBER RB {__declare_array($3,$5,$7,yylineno);}
  54. | PIDENTIFER {__declare_variable($1,yylineno);}
  55. | PIDENTIFER LB NUMBER COL NUMBER RB {__declare_array($1,$3,$5,yylineno);}
  56. ;
  57.  
  58. commands:
  59. commands command {}
  60. | command {}
  61. ;
  62.  
  63. command:
  64. identifier ASSIGN expression SEM {__assign_variable($1,$3,yylineno);}
  65. | IF condition THEN commands {__if_else();}
  66. ELSE commands ENDIF {__end_if_else();}
  67. | IF condition THEN commands ENDIF {__end_if();}
  68. | WHILE condition DO {__loop_while($2);}
  69. commands ENDWHILE {__end_loop_while();}
  70. | FOR PIDENTIFER FROM value TO value DO {__loop_for($2,$4,$6,true,yylineno);}
  71. commands ENDFOR {__end_loop_for(true);}
  72. | FOR PIDENTIFER FROM value DOWNTO value DO {__loop_for($2,$4,$6,false,yylineno);}
  73. commands ENDFOR {__end_loop_for(false);}
  74. | READ identifier SEM {__read($2);}
  75. | WRITE value SEM {__write($2,yylineno);}
  76. | DO {__loop_do_while();}
  77. commands WHILE condition ENDDO {__end_loop_do_while();}
  78. ;
  79.  
  80. expression:
  81. value
  82. | value ADD value {$$ = __add($1,$3,yylineno);}
  83. | value SUB value {$$ = __sub($1,$3,yylineno);}
  84. | value MUL value {$$ = __mul($1,$3,yylineno);}
  85. | value DIV value {$$ = __div($1,$3,yylineno);}
  86. | value MOD value {$$ = __mod($1,$3,yylineno);}
  87. ;
  88.  
  89. condition:
  90. value EQ value {$$ = __EQ($1,$3);}
  91. | value NEQ value {$$ = __NEQ($1,$3);}
  92. | value LE value {$$ = __LE($1,$3);}
  93. | value GE value {$$ = __GE($1,$3);}
  94. | value LEQ value {$$ = __LEQ($1,$3);}
  95. | value GEQ value {$$ = __GEQ($1,$3);}
  96. ;
  97.  
  98. value:
  99. NUMBER {$$ = __create_number($1);}
  100. | identifier
  101. ;
  102.  
  103. identifier:
  104. PIDENTIFER {$$ = __read_variable($1, yylineno);}
  105. | PIDENTIFER LB NUMBER RB {$$ = __read_variable_from_arrayNUM($1,$3,yylineno);}
  106. | PIDENTIFER LB PIDENTIFER RB {$$ = __read_variable_from_arrayVAR($1,$3,yylineno);}
  107. ;
  108.  
  109. %%
  110.  
  111. int yyerror(string err) {
  112. usleep(500000);
  113. cout << "\e[1m\x1B[31m[ ERROR ]\e[0m \e[1m[ LINE " << yylineno << " ] \e[1m\x1B[31m" << err << ".\e[0m\n" << endl;
  114. exit(1);
  115. }
  116.  
  117. int main(int argc, char *argv[])
  118. {
  119. createOnes();
  120. yyin = fopen(argv[1], "r");
  121. if (argv[1] == NULL || argv[2] == NULL){
  122. cout << "\e[1m\x1B[31m[ USAGE ]\e[0m \e[1m compiler_name file_in file_out \e[0m\n" << endl;
  123. exit(1);
  124. }
  125. yyparse();
  126. create_ASM(argv[2]);
  127. cout << "Compiled without errors." << endl;
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement