CDSS Lab Programs 1-11
CDSS Lab Programs 1-11
CDSS Lab Programs 1-11
1a. Program to count the number, words, spaces and lines in a given input file
Code:
%{
#include<stdio.h>
int c=0;
int w=0;
int s=0;
int l=0;
%}
%%
" " { s++; w++;}
[\n] { l++; w++;}
[\t\n] {w++;}
[^\t\n] {c++;}
%%
int yywrap()
{
return 1;
}
int main()
{
yyin=fopen("Info.txt", "r");
yylex();
printf("Characters = %d\nWords = %d\nSpaces = %d\nLines= %d\n",c,w,s,l);
return 0;
}
Input file:
Output:
2a. Programs to count the numbers of comments lines in a given C program. Also
eliminate them and copy the resulting program into separate file.
Code:
%{
#include<stdio.h>
int s=0,m=0;
%}
%%
"/*"[a-zA-Z0-9' '\t\n]*"*/" m++;
"//".* s++;
%%
void main(){
yyin=fopen("f1.txt","r");
yyout=fopen("f2.txt","w");
yylex();
fclose(yyin);
fclose(yyout);
printf("no of single line comments=%d\n",s);
printf("no of multi line comments=%d\n",m);
}
int yywrap()
{
return 1;
}
3b. Program to count the number of “scanf” and “printf” statements in a C program.
Replace them with “readf” and “writef” statements respectively.
Code:
%{
#include<stdio.h>
int sf=0,pf=0;
%}
%%
"scanf" {sf++; fprintf(yyout,"readf");}
"printf" {pf++; fprintf(yyout,"writef");}
%%
int main()
{
yyin=fopen("f1.c","r");
yyout=fopen("f2.c","w");
yylex();
printf("no of scanf =%d\n no of printf =%d\n",sf,pf);
return 0;
}
Input file with printf and scanf statements:
%%
input:exp {printf("%d\n",$$);exit(0);}
exp:exp'+'exp {$$=$1+$3;}
|exp'-'exp {$$=$1-$3;}
|exp'*'exp {$$=$1*$3;}
|exp'/'exp {if($3==0){printf("Division by zero\n");exit(0);}
else
$$=$1/$3;}
|'('exp')' {$$=$2;}
|num {$$=$1;};
%%
int yyerror()
{
printf("error");
exit(0);
}
int main()
{
printf("Enter the expression:\n");
yyparse();
}
Output:
5. Program to recognize a valid variable which starts with a letter, followed by any
number of letter or digits
Code:
//lex code
%{
#include "y.tab.h"
%}
%%
[a-zA-z] return L;
[0-9] return D;
%%
//yacc code
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token L D
%%
var:L E {printf("Valid Variable\n"); return 0;}
E:E L;
|E D;
|;
%%
int main()
{
printf("Type the variable\n");
yyparse();
return 0;
}
int yyerror()
{
printf("Invalid Variable\n");
exit(0);
}
Output:
6. Program to recognize the strings using the grammar (anbn; n>=0)
Code:
//lex code
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
. return yytext[0];
\n return yytext[0];
%%
//yacc code
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token A B
%%
Str:S '\n' {return 0;}
S:A S B;
|;
%%
int main()
{
printf("Type the string\n");
if (!yyparse())
printf("Valid String\n");
return 0;
}
int yyerror()
{
printf("Invalid String\n");
exit(0);
}
Output:
FILE *fp1,*fp2,*fp3,*fp4;
fp1=fopen("Input.txt","r");
fp2=fopen("Optab.txt","r");
fp3=fopen("Symtabl.txt","w");
fp4=fopen("Out.txt","w");
fscanf(fp1,"%s\t%s\t%s", label,opcode,operand);
if(strcmp(opcode,"START")==0)
{
start=atoi(operand);
locctr=start;
fprintf(fp4,"\t%s\t%s\t%s\n",label,opcode,operand);
fscanf(fp1,"%s\t%s\t%s",label,opcode,operand);
}
else
locctr=0;
while(strcmp(opcode,"END")!=0)
{
fprintf(fp4,"%d\t",locctr);
if(strcmp(label,"**")!=0)
fprintf(fp3,"%s\t%d\n",label,locctr);
fscanf(fp2,"%s\t%s",code,mnemonic);
while(strcmp(code,"END")!=0)
{
if(strcmp(opcode,code)==0)
{
locctr+=3;
break;
}
fscanf(fp2,"%s\t%s",code,mnemonic);
}
if(strcmp(opcode,"WORD")==0)
locctr+=3;
else if(strcmp(opcode,"RESW")==0)
locctr+=(3*(atoi(operand)));
else if(strcmp(opcode,"RESB")==0)
locctr+=atoi(operand);
else if(strcmp(opcode,"BYTE")==0)
++locctr;
fprintf(fp4,"%s\t%s\t%s\t\n",label,opcode,operand);
fscanf(fp1,"%s\t%s\t%s",label,opcode,operand);
}
fprintf(fp4,"%d\t%s\t%s\t%s\n",locctr,label,opcode,operand);
length=locctr-start;
printf("The length of the code:%d\n",length);
fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);
}
Input files:
1. Input.txt
2. Optab.txt
Output:
Output files:
1.Symtabl.txt
3. Out.txt
void FIRST(char);
int count,n=0;
char prodn[10][10],first[10];
void main()
{
int i,choice;
char c,ch;
printf("Enter the number of productions: ");
scanf("%d",&count);
printf("Enter %d productions:\nEpsilon=$\n",count);
for(i=0;i<count;i++)
scanf("%s%c",prodn[i],&ch);
do{
n=0;
printf("Element :");
scanf("%c",&c);
FIRST(c);
printf("\nFIRST(%c)={",c);
for(i=0;i<n;i++)
printf(" %c",first[i]);
printf("}\n");
printf("Press 1 to continue :");
scanf("%d%c",&choice,&ch);
}
while(choice==1);
}
void FIRST(char c)
{
int j;
if(!(isupper(c)))first[n++]=c;
for(j=0;j<count;j++)
{
if(prodn[j][0]==c)
{
if(prodn[j][2]=='S')first[n++]='$';
else if(islower(prodn[j][2]))first[n++]=prodn[j][2];
else FIRST(prodn[j][2]);
}
}
}
Output:
10. C program to implement Shift Reduce Parser for the given grammar:
E → E+E
E → E*E
E →(E)
E→ id
Code:
#include<stdio.h>
#include<string.h>
int k=0,z=0,i=0,j=0,c=0;
char a[16],ac[20],stk[15],act[10];
void check();
int main()
{ puts("GRAMMAR is E->E+E \n E->E*E \n E->(E) \n E->id");
puts("enter input string ");
gets(a);
c=strlen(a);
strcpy(act,"SHIFT->");
puts("stack \t input \t action");
printf("\n$\t%s$\t---",a);
for(k=0,i=0; j<c; k++,i++,j++)
{ if(a[j]=='i' && a[j+1]=='d')
{ stk[i]=a[j];
stk[i+1]=a[j+1];
stk[i+2]='\0';
a[j]=' ';
a[j+1]=' ';
printf("\n$%s\t%s$\t%sid",stk,a,act);
check();
}
else
{ stk[i]=a[j];
stk[i+1]='\0';
a[j]=' ';
printf("\n$%s\t%s$\t%ssymbols",stk,a,act);
check(); }
}
}
void check()
{
strcpy(ac,"REDUCE TO E");
for(z=0; z<c; z++)
if(stk[z]=='i' && stk[z+1]=='d')
{ stk[z]='E';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
j++;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='+' && stk[z+2]=='E')
{ stk[z]='E';
stk[z+1]='\0';
stk[z+2]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='E' && stk[z+1]=='*' && stk[z+2]=='E')
{ stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}
for(z=0; z<c; z++)
if(stk[z]=='(' && stk[z+1]=='E' && stk[z+2]==')')
{ stk[z]='E';
stk[z+1]='\0';
stk[z+1]='\0';
printf("\n$%s\t%s$\t%s",stk,a,ac);
i=i-2;
}}
Output:
DO WHILE:
RECURSION:
----------------------------------------------------------------------------------------------------------------