Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. %{
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "header.h"
  6. #include<fstream>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. #define YYSTYPE simple* /* yyparse() stack type */
  12. int yyparse(void);
  13. int yylex(void);
  14.  
  15. extern int line_count;
  16. extern FILE *yyin, *yyout;
  17. char* temp;
  18. extern char t[100];
  19. char str[100];
  20. FILE * fp1,*fp2,*fp3,*code;
  21. void yyerror(char *s)
  22. {
  23. fprintf(stdout,"At Line %d, ERROR-> %s\n",line_count,s);
  24. return;
  25. }
  26. %}
  27.  
  28.  
  29. %token INCLUDE WS NUMBER VARIABLE INT MAIN LPAREN RPAREN LCURL RCURL SPECIFIER SEMICOLON ASSIGNOP D_QUOTE PRINTF COMMA
  30.  
  31.  
  32. %%
  33. Program: INCLUDE Line {
  34. strcpy(str,"");
  35.  
  36. char t1[100],t2[100];
  37. strcpy(t2,$2->ch);
  38. strcpy(t1,"DATA SEGMENT ");
  39. strcat(t1,t2);
  40.  
  41. code=fopen("output1.asm","w");
  42. fprintf(code,"%s",t1);
  43. };
  44.  
  45. Line: INT MAIN LPAREN RPAREN LCURL stmts RCURL
  46. {$$ = new simple();
  47. char t1[100];
  48. strcpy(t1,"\nCODE SEGMENT ");
  49.  
  50. strcat(t1,$6->ch);
  51. $$->ch = t1;
  52.  
  53. };
  54.  
  55. stmts: | stmts stmt {
  56. $$ = new simple();
  57. strcpy(t,$2->ch);
  58. strcat(str,t);
  59.  
  60. $$->ch=str;
  61. //printf("%s",str);
  62. }
  63.  
  64.  
  65. ;
  66.  
  67. stmt: INT VARIABLE SEMICOLON
  68. {
  69. $$ = new simple();
  70. int len=(int)$2->d;
  71. char t1[100],t2[100];
  72. strcpy(t2,$2->ch);
  73. t2[len]='\0';
  74. strcpy(t1,"\n");
  75. strcat(t1,t2);
  76.  
  77. strcat(t1," db ? ");
  78. $$->ch = t1;
  79.  
  80. };
  81. | VARIABLE ASSIGNOP NUMBER SEMICOLON
  82. { $$ = new simple();
  83. char t2[100], t1[100], t3[100],t4[100];
  84.  
  85. int len = (int) $1->d;
  86. strcpy(t1, "\nMOV ");
  87. strcpy(t4, $1->ch);
  88. t4[len]='\0';
  89. strcat(t1,t4);
  90. strcat(t1," , ");
  91.  
  92. len = (int) $3->d;
  93. strcpy(t3, $3->ch);
  94. t3[len]='\0';
  95. strcat(t1,t3);
  96.  
  97. $$->ch=t1;
  98.  
  99. };
  100. | PRINTF LPAREN D_QUOTE SPECIFIER D_QUOTE COMMA VARIABLE RPAREN SEMICOLON
  101. {
  102. $$ = new simple();
  103. char t2[100], t1[100], t3[100],t4[100];
  104.  
  105. int len = (int) $7->d;
  106. strcpy(t1, "\nMOV AX , ");
  107. strcpy(t4, $7->ch);
  108. t4[len]='\0';
  109. strcat(t1,t4);
  110. strcat(t1,"\nOUT AX");
  111.  
  112. $$->ch=t1;
  113. };
  114.  
  115.  
  116.  
  117. %%
  118.  
  119. int main(void){
  120. fp1=fopen("input_code.txt","r");
  121. yyin=fp1;yyout=fp2;
  122. printf("START\n");
  123. yyparse();
  124. printf("END\n");
  125. fclose(fp1);fclose(fp2);
  126. fclose(fp3);fclose(code);
  127.  
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement