You are on page 1of 17

NAME: BIDISHA DEB

ROLL: 1951021

COMPILER DESIGN LAB-ASSIGNMENT(CSEN-4161)

B. Tech. (CSE) 4th Year 7th Semester.

QUESTIONS

1i) Flex
%{
#include "ex8.tab.h"
%}
%option noyywrap

TYPE int|char
ID [a-zA-Z]+
EQUAL "="
SEMICOLON ";"
SPACE " "

%%
"=" {return EQUAL;}
"+" {return ADD;}
"\n" {printf(" ");}

{ID}({SPACE})*{EQUAL}({SPACE})*{ID}{SEMICOLON}
{yylval.sval = strdup(yytext); return ASSIGN;}
{TYPE}({SPACE}){ID}{SEMICOLON} {yylval.sval = strdup(yytext);
return DECL;} %%

Bison
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void yyerror(char*);
extern int yylex();
extern int yyparse();
extern FILE *yyin;
%}
%union{
char* sval;
}
%token <sval> DECL
%token <sval> ASSIGN
%token EQUAL ADD
%right EQUAL
%left ADD
%type <sval> D
%type <sval> A

%%
E : T P;
T : D{printf("decend\n");}
D:D
DECL{printf("%s\n",$2);
}|
{printf("decbegin\n");}

;
P : A{printf("progend\n");}
A : A ASSIGN{printf("%s\n",$2);}
| {printf("progbegin\n");}
;
%%
int main()
{

FILE *myfile =
fopen("ex8.txt","r");
if(!myfile){

printf("File not found");


return -1;
}
yyin = myfile;
yyparse();
}

void yyerror(char* msg)


{
printf(msg);
printf("\n");
}
ii) Flex:
%{
#include<stdio.h>
#include<string.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
void yyerror(char*);
void declare(char*,char*);
char name[10][10];
char type[10][10];
int pos=0;
%}
%union{
char* sval;
}
%token EQUAL ID INT CHAR
%type <sval> ID
%%
S:D
|
;

D : INT ID {printf("int
%s\n",$2);strcpy(name[pos],$2);strcpy(type[pos],"int");pos++;}
| CHAR ID {printf("char
%s\n",$2);strcpy(name[pos],$2);strcpy(type[pos],"char
");pos++;} | ID EQUAL ID {declare($1,$3); printf("%s =
%s\n",$1,$3);} ;
%%

void declare(char*
s1,char* s2) {

int i,pos1,pos2;
for(i=0;i<pos;i++){
if(strcmp(name[i],s1)==0)
pos1=i;
}
printf("%c
%s\n",type[i],s2); }

void yyerror(char* msg)


{
printf(msg);
printf("\n");

}
int main()
{
FILE *myfile =
fopen("ex11.txt", "r"); if
(!myfile) {

printf("File not found");


return -1;

}
yyin = myfile;
yyparse();
}
iv) Flex:
%{
#include "ex12.tab.h"
#include<string.h>
%}
%option noyywrap
%%
"++" {return PLUS;}
"--" {return MINUS;}
"=" {return EQUAL;}
";" {return SEMICOLON;}
[a-zA-Z]+ {yylval.sval=strdup(yytext);return ID;}

Bison:
%{
#include<stdio.h>
#include<string.h>
extern int yylex();
extern int yyparse();
void yyerror(char*);
extern FILE *yyin;
%}
%union{
char* sval;
}

%token ID PLUS MINUS EQUAL SEMICOLON


%type <sval> ID
%%
E:TE

|
;

T : ID EQUAL PLUS ID SEMICOLON


{printf("%s=%s+1\n",$4,$4);printf("%s=%s\n",$1,$4);}
| ID EQUAL ID PLUS SEMICOLON
{printf("%s=%s\n",$1,$3);printf("%s=%s+1\n",$3,$3);}
| ID EQUAL MINUS ID SEMICOLON{printf("%s=%s
1\n",$4,$4);printf("%s=%s\n",$1,$4);}
| ID EQUAL ID MINUS
SEMICOLON{printf("%s=%s\n",$1,$3);printf("%s=%s
1\n",$3,$3);}
;
%%

int main()
{
FILE *myfile = fopen("ex12.txt", "r");
if (!myfile) {
printf("File not found");
return -1;
}
yyin = myfile;
yyparse();
}

void yyerror(char* msg)


{
printf(msg);
printf("\n");

v)Flex:
%{
#include "ex13.tab.h"
#include<string.h>
%}
%option noyywrap
%%

"=" {return EQUAL;}


";" {return SEMICOLON;}
"/n" {}
[a-zA-Z]+
{yylval.sval=strdup(yytext);return ID;}
%%
Bison:
%{
#include<stdio.h>
#include<string.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
void yyerror(char*);
void display(char*);
char name[5];
int pos=0;
%}
%union{
char* sval;
}
%token ID EQUAL SEMICOLON
%type <sval> ID
%%
E : T SEMICOLON;
T : ID EQUAL T
{name[pos]=$1;pos++;} | ID
{name[pos]=$1;pos++;display(name
);} ;

%%

void display(char *s){


int i;
for(i=pos-1;i>0;i--){
printf("%s=%s;",s[i-1],s[i]);
}
}

int main()
{
FILE *myfile =
fopen("ex13.txt", "r"); if
(!myfile) {

printf("File not found");


return -1;
}
yyin = myfile;
yyparse();
}

void yyerror(char* msg)


{
printf(msg);
printf("\n");
}

2) Flex:
%{
#include "ex9.tab.h"
#include<string.h>
%}
%option noyywrap
%%
"=" {return EQUAL;}
"+" {return PLUS;}
";" {return SEMICOLON;}
[a-zA-Z]+ {yylval.sval=strdup(yytext);return ID;}
[0-9]+\.[0-9]+ { yylval.fval = atof(yytext); return
FLOAT; } [0-9]+ { yylval.ival = atoi(yytext); return
INT; } "\n" {printf("");}

Bison:
%{
#include<stdio.h>
#include<string.h>
extern int yylex();
extern int yyparse();
extern FILE *yyin;
void yyerror(char*);
void check(char*,char*);
char name[10][10];
char type[10][10];
int pos=0;
%}
%union{
char* sval;
floatfval;
int ival;
}
%token EQUAL PLUS SEMICOLON ID INT FLOAT
%type <sval> ID
%type <ival> INT
%type <fval> FLOAT

%%
S:ST
|
;
T : ID EQUAL INT SEMICOLON {printf("%d is of type
int\n",$3);
strcpy(name[pos],$1);strcpy(type[pos],"int");pos++;}
| ID EQUAL FLOAT SEMICOLON {printf("%f is of type
float\n",$3);strcpy(name[pos],$1);strcpy(type[pos],"float");pos++;
} | ID EQUAL ID PLUS ID SEMICOLON {printf("%s is of type
",$1);check($3,$5);} ;

%%
void check(char* s1,char* s2)
{
int i,pos1,pos2;
for(i=0;i<pos;i++){
if(strcmp(name[i],s1)==0)
pos1=i;
if(strcmp(name[i],s2)==0)
pos2=i;
}

if(strcmp(type[pos1],type[pos2])==0 &&
type[pos1]=="int") printf("type int\n");

else if(strcmp(type[pos1],type[pos2])==0 &&


type[pos1]=="float") printf("type float\n");

else
printf("error\n");
}

void yyerror(char* msg)


{
printf(msg);
printf("\n");
}

int main()
{
FILE *myfile = fopen("ex9.txt", "r");
if (!myfile) {
printf("File not found");
return -1;
}
yyin = myfile;
yyparse();
}
4) Flex:
%{
#include "ex10.tab.h"
#include<string.h>
%}
%option noyywrap
%%
"int" {return INT;}
[a-zA-Z][a-zA-Z]+ {return ID;}
[wW] {return W;}
[rR] {return R;}
[pP] {return P;}
[mM] {return M;}
[lL] {return L;}
[sS] {return S;}
[0-9]+ {yylval.ival=atoi(yytext);return
NUM;} ";" {return SEMICOLON;}

%%
Bison:
%{
#include<stdio.h>
#include<string.h>
extern int yylex();
extern int yyparse();
void yyerror(char*);
void printarray();
extern FILE *yyin;
int arr[2];
int pos=0;
%}
%union{
int ival;
}
%token W R P M L S NUM ID INT SEMICOLON
%type <ival> NUM
%%
E : D T;
D : INT ID SEMICOLON {printf("Array is declared\n");}

T : T W R NUM {arr[pos]=$4; if(pos+1>1) printf("Array out of


bounds"); else pos++;}

| T W L NUM {arr[pos]=$4; if(pos-1<0) printf("Array index out of


bounds");else pos--;}

| T W S NUM {arr[pos]=$4;}
| T R R {printf("%d\n",arr[pos]); if(pos+1>1) printf("Array out of
bounds");else pos++;}

| T R L {printf("%d\n",arr[pos]); if(pos-1<0) printf("Array


index out of bounds");else pos--;}
| T R S {printf("%d\n",arr[pos]);}
| T P {printarray();}
| T M L NUM {if(pos-$4 >= 0) pos =
pos-$4;} | T M R NUM {if(pos+$4 <2)
pos = pos+$4;} |

;
%%

void printarray()
{
int i;
printf("Array elements: ");
for(i=0;i<2;i++)
{
printf("%d ",arr[i]);
}
}

void yyerror(char* msg)


{
printf(msg);
printf("\n");
}

int main()
{
FILE *myfile =
fopen("ex10.txt", "r"); if
(!myfile) {

printf("File cannot be opened");


return -1;
}
yyin =
myfile;
yyparse();
}

You might also like