CC Lab Programs
CC Lab Programs
CC Lab Programs
a Recognation of Comments
#include<stdio.h>
#include<conio.h>
void main()
{
char s[25];
int i=0;
int state=1;
scanf("%s",s);
clrscr();
while(state>=1 && state<=4 && s[i]!='\0')
{
switch(state)
{
case 1: if(s[i]=='/')
{
i++;
state=2;
}
else
{
state=6;
}
break;
case 2: if(s[i]=='*')
{
i++;
state=3;
}
else
{
if(s[i]=='/')
{
state=5;
}
else
{
state=6;
}
}
break;
case 3: if(s[i]=='*')
{
i++;
state=4;
}
else
{
i++;
}
break;
case 4: if(s[i]=='/')
{
state=5;
}
else
{
if(s[i]=='*')
{
i++;
}
else
{
i++;
state=3;
}
}
break;
}
}
if(state==5)
{
printf("accepted");
}
else
printf("not accepted");
}
1.c.Recognation identifier
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int ip=0,r,x=1,i;
char c,input[5];
clrscr();
printf("\n\n enter one identifier: ");
scanf("%s",input);
while(input[ip]!='\0')
{
ip++;
}
printf("\n\n length of given string is: %d",ip);
if(ip<6)
{
i=isalpha(input[0]);
if(i==0)
printf("\n\n Identifier should start with ALPHABET....");
else
{
for(r=0;r<=ip;r++)
{
c=input[r];
switch(c)
{
case '!':
case '@':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':x=0;
break;
}
}
if(x==0)
printf(" \n\n Identifier should not contain any special symbol");
else
printf(" \n\n YOU ENTERED AN IDENTIFIER ");
}
}
else
printf(" \n\n PLEASE ENTER <=5 IDENTIFIERS ");
getch(); }
len=strlen(s);
if(lastentry+1>MAX)
Error_Message("symbol table is full");
if(lastchar+len+1>MAX)
Error_Message("symbol table is full");
lastentry=lastentry+1;
symtable[lastentry].token=tok;
symtable[lastentry].lexptr=&lexemes[lastchar+1];
lastchar=lastchar+len+1;
strcpy(symtable[lastentry].lexptr,s);
return lastentry;
}
void initialize()
{
struct entry *ptr;
for(ptr=keywords;ptr->token;ptr++)
insert(ptr->lexptr,ptr->token);
}
int lexer()
{
int t;
int val,i=0;
while(1)
{
t=getchar();
if(t==' '||t=='\t');
else if(t=='\n')
lineno=lineno+1;
else if(t=='('||t==')')
return PAREN;
else if(t=='<'||t=='>'||t=='<='||t=='>='||t=='!=')
return REL_OP;
else if(t=='=')
return ASSIGN;
else if(isdigit(t))
{
ungetc(t,stdin);
scanf("%d",&tokenval);
return NUM;
}
else if(isalpha(t))
{
while(isalnum(t))
{
buffer[i]=t;
t=getchar();
i=i+1;
if(i>=SIZE)
Error_Message("compiler error");
}
buffer[i]=EOS;
if(t!=EOF)
ungetc(t,stdin);
val=look_up(buffer);
if(val==0)
val=insert(buffer,ID);
tokenval=val;
return symtable[val].token;
}
else if(t==EOF)
return DONE;
else
{
tokenval=NONE;
return t;
}
}
}
void main()
{
int lookahead;
char ans;
clrscr();
printf("\n program for lexical analysis\n");
initialize();
printf("\n enter the expression and put ; at the end");
printf("\n press cntrl z to terminate");
lookahead=lexer();
while(lookahead!=DONE)
{
if(lookahead==NUM)
{
printf("\n number");
printf("%d",tokenval);
}
if(lookahead=='+'||lookahead=='-'||lookahead=='*'||lookahead=='/')
printf("\n operator");
if(lookahead==ID)
{
printf("\n identifier");
printf("%s",symtable[tokenval].lexptr);
}
if(lookahead==KEYWORD)
printf("\n keyword");
if(lookahead==ASSIGN)
printf("\n assignment operator");
if(lookahead==REL_OP)
printf("\n relational operator");
lookahead==lexer();
}}
4.b. Identifying the string start and end with k using lex
%{
#include<stdio.h>
%}
begin_with_k k.*
end_with_k .*k
%%
{begin_with_k} { printf("%s is a word that begin with k", yytext); }
{end_with_k} { printf("%s is a word that ends with k",yytext); }
%%
main()
{
yylex();
return 0;
}
5.First production
#include<stdio.h>
#include<conio.h>
#include<string.h>
char pro[10][20];
void first(char,int);
char fir[10][20];
char com[10][40];
int n,c,i,j,j1,l=0,p,b,m;
void main()
{
int k,q;
clrscr();
printf("enter no of productions\n");
scanf("%d",&n);
printf("enter the productions");
for(i=0;i<n;i++)
scanf("%s",pro[i]);
for(i=0;i<n;i++)
{
c=0;
q=0;
com[i][q++]=pro[i][0];
first(pro[i][0],q);
fir[i][c]='\0';
}
for(i=0;i<n;i++)
for(j=0;j<strlen(fir[i]);j++)
for(k=j+1;k<strlen(fir[i]);k++)
if(fir[i][j]==fir[i][k])
fir[i][k]=' ';
printf("firsts of productions\n");
for(i=0;i<n;i++)
{
int j;
printf("\n%c=>",pro[i][0]);
for(j=0;j<strlen(fir[i]);j++)
printf("%3c",fir[i][j]);
}
getch();
}
void first(char t,int q)
{
int k=0;
int m;
if(!(t>='A'&&t<='Z')&&t!='1'&&t!='\0'&&t!='$')
fir[i][c++]=t;
else
for(;k<n;k++)
{
if(pro[k][0]==t)
{
if(pro[k][3]>='A'&&pro[k][3]<='Z')
{
b=0;
for(p=0;p<q;p++)
if(com[i][p]==pro[k][3])
b++;
if(b!=2)
{
com[i][q++]=pro[k][3];
first(pro[k][3],q);
}
m=4;
while(fir[i][c-1]=='$'&&pro[k][m]!='l'&&pro[k][m]!='\0')
{
c--;
q--;
com[i][q++]=pro[k][m];
first(pro[k][m],q);
m++;
}
}
else
fir[i][c++]=pro[k][3];
for(j=4;pro[k][j]!='\0';j++)
{
if(pro[k][j]=='l')
{
j++;
if(pro[k][j]>='A'&&pro[k][j]<='z')
{
b=0;
for(p=0;p<q;p++)
if(com[i][p]==pro[k][j])
b++;
if(b!=2)
{
com[i][q++]=pro[k][j];
first(pro[k][j],q);
}
m=j+1;
while(fir[i][c-1]=='$'&&pro[k][m]!='l'&&pro[k][m]!='\0')
{
c--;
q--;
com[i][q++]=pro[k][m];
first(pro[k][m],q);
m++;
}
}
else
fir[i][c++]=pro[k][j];
}
}
}
}
}
6.LL1 parsing
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
char l[20]={'E','G','G','T','U','U','F','F'};
char r[20][20]={"TG","+TG","@","FU","*FU","@","(E)","i"};
char x[5]={'E','G','T','U','F'};
char y[6]={'i','+','*','(',')','$'};
int tab[5][6],np[10],ra=100,z;
static char temp[10];
char ch[20],t1[10];
int i,j,k=0,m,n,p,h,m1,b=0;
void FIRST(char);
void FOLLOW(char);
void main()
{
clrscr();
printf("\n\n THE GRAMMER IS...:\n\n");
for(i=0;i<8;i++)
{
printf("%c----->",l[i]);
for(j=0;j<4;j++)
printf("%c",r[i][j]);
printf("\n");
}
for(p=0;p<5;p++)
{
for(m=0;m<8;m++)
{ if(x[p]==l[m])
{
n=m;
break;
}
}
k=0;ra=100;z=0;
FIRST(x[p]);
for(j=0;j<k;j++)
{
for(h=0;h<6;h++)
{
if(temp[j]==y[h]&&j<ra)
{
tab[p][h]=n+1;
if(r[n][0]==temp[j])
tab[p][h]=n+1;
else if(r[n+1][0]==temp[j])
tab[p][h]=n+2;
}
if(temp[j]==y[h]&&j>=ra)
{
tab[p][h]=n+2;
if(r[n][0]==temp[j])
tab[p][h]=n+2;
else if(r[n+1][0]==temp[j])
tab[p][h]=n+3;
}
}
}
}
printf("\n");
for(i=0;i<6;i++)
printf("\t %c",y[i]);
for(i=0;i<5;i++)
{
printf("\n\t %c",x[i]);
for(j=0;j<6;j++)
printf("%d \t",tab[i][j]);
}
getch();
return 0;
}
void FOLLOW(char c)
{
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(c==r[i][j])
{
if(r[i][j+1]>='A' && r[i][j+1]<='Z')
FIRST(r[i][j+1]);
else if(r[i][j+1]=='\0')
FOLLOW(l[i]);
else
{
temp[k]=r[i][j+1];
k++;
}
}
}
}
if(c==l[0])
{
temp[k]='$';
k++;
}
}
void FIRST(char c)
{
for(i=0;i<20;i++)
{
if(c==l[i])
{
if(r[i][0]>='A' && r[i][0]<='Z')
FIRST(r[i][0]);
else if(r[i][0]=='@')
{
if(z==0)
{
ra=k;
z++;
}
FOLLOW(l[i]);
}
}
else
{
temp[k]=r[i][0];
k++;
}
}
}
for(l=m;p[l]!='\0';l++)
{
printf("%c",p[l]);
}
printf("\n");
}
void error()
{
printf("Syntax Error");
}
void reduce(int p)
{
int len,k,ad; char src,*dest;
switch(p)
{
case 1:dest="E+T";
src='E';
break;
case 2:dest="T";
src='E';
break;
case 3:dest="T*F";
src='T';
break;
case 4:dest="F";
src='T';
break;
case 5:dest="(E)";
src='F';
break;
case 6:dest="i";
src='F';
break;
default:dest="\0";
src='\0';
break;
}
for(k=0;k<strlen(dest);k++)
{
pop();
popb();
}
pushb(src);
switch(src)
{
case 'E':ad=0;
break;
case 'T':ad=1;
break;
case 'F':ad=2;
break;
default: ad=-1;
break;
}
push(gotot[TOS()][ad]);}
int main()
{
int j,st,ic;
char ip[20]="\0",an;
clrscr();
printf("Enter any String");
gets(ip);
push(0);
display();
printf("\t%s\n",ip);
for(j=0;ip[j]!='\0';)
{
st=TOS();
an=ip[j];
if(an>='a'&&an<='z')
ic=0;
else if(an=='+')
ic=1;
else if(an=='*')
ic=2;
else if(an=='(')
ic=3;
else if(an==')')
ic=4;
else if(an=='$')
ic=5;
else
{
error();
break;
}
if(axn[st][ic][0]==100)
{
pushb(an);
push(axn[st][ic][1]);
display();
j++;
display1(ip,j);
}
if(axn[st][ic][0]==101)
{
reduce(axn[st][ic][1]);
display();
display1(ip,j);
}
if(axn[st][ic][1]==102)
{
printf("Given String is accepted\n");
break;
}
}
getch();
return 0;
}
expression:expression'+'expression {$$=$1+$3;}
|expression'-'expression {$$=$1+$3;}
|expression'*'expression {$$=$1*$3;}
|expression'/'expression
{
if($3==0)
yyerror("divide by zero");
else
$$=$1/$3;}
|expression'^'expression {$$=pow($1,$3);}
;
expression: '-' expression %prec UMINUS {$$=-$2;}
|'('expression')' {$$=$2;}
|LOG expression {$$=log($2)/log(10);}
|nLOG expression {$$=log($2);}
|SINE expression {$$=sin($2*3.14159/180);}
|COS expression {$$=cos($2*3.14159/180);}
|TAN expression {$$=tan($2*3.14159/180);}
|NUMBER { $$ = $1;}
|MEM {$$=memvar;}
;
%%
main()
{
printf("enter expression:");
yyparse();
}
int yyerror(char *error)
{
fprintf(stderr,"%s\n",error);
}
yywrap()
{ return 1;
}
}
switch(op[0])
{ case '*': fscanf(fp1,"%s%s%s",op1,op2,res);
fprintf(fp2,"\n\t LOAD %s,R0",op1);
fprintf(fp2,"\n\t LOAD %s,R1",op2);
fprintf(fp2,"\n\t MUL R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",res);
break;
case '+': fscanf(fp1,"%s%s%s",op1,op2,res);
fprintf(fp2,"\n\t LOAD %s,R0",op1);
fprintf(fp2,"\n\t LOAD %s,R1",op2);
fprintf(fp2,"\n\t ADD R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",res);
break;
case '-': fscanf(fp1,"%s%s%s",op1,op2,res);
fprintf(fp2,"\n\t LOAD %s,R0",op1);
fprintf(fp2,"\n\t LOAD %s,R1",op2);
fprintf(fp2,"\n\t SUB R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",res);
break;
case '/': fscanf(fp1,"%s%s%s",op1,op2,res);
fprintf(fp2,"\n\t LOAD %s,R0",op1);
fprintf(fp2,"\n\t LOAD %s,R!",op2);
fprintf(fp2,"\n\t DIV R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",res);
break;
case '%': fscanf(fp1,"%s%s%s",op1,op2,res);
fprintf(fp2,"\n\t LOAD %s,R0",op1);
fprintf(fp2,"\n\t LOAD %s,R1",op2);
fprintf(fp2,"\n\t DIV R1,R0");
fprintf(fp2,"\n\t STORE R0,%s",res);
break;
case '=': fscanf(fp1,"%s%s",op1,res);
fprintf(fp2,"\n\t STORE %s %s",op1,res);
break;
case '>': j++;
fscanf(fp1,"%s%s%s",op1,op2,res);
fprintf(fp2,"\n\t LOAD %s,R0",op1);
fprintf(fp2,"\n\t JGT %s,label.# %s",op2,res);
label[no++]=atoi(res);
break;
}
}
fclose(fp2);
fclose(fp1);
fp2=fopen("target.txt","r");
if(fp2==NULL)
{ printf("\n error in opening file target.txt");
getch();
exit(0);
}
do{ ch=fgetc(fp2);
printf("%c",ch);
}while(ch!=EOF);
fclose(fp2);
getch();
return 0;
}
int check_label(int k)
{
//printf("in check_label");
int i;
for(i=0;i<no;i++)
{ if(k==label[i])
return 1;
}
return 0;
}
exit(0);
}
struct Intercode code[20];char temp='A';
char addtotable(char operand1,char operand2,char opera)
{
//char temp='A';
code[nIndex].operand1=operand1;
code[nIndex].operand2=operand2;
code[nIndex].opera=opera;
nIndex++;
temp++;
return temp;
}
/*threeaddresscode()
{
int nCnt=0;
char temp='A';
printf("\n\n\t three address codes\n\n");
temp++;
while(nCnt<nIndex)
{
printf("%c=\t",temp);
if(isalpha(code[nCnt].operand1))
printf("%c\t",code[nCnt].operand1);
else
printf("%c\t",temp);
printf("%c\t",code[nCnt].opera);
if(isalpha(code[nCnt].operand2))
printf("%c\t",code[nCnt].operand2);
else
printf("%c\t",temp);
printf("\n");
nCnt++;
temp++;
}
}
*/
void quadruples()
{
int nCnt=0;
char temp='A';
temp++;
printf("\n\n\t Quadruples\n");
printf("\n ID OPERATOR OPERAND1 OPERAND2 \n");
while(nCnt<nIndex)
{
printf("\n %d\t %c\t",nCnt,code[nCnt].opera);
if(isalpha(code[nCnt].operand1))
printf("%c\t",code[nCnt].operand1);
else
printf("%c\t",temp);
//printf("%c\t",code[nCnt].opera);
if(isalpha(code[nCnt].operand2))
printf("%c\t",code[nCnt].operand2);
else
printf("%c\t",temp);
printf("%c\t",temp);
printf("\n");
nCnt++;
temp++;
}
}
main()
{
printf("enter expression");
yyparse();
//threeaddresscode();
quadruples();
}
yywrap()
{
return 1;
}
11.code optimization
#include<stdio.h>
#include<conio.h>
int tc[10],fb=0,i=0,j=0,k=0,p=0,fstar=0,c=-1,c1=0,c2=0,t1,t2,t3,t4,f0=0;
char m[30],temp[30],opt[10][4];
operatormajid(char,char);
void main()
{
int a,d;
clrscr();
for(i=0;i<10;i++)
tc[i]=-1;
printf("\n code stmt evaluation following precedence:");
printf("\n 1.() with in the () stmet should be of the form: x op z");
printf("\n 2.*,/ equal precedence");
printf("\n 3.+,- equal precedence");
printf("\n enter ur code stmt-");
gets(m);
i=0;
while(m[i]!='\0')
{
if(m[i++]=='(')
{
fb++;
break;
}
}
i=0;
printf("\n the intermediate cod may generated as-");
if(fb==1)
{
while(m[i]!='\0')
if(m[i]=='(')
{
temp[j++]='T';
i++;
t3=i;
while(m[i]!=')')
opt[c1][c2++]=m[i++];
for(t4=c1-1;t4>=0;t4--)
if(strcmp(opt[c1],opt[t4])==0)
{
tc[p++]=t4;
f0=1;
}
if(f0==0)
{
tc[p++]=k++;
printf("\n T%d=",k-1);
while(m[t3]!=')')
printf("%c",m[t3++]);
}
i++;
c1++;
c2=f0=0;
}
else if (m[i]!='(')
temp[j++]=m[i++];
if(fb==1)
{
temp[j]='\0';
for(i=0;temp[i]!='\0';i++)
m[i]=temp[i];
m[i]='\0';
}
}
/*end of evaluating sub emxp*/
a=operatormajid('*','/');
d=operatormajid('+','-');
if(a==0 && d==0 && m[1]=='=')
printf("\n%s%d",m,k-1);
getch();
}
/*function to evaluate operatores*/
operatormajid(char haj,char haj1)
{
m1:for(i=0;m[i]!='\0';i++)
if(m[i]==haj|| m[i]==haj1)
{
fstar++;
break;
}
if(fstar==1)
{
for(j=0;j<i;j++)
if(m[j]=='T')
c++;
printf("\n T%d=",k);
if(m[i-1]=='T'&&m[i+1]=='T')
{
printf("\n%c%d%c%c%d",m[i-1],tc[c],m[i],m[i+1],tc[c+1]);
tc[c]=k++;
for(t2=c+1;t2<9;t2++)
tc[t2]=tc[t2+1];
}
else if(m[i-1]!='T' &&m[i+1]!='T')
{
printf("%c%c%c",m[i-1],m[i],m[i+1]);
if(c==-1)
{
for(t1=9;t1>0;t1--)
tc[t1]=tc[t1-1];
tc[0]=k++;
}
else if(c>=0)
{
for(t1=9;t1>c+1;t1--)
tc[t1]=tc[t1-1];
tc[t1]=k++;
}
}
else if(m[i-1]=='T' && m[i+1]!='T')
{
printf("%c%d%c%c",m[i-1],tc[c],m[i],m[i+1]);
tc[c]=k++;
}
else if(m[i-1]!='T'&& m[i+1]=='T')
{
printf("%c%c%c%d",m[i-1],m[i],m[i+1],tc[c+1]);
tc[c+1]=k++;
}
for(t1=0;t1<i-1;t1++)
temp[t1]=m[t1];
temp[t1++]='T';
for(t2=i+2;m[t2]!='\0';t2++)
temp[t1++]=m[t2];
temp[t1++]='\0';
fstar=0;
for(i=0;temp[i]!='\0';i++)
m[i]=temp[i];
m[i]='\0';
c=-1;
goto m1;
}
else
return 0;
}