Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "header.h"
- #include<fstream>
- #include <iostream>
- using namespace std;
- #define YYSTYPE simple* /* yyparse() stack type */
- int yyparse(void);
- int yylex(void);
- extern int line_count;
- extern FILE *yyin, *yyout;
- char* temp;
- extern char t[100];
- char str[100];
- FILE * fp1,*fp2,*fp3,*code;
- FILE * F2;
- void yyerror(char *s)
- {
- fprintf(stdout,"At Line %d, ERROR-> %s\n",line_count,s);
- return;
- }
- %}
- %token INCLUDE WS NUMBER VARIABLE INT MAIN LPAREN RPAREN LCURL RCURL SPECIFIER SEMICOLON ASSIGNOP D_QUOTE PRINTF COMMA
- %%
- Program: INCLUDE Line {
- strcpy(str,"");
- char t1[100],t2[100];
- strcpy(t2,$2->ch);
- strcpy(t1,"DATA SEGMENT ");
- strcat(t1,t2);
- code=fopen("output1.asm","w");
- fprintf(code,"%s",t1);
- };
- Line: INT MAIN LPAREN RPAREN LCURL stmts RCURL
- {$$ = new simple();
- char t1[100];
- strcpy(t1,"\nCODE SEGMENT ");
- strcat(t1,$6->ch);
- $$->ch = t1;
- };
- stmts: | stmts stmt {
- $$ = new simple();
- strcpy(t,$2->ch);
- strcat(str,t);
- $$->ch=str;
- //printf("%s",str);
- }
- ;
- stmt: INT VARIABLE SEMICOLON
- {
- $$ = new simple();
- int len=(int)$2->d;
- char t1[100],t2[100];
- strcpy(t2,$2->ch);
- t2[len]='\0';
- strcpy(t1,"\n");
- strcat(t1,t2);
- strcat(t1," db ? ");
- $$->ch = t1;
- };
- | VARIABLE ASSIGNOP NUMBER SEMICOLON
- { $$ = new simple();
- char t2[100], t1[100], t3[100],t4[100];
- int len = (int) $1->d;
- strcpy(t1, "\nMOV ");
- strcpy(t4, $1->ch);
- t4[len]='\0';
- strcat(t1,t4);
- strcat(t1," , ");
- len = (int) $3->d;
- strcpy(t3, $3->ch);
- t3[len]='\0';
- strcat(t1,t3);
- $$->ch=t1;
- };
- | PRINTF LPAREN D_QUOTE SPECIFIER D_QUOTE COMMA VARIABLE RPAREN SEMICOLON
- {
- $$ = new simple();
- char t2[100], t1[100], t3[100],t4[100];
- int len = (int) $7->d;
- strcpy(t1, "\nMOV AX , ");
- strcpy(t4, $7->ch);
- t4[len]='\0';
- strcat(t1,t4);
- strcat(t1,"\nOUT AX");
- $$->ch=t1;
- };
- %%
- void printBinary(char* arrr)
- {
- int num = 0;
- for (int i = 0; i < strlen(arrr); i++) {
- num = num * 10 + arrr[i] - '0';
- }
- char temp[20];
- int len = 0;
- while (num) {
- temp[len++] = num % 2 + 48;
- num = num / 2;
- }
- for (int i = 0; i < len / 2; i++) {
- char ccc = temp[i];
- temp[i] = temp[len-1-i];
- temp[len-1-i] = ccc;
- }
- temp[len] = '\0';
- printf("%s ", temp);
- fprintf(F2,"%s",temp);
- }
- void make_word(char *word){
- char word1[11][10]={",","DATA","SEGMENT","CODE","MOV","OUT","db","?","AX","a","b"};
- char word2[11][10]={",","0000","0010","0001","101100","101110","1000","0100","0011","1100","0111"};
- char x[10];
- int a=0;
- int ind1=0,ind2=0;
- for(int i=0;word[i]!='\0';i++){
- if (word[i] != ' ')
- x[a++]=word[i];
- if(word[i]==' ' || word[i+1]=='\0'){
- x[a]='\0';
- a=0;
- // printf("%s",x);
- int flag = 0;
- for(int j=0;j<11;j++){
- if(!strcmp(word1[j],x)){
- printf("%s ",word2[j]);
- fprintf(F2,"%s",word2[j]);
- flag = 1;
- break;
- }
- }
- if (!flag) {
- printBinary(x);
- }
- }
- }
- }
- int main(void){
- fp1=fopen("input_code.txt","r");
- yyin=fp1;yyout=fp2;
- printf("START\n");
- yyparse();
- printf("END\n");
- fclose(fp1);fclose(fp2);
- fclose(fp3);fclose(code);
- //printf("OK\n");
- FILE *F1 = fopen("output1.asm", "r");
- F2 = fopen("output2.txt", "w");
- char chh[100];
- while(EOF!=fscanf(F1, "%[^\n]\n", chh)){
- make_word(chh);
- fprintf(F2,"\n");
- //printf("%s\n", chh);
- //fprintf(F2,"%s\n",chh);
- }
- fclose(F1);fclose(F2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement