Advertisement
kknndd_

Untitled

May 14th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include "lexer.h"
  6. #include "generator.h"
  7.  
  8. #define MAX_INPUT_SIZE 160
  9.  
  10. /*** Syntakticky analyzator a interpretator ***/
  11.  
  12. /* Overenie symbolu na vstupe a precitanie dalsieho.
  13. * Vracia atribut overeneho symbolu. */
  14. int match(const Symbol expected)
  15. {
  16. if (lex_symbol == expected) {
  17. int attr = lex_attr;
  18. next_symbol();
  19. return attr;
  20. } else {
  21. fprintf(stderr,
  22. "CHYBA: Chyba symbol %s, namiesto toho sa vyskytol %s.\n",
  23. symbol_name(expected), symbol_name(lex_symbol));
  24. exit(1);
  25. }
  26. }
  27.  
  28. /* Tabulka premennych:
  29. * index -- index identifikatora, hodnota -- hodnota premennej */
  30. int variables[LEX_IDS_MAX];
  31.  
  32. void read_variable(int id_idx)
  33. {
  34. int value;
  35. printf("%s = ", lex_ids[id_idx]);
  36. scanf("%d", &value);
  37. variables[id_idx] = value;
  38. }
  39.  
  40. /* Gramatika:
  41. *
  42. * Expr -> Term {("+"|"-") Term}
  43. * Term -> VALUE | "(" Expr ")"
  44. */
  45.  
  46. int expr(), term(), print(), read(), mul(), power(), equal(), let(), assign(), condition(), program();
  47.  
  48. int print(){
  49.  
  50. if(lex_symbol == PRINT){
  51. // sicko ok
  52. } else {
  53. fprintf(stderr,"Chyba prikaz PRINT ! objavil sa : %s", symbol_name(lex_symbol));
  54. }
  55.  
  56. next_symbol();
  57. return equal();
  58.  
  59. }
  60.  
  61. int condition(){
  62.  
  63. // let a; let b; if a < b ? print 1 : print 0;
  64.  
  65. int res = -1;
  66.  
  67. if(lex_symbol == IF){
  68. next_symbol();
  69.  
  70. if(equal()) {
  71.  
  72. while( lex_symbol != ELSE ){
  73.  
  74. switch (lex_symbol) {
  75. case PRINT: res = print(); break;
  76. case ASSIGN: assign(); break;
  77. }
  78. if(lex_symbol == ELSE){
  79. break;
  80. }
  81. next_symbol();
  82. }
  83.  
  84. while(lex_symbol != SEMICOLON && lex_symbol != SEOF){
  85. next_symbol();
  86. }
  87.  
  88. next_symbol();
  89.  
  90. } else {
  91. while (lex_symbol != ELSE){
  92. next_symbol();
  93. }
  94.  
  95. while( lex_symbol != SEMICOLON ){
  96.  
  97. switch (lex_symbol) {
  98. case PRINT: res = print(); break;
  99. case ASSIGN: assign(); break;
  100. }
  101. if(lex_symbol == ELSE){
  102. break;
  103. }
  104. next_symbol();
  105. }
  106.  
  107. next_symbol();
  108.  
  109. }
  110.  
  111. }
  112.  
  113. return res;
  114. }
  115.  
  116. int let(){
  117.  
  118. int pocet_premennych = 0;
  119.  
  120. if( lex_symbol == LET ){
  121.  
  122. while( lex_symbol != SEMICOLON) {
  123.  
  124. next_symbol();
  125.  
  126. if(lex_symbol == SEMICOLON){
  127. break;
  128. }
  129.  
  130. if(lex_symbol == COMMA){
  131. next_symbol();
  132. }
  133.  
  134. if(lex_symbol == SEMICOLON){
  135. break;
  136. }
  137.  
  138. if(lex_symbol == ID){
  139. read_variable(lex_attr);
  140. }
  141.  
  142. pocet_premennych++;
  143.  
  144. next_symbol();
  145.  
  146. }
  147.  
  148. }
  149.  
  150. return pocet_premennych;
  151.  
  152. }
  153.  
  154. int read(){
  155.  
  156. int pocet_premennych = 0;
  157.  
  158. if(lex_symbol == READ){
  159.  
  160. do {
  161.  
  162. next_symbol();
  163.  
  164. if(lex_symbol != ID){
  165. fprintf(stderr,"Chyba ID !");
  166. } else {
  167. read_variable(lex_attr);
  168. }
  169.  
  170. next_symbol();
  171. pocet_premennych++;
  172.  
  173. } while (lex_symbol == COMMA);
  174.  
  175. } else {
  176. fprintf(stderr,"Chyba prikaz READ !");
  177. }
  178.  
  179. return pocet_premennych;
  180.  
  181. }
  182.  
  183. int assign(){
  184.  
  185. int variables_count = 0;
  186. // let a; a:=10; print a; -> 10
  187. // let a,b; print a*b+5; let a,b; print (a*b)+5;
  188. // let a,b; a:=b*b; print a;
  189. // let a,b; print a*b;
  190.  
  191. do {
  192.  
  193. if(lex_symbol == COMMA){
  194. next_symbol();
  195. }
  196.  
  197. int position = lex_attr;
  198.  
  199. next_symbol();
  200.  
  201. if(lex_symbol == SEMICOLON || lex_symbol == SEOF || lex_symbol != ASSIGN){
  202. next_symbol();
  203. break;
  204. }
  205.  
  206. next_symbol();
  207.  
  208. // if assign
  209. variables[position] = equal();
  210. variables_count++;
  211.  
  212. } while(lex_symbol != SEOF && lex_symbol != SEMICOLON);
  213.  
  214. next_symbol();
  215.  
  216. return variables_count;
  217.  
  218. }
  219.  
  220. int program (){
  221. int j = 0;
  222. int k;
  223. int res = 0;
  224.  
  225. while(lex_symbol != SEOF){
  226. switch (lex_symbol) {
  227. case LET:
  228. k = j;
  229. j += let();
  230. for (; k < j; k++) {
  231. write_ask_var(k, lex_ids[k]);
  232. }
  233. break;
  234. case READ:
  235. k = j;
  236. j += read();
  237. for (; k < j; k++) {
  238. write_ask_var(k, lex_ids[k]);
  239. }
  240. break;
  241. case ID: assign(); break;
  242. case PRINT: res = print(); break;
  243. case SEMICOLON: next_symbol(); break;
  244. case IF: condition(); break;
  245. case SEOF: break;
  246. }
  247.  
  248. }
  249.  
  250. return res;
  251. }
  252.  
  253. /* Term -> VALUE | "(" Expr ")" */
  254. int term()
  255. {
  256. int value;
  257.  
  258. switch (lex_symbol) {
  259. case VALUE:
  260. value = lex_attr;
  261. write_number(value);
  262. next_symbol();
  263. break;
  264. case LPAR:
  265. next_symbol();
  266. value = expr();
  267. match(RPAR);
  268. break;
  269. case ID:
  270. write_var(lex_attr);
  271. value = variables[lex_attr];
  272. next_symbol();
  273. break;
  274. case SEOF: break;
  275. // default:
  276. // default:
  277. //fprintf(stderr, "CHYBA: Chyba operand, namiesto toho sa vyskytol %s\n.",
  278. // symbol_name(lex_symbol));
  279. // exit(1);
  280. }
  281.  
  282. return value;
  283. }
  284.  
  285. /* Expr -> Term {("+"|"-") Term} */
  286. int expr()
  287. {
  288. int leftOp, rightOp;
  289.  
  290. Symbol operator;
  291. leftOp = mul();
  292.  
  293. while (lex_symbol == PLUS || lex_symbol == MINUS) {
  294.  
  295. operator = lex_symbol;
  296.  
  297. next_symbol();
  298.  
  299. rightOp = mul();
  300.  
  301. switch (operator) {
  302. case PLUS:
  303. write_add();
  304. leftOp = leftOp + rightOp;
  305. break;
  306. case MINUS:
  307. write_sub();
  308. leftOp = leftOp - rightOp;
  309. break;
  310. case SEOF:
  311.  
  312. break;
  313. default:
  314. assert("Neocakavany operator v expr()");
  315. }
  316. }
  317.  
  318.  
  319. return leftOp;
  320. }
  321.  
  322. int power(){
  323. int leftOp, rightOp;
  324.  
  325. leftOp = term();
  326. Symbol operator = lex_symbol;
  327.  
  328. while(operator == POWER){
  329.  
  330. operator = lex_symbol;
  331.  
  332. next_symbol();
  333. rightOp = power();
  334.  
  335. if(operator == POWER){
  336. write_power(leftOp, rightOp);
  337. leftOp = (int)pow(leftOp, rightOp);
  338. }
  339.  
  340. }
  341.  
  342. return leftOp;
  343.  
  344. }
  345.  
  346. // let a; let b; if a < b ? print 1 : print 0;
  347. //
  348. // var a,b; read a,b; if (a>b) then a:=9; else a:=0; print a;
  349. // let a,b; if a>b ? print 9 : print 0;
  350. //
  351.  
  352. int mul(){
  353.  
  354. int leftOp, rightOp;
  355. Symbol operator;
  356.  
  357. leftOp = power();
  358.  
  359. operator = lex_symbol;
  360.  
  361. while( operator == MUL || operator == DIV ) {
  362.  
  363. operator = lex_symbol;
  364.  
  365. if( operator == SEOF || operator == SEMICOLON ){
  366. break;
  367. }
  368.  
  369. next_symbol();
  370.  
  371. rightOp = power();
  372.  
  373. switch ( operator ) {
  374. case MUL:
  375. write_mul();
  376. leftOp = leftOp * rightOp;
  377. break;
  378. case DIV:
  379. write_div();
  380. leftOp = leftOp / rightOp;
  381. break;
  382. default:
  383. break;
  384. //fprintf(stderr, "CHYBA: Neocakavany ('%s') operator pri deleni a nasobeni", symbol_name(operator));
  385. }
  386.  
  387. }
  388.  
  389. return leftOp;
  390.  
  391. }
  392.  
  393. int equal(){
  394.  
  395. int leftOp = expr(), rightOp;
  396. Symbol operator = lex_symbol;
  397.  
  398. while(
  399. operator == EQUAL ||
  400. operator == GREATER_THAN ||
  401. operator == GREATER_THAN_EQUAL ||
  402. operator == LESS_THAN_EQUAL ||
  403. operator == LESS_THAN
  404. ){
  405.  
  406. operator = lex_symbol;
  407. next_symbol();
  408.  
  409. rightOp = expr();
  410.  
  411. switch ( operator ) {
  412. case EQUAL: leftOp = leftOp == rightOp; break;
  413. case GREATER_THAN_EQUAL: leftOp = leftOp >= rightOp; break;
  414. case GREATER_THAN: leftOp = leftOp > rightOp; break;
  415. case LESS_THAN_EQUAL: leftOp = leftOp <= rightOp; break;
  416. case LESS_THAN: leftOp = leftOp < rightOp; break;
  417. case SEOF: break;
  418. //default: fprintf(stderr ,"CHYBA: ockava sa operator porovnania nie : ('%s') ", symbol_name(lex_symbol)); break;
  419. }
  420.  
  421. }
  422.  
  423. return leftOp;
  424.  
  425. }
  426.  
  427. int main(int argc, char** argv)
  428. {
  429.  
  430. // otvorenie suboru pre Computron VM
  431. FILE *output_file = fopen("program.bin", "wb");
  432. init_generator(output_file);
  433.  
  434. printf("Vstupny retazec: ");
  435. // Citanie vstupneho retazca
  436. char source[MAX_INPUT_SIZE];
  437. fgets(source, MAX_INPUT_SIZE, stdin);
  438.  
  439. init_lexer(source);
  440. print_tokens();
  441.  
  442. printf("\nZaciatok syntaxou riadenej interpretacie\n\n");
  443. init_lexer(source);
  444. next_symbol();
  445.  
  446. // volanie programu
  447. int result = program();
  448. printf("Vysledok: %d\n", result);
  449.  
  450. write_result();
  451. write_end();
  452. // ukoncenie prace s binarnym suborom pre Computron VM
  453. generate_output();
  454. fclose(output_file);
  455. printf("Program vygenerovany v program.bin\n");
  456.  
  457. getchar();
  458. return 0;
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement