Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 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. FILE * F2;
  22. void yyerror(char *s)
  23. {
  24.     fprintf(stdout,"At Line %d, ERROR-> %s\n",line_count,s);
  25.     return;
  26. }
  27. %}
  28.  
  29.  
  30. %token  INCLUDE WS NUMBER VARIABLE INT MAIN LPAREN RPAREN LCURL RCURL SPECIFIER SEMICOLON ASSIGNOP D_QUOTE PRINTF COMMA
  31.  
  32.  
  33. %%
  34. Program: INCLUDE Line   {
  35.                         strcpy(str,"");
  36.  
  37.                         char t1[100],t2[100];
  38.                         strcpy(t2,$2->ch);
  39.                         strcpy(t1,"DATA SEGMENT ");
  40.                         strcat(t1,t2);
  41.                        
  42.                         code=fopen("output1.asm","w");
  43.                         fprintf(code,"%s",t1);
  44.                         };
  45.  
  46. Line: INT MAIN LPAREN RPAREN LCURL stmts RCURL  
  47.                         {$$ = new simple();
  48.                         char t1[100];
  49.                         strcpy(t1,"\nCODE SEGMENT ");
  50.                        
  51.                         strcat(t1,$6->ch);
  52.                         $$->ch = t1;
  53.  
  54.                         };
  55.                        
  56. stmts: | stmts stmt {
  57.                         $$ = new simple();
  58.                         strcpy(t,$2->ch);
  59.                         strcat(str,t);
  60.                        
  61.                         $$->ch=str;
  62.                         //printf("%s",str);
  63.                      }
  64.        
  65.        
  66.         ;
  67.                        
  68. stmt: INT VARIABLE SEMICOLON
  69.                         {
  70.                          $$ = new simple();
  71.                          int len=(int)$2->d;
  72.                          char t1[100],t2[100];
  73.                          strcpy(t2,$2->ch);
  74.                          t2[len]='\0';
  75.                          strcpy(t1,"\n");
  76.                          strcat(t1,t2);
  77.                        
  78.                          strcat(t1," db ? ");
  79.                          $$->ch = t1;
  80.                        
  81.                         };
  82.     | VARIABLE ASSIGNOP NUMBER SEMICOLON
  83.                         { $$ = new simple();
  84.                          char t2[100], t1[100], t3[100],t4[100];
  85.                          
  86.                          int len = (int) $1->d;
  87.                          strcpy(t1, "\nMOV ");
  88.                          strcpy(t4, $1->ch);
  89.                          t4[len]='\0';
  90.                          strcat(t1,t4);
  91.                          strcat(t1," , ");
  92.                                        
  93.                          len = (int) $3->d;
  94.                          strcpy(t3, $3->ch);
  95.                          t3[len]='\0';
  96.                          strcat(t1,t3);
  97.                          
  98.                          $$->ch=t1;
  99.                        
  100.                         };  
  101.     | PRINTF LPAREN D_QUOTE SPECIFIER D_QUOTE COMMA VARIABLE RPAREN SEMICOLON
  102.                     {
  103.                         $$ = new simple();
  104.                          char t2[100], t1[100], t3[100],t4[100];
  105.                          
  106.                          int len = (int) $7->d;
  107.                          strcpy(t1, "\nMOV AX , ");
  108.                          strcpy(t4, $7->ch);
  109.                          t4[len]='\0';
  110.                          strcat(t1,t4);
  111.                          strcat(t1,"\nOUT AX");
  112.                          
  113.                          $$->ch=t1;
  114.                     };
  115.  
  116.    
  117.  
  118. %%
  119. void printBinary(char* arrr)
  120. {
  121.     int num = 0;
  122.     for (int i = 0; i < strlen(arrr); i++) {
  123.         num = num * 10 + arrr[i] - '0';
  124.     }
  125.     char temp[20];
  126.     int len = 0;
  127.     while (num) {
  128.         temp[len++] = num % 2 + 48;
  129.         num = num / 2;
  130.     }
  131.     for (int i = 0; i < len / 2; i++) {
  132.         char ccc = temp[i];
  133.         temp[i] = temp[len-1-i];
  134.         temp[len-1-i] = ccc;
  135.     }
  136.     temp[len] = '\0';
  137.     printf("%s ", temp);
  138.     fprintf(F2,"%s",temp);
  139. }
  140.  
  141.  
  142. void make_word(char *word){
  143.  
  144.     char word1[11][10]={",","DATA","SEGMENT","CODE","MOV","OUT","db","?","AX","a","b"};
  145.     char word2[11][10]={",","0000","0010","0001","101100","101110","1000","0100","0011","1100","0111"};
  146.  
  147.     char x[10];
  148.     int a=0;
  149.     int ind1=0,ind2=0;
  150.     for(int i=0;word[i]!='\0';i++){
  151.         if (word[i] != ' ')
  152.             x[a++]=word[i];
  153.         if(word[i]==' ' || word[i+1]=='\0'){
  154.             x[a]='\0';
  155.             a=0;
  156. //          printf("%s",x);
  157.             int flag = 0;
  158.             for(int j=0;j<11;j++){
  159.                 if(!strcmp(word1[j],x)){
  160.                     printf("%s ",word2[j]);
  161.                     fprintf(F2,"%s",word2[j]);
  162.                     flag = 1;
  163.                     break;
  164.                 }
  165.             }
  166.             if (!flag) {
  167.                 printBinary(x);
  168.             }
  169.         }
  170.     }
  171. }
  172.  
  173. int main(void){
  174.     fp1=fopen("input_code.txt","r");    
  175.     yyin=fp1;yyout=fp2;
  176.     printf("START\n");
  177.     yyparse();
  178.     printf("END\n");
  179.     fclose(fp1);fclose(fp2);
  180.     fclose(fp3);fclose(code);
  181.     //printf("OK\n");
  182.    
  183.    
  184.     FILE *F1 = fopen("output1.asm", "r");
  185.     F2 = fopen("output2.txt", "w");
  186.    
  187.     char chh[100];
  188.     while(EOF!=fscanf(F1, "%[^\n]\n", chh)){
  189.  
  190.       make_word(chh);
  191.       fprintf(F2,"\n");
  192.       //printf("%s\n", chh);
  193.       //fprintf(F2,"%s\n",chh);
  194.     }
  195.    
  196.     fclose(F1);fclose(F2);
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement