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;
- 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;
- };
- %%
- 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);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement