Advertisement
Guest User

Untitled

a guest
May 20th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. %baseclass-preinclude "semantics.h"
  2. %lsp-needed
  3.  
  4. %token NATURAL;
  5. %token BOOLEAN;
  6. %token TRUE;
  7. %token FALSE;
  8. %token NUMBER;
  9. %token <szoveg> IDENT;
  10. %token ASSIGN;
  11. %right PLUS;
  12.  
  13. %type <expr_d> expr;
  14. %type <expr_d> declaration;
  15. %type <instr_d> declarations;
  16. %type <expr_d> assignment;
  17. %type <instr_d> assignments;
  18.  
  19. %union
  20. {
  21. std::string *szoveg;
  22. expr_data *expr_d;
  23. instr_data *instr_d;
  24. }
  25.  
  26. %%
  27.  
  28. start:
  29. declarations assignments
  30. {
  31. std::cout << std::string("") +
  32. "extern ki_elojeles_egesz\n" +
  33. "extern ki_logikai\n" +
  34. "global main\n" +
  35. "section .bss\n" +
  36. $1->code +
  37. "section .text\n" +
  38. "main:\n" +
  39. $2->code +
  40. "ret\n";
  41. delete $1;
  42. delete $2;
  43. }
  44. ;
  45.  
  46. declarations:
  47. // ures
  48. {
  49. $$ = new instr_data(d_loc__.first_line, "");
  50. }
  51. |
  52. declaration declarations
  53. {
  54. std::string fun;
  55. fun = ($1->var_type == natural ? "ki_elojeles_egesz" : "ki_logikai");
  56. $$ = new instr_data( $1->decl_row,
  57. $1->code +
  58. $2->code
  59. );
  60. delete $1;
  61. delete $2;
  62. }
  63. ;
  64.  
  65. declaration:
  66. NATURAL IDENT
  67. {
  68. if( szimbolumtabla.count(*$2) > 0 )
  69. {
  70. std::stringstream ss;
  71. ss << "Ujradefinialt valtozo: " << *$2 << std::endl
  72. << "Korabbi deklaracio sora: " << szimbolumtabla[*$2].decl_row;
  73. error( ss.str().c_str() );
  74. }
  75. szimbolumtabla[*$2] = var_data( d_loc__.first_line, natural );
  76. $$ = new expr_data(d_loc__.first_line, natural, *$2 + ": resd 1\n");
  77. delete $2;
  78. }
  79. |
  80. BOOLEAN IDENT
  81. {
  82. if( szimbolumtabla.count(*$2) > 0 )
  83. {
  84. std::stringstream ss;
  85. ss << "Ujradefinialt valtozo: " << *$2 << std::endl
  86. << "Korabbi deklaracio sora: " << szimbolumtabla[*$2].decl_row;
  87. error( ss.str().c_str() );
  88. }
  89. szimbolumtabla[*$2] = var_data( d_loc__.first_line, boolean );
  90. $$ = new expr_data(d_loc__.first_line, boolean, *$2 + ": resb 1\n");
  91. delete $2;
  92. }
  93. ;
  94.  
  95. assignments:
  96. // ures
  97. {
  98. $$ = new instr_data(d_loc__.first_line, "");
  99. }
  100. |
  101. assignment assignments
  102. {
  103. $$ = new instr_data( $1->decl_row,
  104. $1->code +
  105. $2->code
  106. );
  107. }
  108. ;
  109.  
  110. assignment:
  111. IDENT ASSIGN expr
  112. {
  113. if( szimbolumtabla.count(*$1) == 0 )
  114. {
  115. error( (std::string("Nem definialt valtozo: ") + *$1).c_str() );
  116. }
  117. if( szimbolumtabla[*$1].var_type != $3->var_type )
  118. {
  119. error( "Tipushibas ertekadas." );
  120. }
  121. $$ = new expr_data(d_loc__.first_line, szimbolumtabla[*$1].var_type,
  122. "mov eax, " +
  123. $3->code +
  124. "mov " +
  125. *$1 +
  126. ", eax\n"
  127. );
  128. delete $1;
  129. delete $3;
  130. }
  131. ;
  132.  
  133. expr:
  134. IDENT
  135. {
  136. if( szimbolumtabla.count(*$1) == 0 )
  137. {
  138. error( (std::string("Nem definialt valtozo: ") + *$1).c_str() );
  139. }
  140. $$ = new expr_data(d_loc__.first_line, szimbolumtabla[*$1].var_type, "mov eax, " + *$1 + "\n");
  141. delete $1;
  142. }
  143. |
  144. NUMBER
  145. {
  146. $$ = new expr_data(d_loc__.first_line, natural, "mov eax, " + *$1 + "\n");
  147. }
  148. |
  149. TRUE
  150. {
  151. $$ = new expr_data(d_loc__.first_line, boolean, "mov eax, 1\n");
  152. }
  153. |
  154. FALSE
  155. {
  156. $$ = new expr_data(d_loc__.first_line, boolean, "mov eax, 0\n");
  157. }
  158. | expr PLUS expr
  159. {
  160. if( $1->var_type != natural || $3->var_type != natural ) {
  161. error( (std::string("Típushibás kifejezés")).c_str() );
  162. }
  163. $$ = new expr_data(d_loc__.first_line, natural, "mov eax, " + $3->code +
  164. "push eax\n" + "mov eax, " + $1->code +
  165. "pop ebx\n"
  166. "add eax, ebx\n"
  167. );
  168. delete $1;
  169. delete $3;
  170. }
  171. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement