Guest User

Untitled

a guest
May 28th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.00 KB | None | 0 0
  1. %{
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define YYDEBUG 1
  7.  
  8. #include "declaration.h"
  9. #include "commands.h"
  10.  
  11. /* Ilość błędów */
  12. int errors;
  13.  
  14. /* Struktura dla etykiet do petli IF, WHILE */
  15. struct lbs
  16. {
  17. int for_goto;
  18. int for_jmp_false;
  19. };
  20.  
  21. /* Inicjalizacja nowej struktury */
  22. struct lbs * newlblrec()
  23. {
  24. /* malloc zwraca *void więc należy rzutować na strukturę ponownie */
  25. return (struct lbs *) malloc(sizeof(struct lbs));
  26. }
  27.  
  28. /* Deklaracja zmiennej */
  29. dec(char *name)
  30. {
  31. var *ID;
  32. ID = decDone(name); //check if prevoiusly declared
  33.  
  34. if (ID == 0)
  35. {
  36. ID = decNew(name);
  37. }
  38. else
  39. {
  40. errors++;
  41. printf("%s jest już zdeklarowany\n", name);
  42. }
  43. }
  44.  
  45. /* Sprawdzenie zadeklarowania */
  46. is_declare(char *command, char *name)
  47. {
  48. var *ID;
  49. ID = decDone(name);
  50.  
  51. if(ID == 0)
  52. {
  53. errors++;
  54. printf("%s nie jest zdeklarowany\n", name);
  55. }
  56. else
  57. {
  58. gen_code(command, ID->nr);
  59. }
  60. }
  61.  
  62. %}
  63.  
  64. %union semrec
  65. {
  66. int val;
  67. char *id;
  68. struct lbs *lbls;
  69. }
  70.  
  71. %start program
  72. %token <val> NUM
  73. %token <id> ID
  74. %token <lbls> IF WHILE
  75. %token THEN ELSE DO END
  76. %token READ PRINT VAR START
  77. %token ASSIGN
  78. %left EQ NEQ LT GT LTEQ GTEQ
  79. %left '-' '+'
  80. %left '*' '/'
  81.  
  82. %%
  83.  
  84. program : VAR
  85. declarations
  86. START
  87. commands
  88. END { gen_code("HALT", -1); YYACCEPT; }
  89. ;
  90. declarations : /* empty */
  91. | id_seq ID { dec($2); }
  92. ;
  93. id_seq : /* empty */
  94. | id_seq ID { dec($2); }
  95. ;
  96. commands : commands command
  97. |
  98. ;
  99. command : ID ASSIGN exp ';' { is_declare("STORE", $1); }
  100. | READ ID ';' { is_declare("READ", $2); }
  101. | PRINT ID ';' { is_declare("PRINT", $2); }
  102. | PRINT NUM ';' {
  103. gen_code("ZERO", -1);
  104. gen_code("ADDC", $2);
  105. is_declare("STORE", "#print0");
  106. is_declare("PRINT", "#print0")
  107. }
  108. | IF condition {
  109.  
  110. $1 = (struct lbs *) newlblrec();
  111. $1->for_jmp_false = reserve_loc();
  112. }
  113. THEN commands {
  114.  
  115. $1->for_goto = reserve_loc(); }
  116. ELSE {
  117.  
  118. back_patch($1->for_jmp_false, "JGE", gen_label()); }
  119. commands
  120. END {
  121.  
  122. back_patch($1->for_goto, "JUMP", gen_label()); }
  123. | WHILE {
  124. $1 = (struct lbs *) newlblrec();
  125. $1->for_goto = gen_label();
  126. }
  127. condition { $1->for_jmp_false = reserve_loc(); }
  128. DO
  129. commands
  130. END {
  131. gen_code("JUMP", $1->for_goto );
  132. back_patch( $1->for_jmp_false, "JGE", gen_label());
  133. }
  134. ;
  135. exp : NUM { gen_code("ZERO", -1);
  136. gen_code("ADDC", $1);
  137. }
  138. | ID { is_declare("LOAD", $1); }
  139. | NUM '+' NUM { gen_code("ZERO", -1);
  140. gen_code("ADDC", $1);
  141. gen_code("ADDC", $3);
  142. }
  143. | ID '+' ID { is_declare("LOAD", $1);
  144. is_declare("ADD", $3);
  145. }
  146. | ID '+' NUM { is_declare("LOAD", $1);
  147. gen_code("ADDC", $3);
  148. }
  149. | NUM '+' ID { gen_code("ZERO", -1);
  150. gen_code("ADDC", $1);
  151. is_declare("ADD", $3);
  152. }
  153. | NUM '-' NUM { gen_code("ZERO", -1);
  154. gen_code("ADDC", $1);
  155. gen_code("SUBC", $3);
  156. }
  157. | ID '-' ID { is_declare("LOAD", $1);
  158. is_declare("SUB", $3);
  159. }
  160. | ID '-' NUM { is_declare("LOAD", $1);
  161. gen_code("SUBC", $3);
  162. }
  163. | NUM '-' ID { gen_code("ZERO", -1);
  164. gen_code("ADDC", $1);
  165. is_declare("SUB", $3);
  166. }
  167. | NUM '*' NUM { gen_code("ZERO",-1);
  168. gen_code("ADDC", ($1*$3));}
  169.  
  170. | NUM '*' ID {
  171.  
  172. if($1==0)
  173. gen_code("ZERO",-1);
  174. else if ($1==1)
  175. is_declare("LOAD",$3);
  176. else
  177. {
  178.  
  179. gen_code("ZERO", -1);
  180. is_declare("STORE", "xA");
  181. gen_code("ZERO",-1);
  182. gen_code("ADDC",$1);
  183. is_declare("SUB",$3);
  184. gen_code("JGE", gen_label()+7);
  185.  
  186. gen_code("ZERO",-1);
  187. gen_code("ADDC", $1);
  188. is_declare("STORE", "xB");
  189. is_declare("LOAD", $3);
  190. is_declare("STORE", "xC");
  191. gen_code("JUMP",gen_label()+6);
  192.  
  193. gen_code("ZERO",-1);
  194. gen_code("ADDC", $1);
  195. is_declare("STORE", "xC");
  196. is_declare("LOAD", $3);
  197. is_declare("STORE", "xB");
  198.  
  199. is_declare("LOAD", "#jeden");
  200. is_declare("STORE", "xD");
  201. is_declare("ADD", "xD");
  202. is_declare("SUB", "xB");
  203. gen_code("JGE", gen_label()+10);
  204. is_declare("LOAD", "xC");
  205. is_declare("ADD", "xC");
  206. is_declare("STORE", "xC");
  207. is_declare("LOAD", "xD");
  208. is_declare("ADD", "xD");
  209. is_declare("STORE", "xD");
  210. is_declare("LOAD", "xD");
  211. is_declare("ADD", "xD");
  212. gen_code("JUMP", gen_label()-10);
  213. is_declare("LOAD", "xB");
  214. is_declare("SUB", "xD");
  215. is_declare("STORE", "xB");
  216. is_declare("LOAD", "xC");
  217. is_declare("ADD", "xA");
  218. is_declare("STORE", "xA");
  219. gen_code("ZERO", -1);
  220. is_declare("ADD", "xB");
  221. gen_code("JZ", gen_label()+4);
  222. is_declare("LOAD", $3);
  223. is_declare("STORE", "xC");
  224. gen_code("JUMP", gen_label()-25);
  225. is_declare("LOAD", "xA");
  226. }
  227. }
  228.  
  229. | ID '*' NUM {
  230.  
  231. if($3==0)
  232. {
  233. gen_code("ZERO",-1);
  234. }
  235. else if ($3==1)
  236. {
  237. is_declare("LOAD",$1);
  238. }
  239. else
  240. {
  241.  
  242. gen_code("ZERO", -1);
  243. is_declare("STORE", "xA");
  244. is_declare("LOAD",$1);
  245. gen_code("SUBC",$3);
  246. gen_code("JGE",gen_label()+7);
  247.  
  248. is_declare("LOAD", $1);
  249. is_declare("STORE", "xB");
  250. gen_code("ZERO",-1);
  251. gen_code("ADDC", $3);
  252. is_declare("STORE", "xC");
  253. gen_code("JUMP", gen_label()+6);
  254.  
  255. is_declare("LOAD", $1);
  256. is_declare("STORE", "xC");
  257. gen_code("ZERO",-1);
  258. gen_code("ADDC", $3);
  259. is_declare("STORE", "xB");
  260.  
  261. is_declare("LOAD", "#jeden");
  262. is_declare("STORE", "xD");
  263. is_declare("ADD", "xD");
  264. is_declare("SUB", "xB");
  265. gen_code("JGE", gen_label()+10);
  266. is_declare("LOAD", "xC");
  267. is_declare("ADD", "xC");
  268. is_declare("STORE", "xC");
  269. is_declare("LOAD", "xD");
  270. is_declare("ADD", "xD");
  271. is_declare("STORE", "xD");
  272. is_declare("LOAD", "xD");
  273. is_declare("ADD", "xD");
  274. gen_code("JUMP", gen_label()-10);
  275. is_declare("LOAD", "xB");
  276. is_declare("SUB", "xD");
  277. is_declare("STORE", "xB");
  278. is_declare("LOAD", "xC");
  279. is_declare("ADD", "xA");
  280. is_declare("STORE", "xA");
  281. gen_code("ZERO", -1);
  282. is_declare("ADD", "xB");
  283. gen_code("JZ", gen_label()+5);
  284. gen_code("ZERO",-1);
  285. gen_code("ADDC", $3);
  286. is_declare("STORE", "xC");
  287. gen_code("JUMP", gen_label()-26);
  288. is_declare("LOAD", "xA");
  289. }
  290. }
  291. | ID '*' ID {
  292.  
  293. gen_code("ZERO", -1);
  294. is_declare("STORE", "xA");
  295. is_declare("LOAD",$1);
  296. is_declare("SUB",$3);
  297. gen_code("JGE",gen_label()+6);
  298.  
  299. is_declare("LOAD", $1);
  300. is_declare("STORE", "xB");
  301. is_declare("LOAD", $3);
  302. is_declare("STORE", "xC");
  303. gen_code("JUMP",gen_label()+5);
  304.  
  305. is_declare("LOAD", $1);
  306. is_declare("STORE", "xC");
  307. is_declare("LOAD", $3);
  308. is_declare("STORE", "xB");
  309.  
  310. is_declare("LOAD", "#jeden");
  311. is_declare("STORE", "xD");
  312. is_declare("ADD", "xD");
  313. is_declare("SUB", "xB");
  314. gen_code("JGE", gen_label()+10);
  315. is_declare("LOAD", "xC");
  316. is_declare("ADD", "xC");
  317. is_declare("STORE", "xC");
  318. is_declare("LOAD", "xD");
  319. is_declare("ADD", "xD");
  320. is_declare("STORE", "xD");
  321. is_declare("LOAD", "xD");
  322. is_declare("ADD", "xD");
  323. gen_code("JUMP", gen_label()-10);
  324. is_declare("LOAD", "xB");
  325. is_declare("SUB", "xD");
  326. is_declare("STORE", "xB");
  327. is_declare("LOAD", "xC");
  328. is_declare("ADD", "xA");
  329. is_declare("STORE", "xA");
  330. gen_code("ZERO", -1);
  331. is_declare("ADD", "xB");
  332. gen_code("JZ", gen_label()+4);
  333. is_declare("LOAD", $3);
  334. is_declare("STORE", "xC");
  335. gen_code("JUMP", gen_label()-25);
  336. is_declare("LOAD", "xA");
  337. }
  338.  
  339. | NUM '/' NUM {
  340. if($3==0)
  341. {
  342. gen_code("ZERO",-1);
  343. }
  344. else if($1/$3<=0)
  345. gen_code("ZERO",-1);
  346. else
  347. {
  348. gen_code("ZERO",-1);
  349. gen_code("ADDC", ($1/$3));
  350. }
  351. }
  352.  
  353. | NUM '/' ID
  354. {
  355. if($1==0)
  356. gen_code("ZERO",-1);
  357. else
  358. {
  359. gen_code("ZERO", -1);
  360. is_declare("ADD", $3);
  361. gen_code("JZ", gen_label()+33);
  362. is_declare("STORE", "xB");
  363. gen_code("ZERO", -1);
  364. gen_code("ADDC", $1);
  365. is_declare("STORE", "xA");
  366. is_declare("LOAD", "#jeden");
  367. is_declare("STORE", "xD");
  368. is_declare("LOAD", $3);
  369. is_declare("STORE", "xC");
  370. is_declare("ADD", "xC");
  371. is_declare("SUB", "xA");
  372. gen_code("JGE", gen_label()+9);
  373. is_declare("LOAD", "xD");
  374. is_declare("ADD", "xD");
  375. is_declare("STORE", "xD");
  376. is_declare("LOAD", "xC");
  377. is_declare("ADD", "xC");
  378. is_declare("STORE", "xC");
  379. is_declare("ADD", "xC");
  380. gen_code("JUMP", gen_label()-9); //
  381. is_declare("LOAD", "xC");
  382. is_declare("SUB", "xA");
  383. gen_code("JGE", gen_label()+7);
  384. is_declare("LOAD", "xA");
  385. is_declare("SUB", "xC");
  386. is_declare("STORE", "xA");
  387. is_declare("LOAD", "xB");
  388. is_declare("ADD", "xD");
  389. is_declare("STORE", "xB");
  390. is_declare("LOAD", $3);
  391. is_declare("SUB", "xA");
  392. gen_code("JGE", gen_label()+4);
  393. gen_code("JUMP", gen_label()-27);
  394. gen_code("ZERO", -1);
  395. gen_code("JUMP", gen_label()+2);
  396. is_declare("LOAD", "xB");
  397. is_declare("SUB", $3);
  398.  
  399. }
  400. }
  401.  
  402. | ID '/' NUM
  403.  
  404. {
  405. if($3==0)
  406. gen_code("ZERO",-1);
  407. else if($3==1)
  408. is_declare("LOAD", $1);
  409. else
  410. {
  411. gen_code("ZERO", -1);
  412. gen_code("ADDC", $3);
  413. gen_code("JZ", gen_label()+34);
  414. is_declare("STORE", "xB");
  415. is_declare("LOAD", $1);
  416. is_declare("STORE", "xA");
  417. is_declare("LOAD", "#jeden");
  418. is_declare("STORE", "xD");
  419. gen_code("ZERO", -1);
  420. gen_code("ADDC", $3);
  421. is_declare("STORE", "xC");
  422. is_declare("ADD", "xC");
  423. is_declare("SUB", "xA");
  424. gen_code("JGE", gen_label()+9);
  425. is_declare("LOAD", "xD");
  426. is_declare("ADD", "xD");
  427. is_declare("STORE", "xD");
  428. is_declare("LOAD", "xC");
  429. is_declare("ADD", "xC");
  430. is_declare("STORE", "xC");
  431. is_declare("ADD", "xC");
  432. gen_code("JUMP", gen_label()-9); //
  433. is_declare("LOAD", "xC");
  434. is_declare("SUB", "xA");
  435. gen_code("JGE", gen_label()+7);
  436. is_declare("LOAD", "xA");
  437. is_declare("SUB", "xC");
  438. is_declare("STORE", "xA");
  439. is_declare("LOAD", "xB");
  440. is_declare("ADD", "xD");
  441. is_declare("STORE", "xB");
  442. gen_code("ZERO", -1);
  443. gen_code("ADDC", $3);
  444. is_declare("SUB", "xA");
  445. gen_code("JGE", gen_label()+4);
  446. gen_code("JUMP", gen_label()-29);
  447. gen_code("ZERO", -1);
  448. gen_code("JUMP", gen_label()+2);
  449. is_declare("LOAD", "xB");
  450. gen_code("SUBC", $3);
  451. }
  452.  
  453. }
  454.  
  455. | ID '/' ID {
  456. gen_code("ZERO", -1);
  457. is_declare("ADD", $3);
  458. gen_code("JZ", gen_label()+32);
  459. is_declare("STORE", "xB");
  460. is_declare("LOAD", $1);
  461. is_declare("STORE", "xA");
  462. is_declare("LOAD", "#jeden");
  463. is_declare("STORE", "xD");
  464. is_declare("LOAD", $3);
  465. is_declare("STORE", "xC");
  466. is_declare("ADD", "xC");
  467. is_declare("SUB", "xA");
  468. gen_code("JGE", gen_label()+9);
  469. is_declare("LOAD", "xD");
  470. is_declare("ADD", "xD");
  471. is_declare("STORE", "xD");
  472. is_declare("LOAD", "xC");
  473. is_declare("ADD", "xC");
  474. is_declare("STORE", "xC");
  475. is_declare("ADD", "xC");
  476. gen_code("JUMP", gen_label()-9); //
  477. is_declare("LOAD", "xC");
  478. is_declare("SUB", "xA");
  479. gen_code("JGE", gen_label()+7);
  480. is_declare("LOAD", "xA");
  481. is_declare("SUB", "xC");
  482. is_declare("STORE", "xA");
  483. is_declare("LOAD", "xB");
  484. is_declare("ADD", "xD");
  485. is_declare("STORE", "xB");
  486. is_declare("LOAD", $3);
  487. is_declare("SUB", "xA");
  488. gen_code("JGE", gen_label()+4);
  489. gen_code("JUMP", gen_label()-27);
  490. gen_code("ZERO", -1);
  491. gen_code("JUMP", gen_label()+2);
  492. is_declare("LOAD", "xB");
  493. is_declare("SUB", $3);
  494.  
  495. }
  496.  
  497. | NUM '%' NUM {
  498. if($3==0)
  499. {gen_code("ZERO",-1);}
  500. else
  501. {
  502. gen_code("ZERO",-1);
  503. gen_code("ADDC",($1%$3));
  504. }
  505. }
  506. | NUM '%' ID {
  507. if($1==0)
  508. {
  509. gen_code("ZERO",-1);
  510. }
  511. else
  512. {
  513. gen_code("ZERO", -1);
  514. is_declare("ADD", $3);
  515. gen_code("JZ", gen_label()+33);
  516. is_declare("STORE", "xB");
  517. gen_code("ZERO", -1);
  518. gen_code("ADDC", $1);
  519. is_declare("STORE", "xA");
  520. is_declare("LOAD", "#jeden");
  521. is_declare("STORE", "xD");
  522. is_declare("LOAD", $3);
  523. is_declare("STORE", "xC");
  524. is_declare("ADD", "xC");
  525. is_declare("SUB", "xA");
  526. gen_code("JGE", gen_label()+9);
  527. is_declare("LOAD", "xD");
  528. is_declare("ADD", "xD");
  529. is_declare("STORE", "xD");
  530. is_declare("LOAD", "xC");
  531. is_declare("ADD", "xC");
  532. is_declare("STORE", "xC");
  533. is_declare("ADD", "xC");
  534. gen_code("JUMP", gen_label()-9); //
  535. is_declare("LOAD", "xC");
  536. is_declare("SUB", "xA");
  537. gen_code("JGE", gen_label()+7);
  538. is_declare("LOAD", "xA");
  539. is_declare("SUB", "xC");
  540. is_declare("STORE", "xA");
  541. is_declare("LOAD", "xB");
  542. is_declare("ADD", "xD");
  543. is_declare("STORE", "xB");
  544. is_declare("LOAD", $3);
  545. is_declare("SUB", "xA");
  546. gen_code("JGE", gen_label()+4);
  547. gen_code("JUMP", gen_label()-27);
  548. gen_code("ZERO", -1);
  549. gen_code("JUMP", gen_label()+2);
  550. is_declare("LOAD", "xA");
  551. }
  552. }
  553. | ID '%' NUM {
  554. if($3==0)
  555. {gen_code("ZERO",-1);}
  556. else
  557. {
  558. gen_code("ZERO", -1);
  559. is_declare("STORE", "xB");
  560. is_declare("LOAD", $1);
  561. is_declare("STORE", "xA");
  562. is_declare("LOAD", "#jeden");
  563. is_declare("STORE", "xD");
  564. gen_code("ZERO", -1);
  565. gen_code("ADDC", $3);
  566. is_declare("STORE", "xC");
  567. is_declare("ADD", "xC");
  568. is_declare("SUB", "xA");
  569. gen_code("JGE", gen_label()+9);
  570. is_declare("LOAD", "xD");
  571. is_declare("ADD", "xD");
  572. is_declare("STORE", "xD");
  573. is_declare("LOAD", "xC");
  574. is_declare("ADD", "xC");
  575. is_declare("STORE", "xC");
  576. is_declare("ADD", "xC");
  577. gen_code("JUMP", gen_label()-9); //
  578. is_declare("LOAD", "xC");
  579. is_declare("SUB", "xA");
  580. gen_code("JGE", gen_label()+7);
  581. is_declare("LOAD", "xA");
  582. is_declare("SUB", "xC");
  583. is_declare("STORE", "xA");
  584. is_declare("LOAD", "xB");
  585. is_declare("ADD", "xD");
  586. is_declare("STORE", "xB");
  587. gen_code("ZERO", -1);
  588. gen_code("ADDC", $3);
  589. is_declare("SUB", "xA");
  590. gen_code("JGE", gen_label()+4);
  591. gen_code("JUMP", gen_label()-29);
  592. gen_code("ZERO", -1);
  593. gen_code("JUMP", gen_label()+2);
  594. is_declare("LOAD", "xA");
  595. }
  596. }
  597. | ID '%' ID {
  598. gen_code("ZERO", -1);
  599. is_declare("ADD", $3);
  600. gen_code("JZ", gen_label()+32);
  601. is_declare("STORE", "xB");
  602. is_declare("LOAD", $1);
  603. is_declare("STORE", "xA");
  604. is_declare("LOAD", "#jeden");
  605. is_declare("STORE", "xD");
  606. is_declare("LOAD", $3);
  607. is_declare("STORE", "xC");
  608. is_declare("ADD", "xC");
  609. is_declare("SUB", "xA");
  610. gen_code("JGE", gen_label()+9);
  611. is_declare("LOAD", "xD");
  612. is_declare("ADD", "xD");
  613. is_declare("STORE", "xD");
  614. is_declare("LOAD", "xC");
  615. is_declare("ADD", "xC");
  616. is_declare("STORE", "xC");
  617. is_declare("ADD", "xC");
  618. gen_code("JUMP", gen_label()-9); //
  619. is_declare("LOAD", "xC");
  620. is_declare("SUB", "xA");
  621. gen_code("JGE", gen_label()+7);
  622. is_declare("LOAD", "xA");
  623. is_declare("SUB", "xC");
  624. is_declare("STORE", "xA");
  625. is_declare("LOAD", "xB");
  626. is_declare("ADD", "xD");
  627. is_declare("STORE", "xB");
  628. is_declare("LOAD", $3);
  629. is_declare("SUB", "xA");
  630. gen_code("JGE", gen_label()+4);
  631. gen_code("JUMP", gen_label()-27);
  632. gen_code("ZERO", -1);
  633. gen_code("JUMP", gen_label()+2);
  634. is_declare("LOAD", "xA");
  635. }
  636. ;
  637. condition : NUM EQ NUM {
  638. if($1 == $3)
  639. {
  640. gen_code("ZERO", -1);
  641. }
  642. else
  643. {
  644. gen_code("ZERO", -1);
  645. gen_code("ADDC", 1);
  646. }
  647. }
  648. | NUM EQ ID {
  649. gen_code("ZERO", -1);
  650. gen_code("ADDC", $1);
  651. is_declare("STORE", "xC");
  652. is_declare("SUB", $3);
  653. gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
  654. is_declare("LOAD", $3);
  655. is_declare("STORE", "xA");
  656. is_declare("LOAD", "xC");
  657. is_declare("STORE", "xB");
  658. gen_code("JUMP", gen_label()+5);
  659. is_declare("LOAD", "xC");
  660. is_declare("STORE", "xA");
  661. is_declare("LOAD", $3);
  662. is_declare("STORE", "xB");
  663. is_declare("LOAD", "xA");
  664. is_declare("SUB", "xB");
  665. gen_code("JGE", gen_label()+3);
  666. gen_code("ZERO", -1);
  667. gen_code("JUMP", gen_label()+2);
  668. is_declare("LOAD", "#jeden");
  669. }
  670. | ID EQ NUM {
  671. gen_code("ZERO", -1);
  672. gen_code("ADDC", $3);
  673. is_declare("STORE", "xC");
  674. is_declare("SUB", $1);
  675. gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
  676. is_declare("LOAD", $1);
  677. is_declare("STORE", "xA");
  678. is_declare("LOAD", "xC");
  679. is_declare("STORE", "xB");
  680. gen_code("JUMP", gen_label()+5);
  681. is_declare("LOAD", "xC");
  682. is_declare("STORE", "xA");
  683. is_declare("LOAD", $1);
  684. is_declare("STORE", "xB");
  685. is_declare("LOAD", "xA");
  686. is_declare("SUB", "xB");
  687. gen_code("JGE", gen_label()+3);
  688. gen_code("ZERO", -1);
  689. gen_code("JUMP", gen_label()+2);
  690. is_declare("LOAD", "#jeden");
  691. }
  692. | ID EQ ID {
  693. is_declare("LOAD", $1);
  694. is_declare("SUB", $3);
  695. gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
  696. is_declare("LOAD", $3);
  697. is_declare("STORE", "xA");
  698. is_declare("LOAD", $1);
  699. is_declare("STORE", "xB");
  700. gen_code("JUMP", gen_label()+5);
  701. is_declare("LOAD", $1);
  702. is_declare("STORE", "xA");
  703. is_declare("LOAD", $3);
  704. is_declare("STORE", "xB");
  705. is_declare("LOAD", "xA");
  706. is_declare("SUB", "xB");
  707. gen_code("JGE", gen_label()+3);
  708. gen_code("ZERO", -1);
  709. gen_code("JUMP", gen_label()+2);
  710. is_declare("LOAD", "#jeden");
  711. /* Tutaj if */
  712.  
  713. }
  714.  
  715. | NUM NEQ NUM {
  716. if($1 == $3)
  717. {
  718. is_declare("LOAD", "#jeden");
  719. }
  720. else
  721. {
  722. gen_code("ZERO", -1);
  723. }
  724. }
  725. | NUM NEQ ID {
  726. gen_code("ZERO", -1);
  727. gen_code("ADDC", $1);
  728. is_declare("STORE", "xC");
  729. is_declare("SUB", $3);
  730. gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
  731. is_declare("LOAD", $3);
  732. is_declare("STORE", "xA");
  733. is_declare("LOAD", "xC");
  734. is_declare("STORE", "xB");
  735. gen_code("JUMP", gen_label()+5);
  736. is_declare("LOAD", "xC");
  737. is_declare("STORE", "xA");
  738. is_declare("LOAD", $3);
  739. is_declare("STORE", "xB");
  740. is_declare("LOAD", "xA");
  741. is_declare("SUB", "xB");
  742. gen_code("JGE", gen_label()+3);
  743. is_declare("LOAD", "#jeden");
  744. gen_code("JUMP", gen_label()+2);
  745. gen_code("ZERO", -1);
  746.  
  747. }
  748. | ID NEQ NUM {
  749. gen_code("ZERO", -1);
  750. gen_code("ADDC", $3);
  751. is_declare("STORE", "xC");
  752. is_declare("SUB", $1);
  753. gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
  754. is_declare("LOAD", $1);
  755. is_declare("STORE", "xA");
  756. is_declare("LOAD", "xC");
  757. is_declare("STORE", "xB");
  758. gen_code("JUMP", gen_label()+5);
  759. is_declare("LOAD", "xC");
  760. is_declare("STORE", "xA");
  761. is_declare("LOAD", $1);
  762. is_declare("STORE", "xB");
  763. is_declare("LOAD", "xA");
  764. is_declare("SUB", "xB");
  765. gen_code("JGE", gen_label()+3);
  766. is_declare("LOAD", "#jeden");
  767. gen_code("JUMP", gen_label()+2);
  768. gen_code("ZERO", -1);
  769. }
  770. | ID NEQ ID {
  771. is_declare("LOAD", $1);
  772. is_declare("SUB", $3);
  773. gen_code("JGE", gen_label()+6); /* Jak wieksze to niech bedzie z lewej strony */
  774. is_declare("LOAD", $3);
  775. is_declare("STORE", "xA");
  776. is_declare("LOAD", $1);
  777. is_declare("STORE", "xB");
  778. gen_code("JUMP", gen_label()+5);
  779. is_declare("LOAD", $1);
  780. is_declare("STORE", "xA");
  781. is_declare("LOAD", $3);
  782. is_declare("STORE", "xB");
  783. is_declare("LOAD", "xA");
  784. is_declare("SUB", "xB");
  785. gen_code("JGE", gen_label()+3);
  786. is_declare("LOAD", "#jeden");
  787. gen_code("JUMP", gen_label()+2);
  788. gen_code("ZERO", -1);
  789. /* Tutaj if */
  790. }
  791.  
  792. | NUM LT NUM {
  793. if($1 < $3)
  794. gen_code("ZERO", -1);
  795. else
  796. is_declare("LOAD", "#jeden");
  797. }
  798. | NUM LT ID {;
  799. is_declare("LOAD", $3);
  800. gen_code("SUBC", $1);
  801. gen_code("JGE", gen_label()+3); /* Większe */
  802. is_declare("LOAD", "#jeden");
  803. gen_code("JUMP", gen_label()+2);
  804. gen_code("ZERO", -1);
  805.  
  806. /* if */
  807.  
  808. }
  809. | ID LT NUM {
  810. gen_code("ZERO", -1);
  811. gen_code("ADDC", $3);
  812. is_declare("SUB", $1);
  813. gen_code("JGE", gen_label()+3); /* Większe */
  814. is_declare("LOAD", "#jeden");
  815. gen_code("JUMP", gen_label()+2);
  816. gen_code("ZERO", -1);
  817. }
  818. | ID LT ID {
  819. is_declare("LOAD", $3);
  820. is_declare("SUB", $1);
  821. gen_code("JGE", gen_label()+3); /* Większe */
  822. is_declare("LOAD", "#jeden");
  823. gen_code("JUMP", gen_label()+2);
  824. gen_code("ZERO", -1);
  825. }
  826.  
  827. | NUM GT NUM {
  828. if($1 > $3)
  829. gen_code("ZERO", -1);
  830. else
  831. is_declare("LOAD", "#jeden");
  832. }
  833. | NUM GT ID {
  834. gen_code("ZERO", -1);
  835. gen_code("ADDC", $1);
  836. is_declare("SUB", $3);
  837. gen_code("JGE", gen_label()+3); /* Większe */
  838. is_declare("LOAD", "#jeden");
  839. gen_code("JUMP", gen_label()+2);
  840. gen_code("ZERO", -1);
  841. }
  842. | ID GT NUM {
  843. is_declare("LOAD", $1);
  844. gen_code("SUBC", $3);
  845. gen_code("JGE", gen_label()+3); /* Większe */
  846. is_declare("LOAD", "#jeden");
  847. gen_code("JUMP", gen_label()+2);
  848. gen_code("ZERO", -1);
  849. }
  850. | ID GT ID {
  851. is_declare("LOAD", $1);
  852. is_declare("SUB", $3);
  853. gen_code("JGE", gen_label()+3); /* Większe */
  854. is_declare("LOAD", "#jeden");
  855. gen_code("JUMP", gen_label()+2);
  856. gen_code("ZERO", -1);
  857. }
  858.  
  859. | NUM LTEQ NUM {
  860. if($1 <= $3)
  861. gen_code("ZERO", -1);
  862. else
  863. is_declare("LOAD", "#jeden");
  864. }
  865. | NUM LTEQ ID {
  866. is_declare("LOAD", $3);
  867. gen_code("SUBC",$1);
  868. gen_code("JGE",gen_label()+7);
  869. gen_code("ZERO",-1);
  870. gen_code("ADDC",$1);
  871. is_declare("SUB", $3);
  872. gen_code("JZ",gen_label()+3);
  873. is_declare("LOAD", "#jeden");
  874. gen_code("JUMP",gen_label()+2);
  875. gen_code("ZERO",-1);
  876.  
  877. }
  878. | ID LTEQ NUM {
  879. gen_code("ZERO",-1);
  880. gen_code("ADDC",$3);
  881. is_declare("SUB",$1);
  882. gen_code("JGE",gen_label()+6);
  883. is_declare("LOAD", $1);
  884. gen_code("SUBC", $3);
  885. gen_code("JZ",gen_label()+3);
  886. is_declare("LOAD", "#jeden");
  887. gen_code("JUMP",gen_label()+2);
  888. gen_code("ZERO",-1);
  889. }
  890. | ID LTEQ ID {
  891. is_declare("LOAD", $3);
  892. is_declare("SUB",$1);
  893. gen_code("JGE",gen_label()+6);
  894. is_declare("LOAD", $1);
  895. is_declare("SUB", $3);
  896. gen_code("JZ",gen_label()+3);
  897. is_declare("LOAD", "#jeden");
  898. gen_code("JUMP",gen_label()+2);
  899. gen_code("ZERO",-1);
  900. }
  901.  
  902. | NUM GTEQ NUM {
  903. if($1 >= $3)
  904. gen_code("ZERO", -1);
  905. else
  906. is_declare("LOAD", "#jeden");
  907. }
  908. | NUM GTEQ ID {
  909. gen_code("ZERO",-1);
  910. gen_code("ADDC",$1);
  911. is_declare("SUB",$3);
  912. gen_code("JGE",gen_label()+6);
  913. is_declare("LOAD", $3);
  914. gen_code("SUBC", $1);
  915. gen_code("JZ",gen_label()+3);
  916. is_declare("LOAD", "#jeden");
  917. gen_code("JUMP",gen_label()+2);
  918. gen_code("ZERO",-1);
  919. }
  920. | ID GTEQ NUM {
  921. is_declare("LOAD", $1);
  922. gen_code("SUBC",$3);
  923. gen_code("JGE",gen_label()+7);
  924. gen_code("ZERO",-1);
  925. gen_code("ADDC",$3);
  926. is_declare("SUB", $1);
  927. gen_code("JZ",gen_label()+3);
  928. is_declare("LOAD", "#jeden");
  929. gen_code("JUMP",gen_label()+2);
  930. gen_code("ZERO",-1);
  931. }
  932. | ID GTEQ ID {
  933.  
  934. is_declare("LOAD", $1);
  935. is_declare("SUB",$3);
  936. gen_code("JGE",gen_label()+6);
  937. is_declare("LOAD", $3);
  938. is_declare("SUB", $1);
  939. gen_code("JZ",gen_label()+3);
  940. is_declare("LOAD", "#jeden");
  941. gen_code("JUMP",gen_label()+2);
  942. gen_code("ZERO",-1);
  943. }
  944. ;
  945.  
  946. %%
  947.  
  948. /* Odpalanie parsera */
  949.  
  950.  
  951. int main(int argc, char *argv[])
  952. {
  953. extern FILE *yyin;
  954. yyin = fopen(argv[1], "r");
  955. errors = 0;
  956. dec("#print0"); /* Wyświetlanie */
  957. dec("#jeden");
  958. dec("xA");
  959. dec("xB");
  960. dec("xC");
  961. dec("xD");
  962. gen_code("ADDC", 1);
  963. is_declare("STORE", "#jeden");
  964. extern int yydebug;
  965. yydebug = 0;
  966. yyparse();
  967.  
  968. if(errors == 0)
  969. {
  970. printCom();
  971. }
  972.  
  973. return 0;
  974. }
  975.  
  976.  
  977. yyerror(char *s)
  978. {
  979. errors++;
  980. printf("%s\n", s);
  981. }
Add Comment
Please, Sign In to add comment