Professional Documents
Culture Documents
ROLL: 1951021
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){
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); }
}
int main()
{
FILE *myfile =
fopen("ex11.txt", "r"); if
(!myfile) {
}
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;
}
|
;
int main()
{
FILE *myfile = fopen("ex12.txt", "r");
if (!myfile) {
printf("File not found");
return -1;
}
yyin = myfile;
yyparse();
}
v)Flex:
%{
#include "ex13.tab.h"
#include<string.h>
%}
%option noyywrap
%%
%%
int main()
{
FILE *myfile =
fopen("ex13.txt", "r"); if
(!myfile) {
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
printf("error\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 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++;}
;
%%
void printarray()
{
int i;
printf("Array elements: ");
for(i=0;i<2;i++)
{
printf("%d ",arr[i]);
}
}
int main()
{
FILE *myfile =
fopen("ex10.txt", "r"); if
(!myfile) {