

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


%option noyywrap %{ #include"stdio.h" #include"y.tab.h" %} %% [a-z][a-zA-Z0-9]*|[0-9]+ { strcpy(yylval.vname,yytext); return NAME; } [ |\t] ; .|\n { return yytext[0]; } %%
#include<stdio.h> #include<ctype.h> #include<string.h> FILE fpOut; %} %union { char vname[10]; int val; } %left '+' '-' %left '' '/' %token
fprintf(fpOut,"SUB AX, %s\n",$ 3 ); } | NAME '*' NAME { fprintf(fpOut,"MOV AX, %s\n",$ 1 ); fprintf(fpOut,"MUL AX, %s\n",$ 3 ); } | NAME '/' NAME { fprintf(fpOut,"MOV AX, %s\n",$ 1 ); fprintf(fpOut,"DIV AX, %s\n",$ 3 ); } | NAME { fprintf(fpOut,"MOV AX, %s\n",$ 1 ); strcpy($$, $ 1 ); } ; %% FILE *yyin; main() { FILE *fpInput; fpInput = fopen("input1.c","r"); if(fpInput=='\ 0 ') { printf("file read error"); //exit(0); } fpOut = fopen("output.c","w"); if(fpOut=='\ 0 ') { printf("file creation error"); //exit(0); } yyin = fpInput; yyparse(); _fcloseall(); } yyerror(char *msg) { printf("%s",msg); }