Académique Documents
Professionnel Documents
Culture Documents
2. #include<stdio.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a[30];
clrscr();
int min=10000,temp=0,i,lev,n,noofc,z;
printf("please enter how many number");
cin>>n;
for(i=0;i<n;i++)
a[i]=0;
cout<<"enter value of root";
cin>>a[0];
for(i=1;i<=n/2;i++)
{
cout<<"please enter no of child of parent with value"<<a[i-1]<<":";
cin>>noofc;
for(int j=1;j<=noofc;j++)
{z=(i)*2+j-2;
cout<<"please enter value of child";
cin>>a[z];
}
}
for(i=n-1;i>=n/2;i--)
{
temp=0;
for(int j=i+1;j>=1;j=j/2)
temp=temp+a[j-1];
if(temp<min)
min=temp;
cout<<"temp min is"<<temp<<"\n";
}
cout<<"min is"<<min;
getch();
}
#include<stdio.h>
#include<conio.h>
//Stack precedence function
int F(char symbol)
{
switch(symbol)
{
case + :
case - :
return 1;
case *:
case ^:
return 6:
case ):
return 0:;
case #:
return -1;
default:
return 8;
}
}
//Input precedence function
int G(char symbol)
{
switch(symbol)
{
case + :
case - :
return 2;
case *:
return 4;
case ^:
return 5:
case (:
return 0;
case ):
return 9:;
case #:
return -1;
default:
return 7;
}
}
Void infix_prefix(char infix[], char prefix[])
{
int top, j, i;
char symbol, s[40];
top = -1;
s[++top] = #;
J = 0;
strrev(infix);
for(i = 0;i < strlen(infix); i++)
{
symbol= infix[i];
while(F(s[top]) > G(symbol))
{
prefix[j] = s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top] = symbol;
else
top--;
}
while(s[top != #)
{
prefix[j++] = s[top--];
}
prefix[j] = \0;
strrev(prefix);
}
void main()
{
char infix[20];
char prefix[20];
printf(/nEnter a valid infix expression\n);
scanf(%s,infix);
infix_prefix(infix, prefix);
}
5) Design LL (1) parser for the given language
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
void main()
{
clrscr();
int i=0,j=0,k=0,m=0,n=0,o=0,o1=0,var=0,l=0,f=0,c=0,f1=0;
char
str[30],str1[40]="E",temp[20],temp1[20],temp2[20],tt[20],t3[20];
strcpy(temp1,'\0');
strcpy(temp2,'\0');
char t[10];
char array[6][5][10] = {
"NT", "<id>","+","*",";",
"E", "Te","Error","Error","Error",
"e", "Error","+Te","Error","\0",
"T", "Vt","Error","Error","Error",
"t", "Error","\0","*Vt","\0",
"V", "<id>","Error","Error","Error"
};
cout << "\n\tLL(1) PARSER TABLE \n";
for(i=0;i<6;i++)
{
for(j=0;j<5;j++)
{
cout.setf(ios::right);
cout.width(10);
cout<<array[i][j];
}
cout<<endl;
}
cout << endl;
cout << "\n\tENTER THE STRING :";
gets(str);
if(str[strlen(str)-1] != ';')
{
cout << "END OF STRING MARKER SHOULD BE ';'";
getch();
exit(1);
}
cout << "\n\tCHECKING VALIDATION OF THE STRING ";
cout <<"\n\t" << str1;
i=0;
while(i<strlen(str))
{
again:
if(str[i] == ' ' && i<strlen(str))
{
cout << "\n\tSPACES IS NOT ALLOWED IN SOURSE STRING ";
getch();
exit(1);
}
temp[k]=str[i];
temp[k+1]='\0';
f1=0;
again1:
if(i>=strlen(str))
{
getch();
exit(1);
}
for(int l=1;l<=4;l++)
{
if(strcmp(temp,array[0][l])==0)
{
f1=1;
m=0,o=0,var=0,o1=0;
strcpy(temp1,'\0');
strcpy(temp2,'\0');
int len=strlen(str1);
while(m<strlen(str1) && m<strlen(str))
{
if(str1[m]==str[m])
{
var=m+1;
temp2[o1]=str1[m];
m++;
o1++;
}
else
{
if((m+1)<strlen(str1))
{
m++;
temp1[o]=str1[m];
o++;
}
else
m++;
}
}
temp2[o1] = '\0';
temp1[o] = '\0';
t[0] = str1[var];
t[1] = '\0';
for(n=1;n<=5;n++)
{
if(strcmp(array[n][0],t)==0)
break;
}
strcpy(str1,temp2);
strcat(str1,array[n][l]);
strcat(str1,temp1);
cout << "\n\t" <<str1;
getch();
if(strcmp(array[n][l],'\0')==0)
{
if(i==(strlen(str)-1))
{
int len=strlen(str1);
str1[len-1]='\0';
cout << "\n\t"<<str1;
cout << "\n\n\tENTERED STRING IS
VALID";
getch();
exit(1);
}
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
goto again1;
}
if(strcmp(array[n][l],"Error")==0)
{
cout << "\n\tERROR IN YOUR SOURCE STRING";
getch();
exit(1);
}
strcpy(tt,'\0');
strcpy(tt,array[n][l]);
strcpy(t3,'\0');
f=0;
for(c=0;c<strlen(tt);c++)
{
t3[c]=tt[c];
t3[c+1]='\0';
if(strcmp(t3,temp)==0)
{
f=0;
break;
}
else
f=1;
}
if(f==0)
{
strcpy(temp,'\0');
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
i++;
k=0;
goto again;
}
else
{
strcpy(temp1,'\0');
strcpy(temp2,'\0');
strcpy(t,'\0');
goto again1;
}
}
}
i++;
k++;
}
if(f1==0)
cout << "\nENTERED STRING IS INVALID";
else
cout << "\n\n\tENTERED STRING IS VALID";
getch();
}
OUTPUT
*********
LL(1)
NT
E
e
T
t
V
PARSER
<id>
Te
Error
Vt
Error
<id>
TABLE
+
Error
+Te
Error
Error
*
Error
Error
Error
*Vt
Error
;
Error
Error
Error
{
char stack[20],ip[20],opt[10][10][1],ter[10];
int i,j,k,n,top=0,col,row;
clrscr();
for(i=0;i<10;i++){stack[i]=NULL; ip[i]=NULL;
for(j=0;j<10;j++){opt[i][j][1]=NULL;}}
printf("Enter the no.of terminals:");
scanf("%d",&n);
printf("\nEnter the terminals:");
scanf("%s",ter);
printf("\nEnter the table values:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the value for %c %c:",ter[i],ter[j]);
scanf("%s",opt[i][j]);
}
}
printf("\nOPERATOR PRECEDENCE TABLE:\n");
for(i=0;i<n;i++){printf("\t%c",ter[i]);}
printf("\n");
for(i=0;i<n;i++){printf("\n%c",ter[i]);
for(j=0;j<n;j++){printf("\t%c",opt[i][j][0]);}}
stack[top]='$';
printf("\nEnter the input string:");
scanf("%s",ip);
i=0;
printf("\nSTACK\t\t\tINPUT STRING\t\t\tACTION\n");
printf("\n%s\t\t\t%s\t\t\t",stack,ip);
while(i<=strlen(ip))
{
for(k=0;k<n;k++)
{
if(stack[top]==ter[k])
col=k;
if(ip[i]==ter[k])
row=k;
}
if((stack[top]=='$')&&(ip[i]=='$')){
printf("String is accepted");
break;}
%}
%%
[0-9]+ {yylval.dval=yytext[0]; return NUM;}
\n {return 0;}
. {return yytext[0];}
%%
void yyerror(char* str)
{
printf("\n%s",str);
}
char gencode(char ch1,char first,char op,char second)
{
printf("\n%c = %c %c %c\n",ch,first,op,second);
return ch++;
}
main()
{
yyparse();
return 0;
}
/********* codegen.y *********/
%{
#include<stdio.h>
int aaa;
%}
%union {
char dval;
}
%token <dval> NUM
%type <dval> E
%left '+' '-'
%left '*' '/'
%%
statement : E {printf("\nt = %c\n This Three Address Code Generate by AAA...\n",$1);}
;
E : E '+' E {$$=gencode($$,$1,'+',$3);}
| E '-' E {$$=gencode($$,$1,'-',$3);}
| E '*' E {$$=gencode($$,$1,'*',$3);}
| E '/' E {$$=gencode($$,$1,'/',$3);}
| '(' E ')' {$$=$2;}
| NUM {$$=$1;}
;
%%
/****** OUTPUT **********/
[a38@localhost ~]$ ./a.out
1+2*5-6/4
a=2*5
b=1+a
c=6/4
d=b-c
t=d
This Three Address Code Generate by AAA...
[a38@localhost ~]$ ./a.out
1+2*(3+4)/5
a=3+4
b=2*a
c=b/5
d=1+c
t=d
This Three Address Code Generate by AAA...
#include <stdlib.h>
#include <string.h>
int label[20];
int no = 0;
int main()
{
FILE *fp1, *fp2;
char fname[10], op[10], ch;
char operand1[8], operand2[8], result[8];
int i = 0, j = 0;
printf("\n Enter filename of the intermediate code");
scanf("%s", &fname);
fp1 = fopen(fname, "r");
fp2 = fopen("target.txt", "w");
if (fp1 == NULL || fp2 == NULL)
{
printf("\n Error opening the file");
exit(0);
}
while (!feof(fp1))
{
fprintf(fp2, "\n");
fscanf(fp1, "%s", op);
i++;
if (check_label(i))
fprintf(fp2, "\nlabel#%d", i);
if (strcmp(op, "print") == 0)
{
fscanf(fp1, "%s", result);
fprintf(fp2, "\n\t OUT %s", result);
}
if (strcmp(op, "goto") == 0)
{
fscanf(fp1, "%s %s", operand1, operand2);
fprintf(fp2, "\n\t JMP %s,label#%s", operand1, operand2);
label[no++] = atoi(operand2);
}
if (strcmp(op, "[]=") == 0)
{
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n\t STORE %s[%s],%s", operand1, operand2, result);
}
if (strcmp(op, "uminus") == 0)
{
fscanf(fp1, "%s %s", operand1, result);
fprintf(fp2, "\n\t LOAD -%s,R1", operand1);
fprintf(fp2, "\n\t STORE R1,%s", result);
}
switch (op[0])
{
case '*':
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n \t LOAD", operand1);
fprintf(fp2, "\n \t LOAD %s,R1", operand2);
fprintf(fp2, "\n \t MUL R1,R0");
fprintf(fp2, "\n \t STORE R0,%s", result);
break;
case '+':
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n \t LOAD %s,R0", operand1);
fprintf(fp2, "\n \t LOAD %s,R1", operand2);
fprintf(fp2, "\n \t ADD R1,R0");
fprintf(fp2, "\n \t STORE R0,%s", result);
break;
case '-':
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n \t LOAD %s,R0", operand1);
fprintf(fp2, "\n \t LOAD %s,R1", operand2);
fprintf(fp2, "\n \t SUB R1,R0");
fprintf(fp2, "\n \t STORE R0,%s", result);
break;
case '/':
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n \t LOAD %s,R0", operand1);
fprintf(fp2, "\n \t LOAD %s,R1", operand2);
fprintf(fp2, "\n \t DIV R1,R0");
fprintf(fp2, "\n \t STORE R0,%s", result);
break;
case '%':
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n \t LOAD %s,R0", operand1);
fprintf(fp2, "\n \t LOAD %s,R1", operand2);
fprintf(fp2, "\n \t DIV R1,R0");
fprintf(fp2, "\n \t STORE R0,%s", result);
break;
case '=':
fscanf(fp1, "%s %s", operand1, result);
fprintf(fp2, "\n\t STORE %s %s", operand1, result);
break;
case '>':
j++;
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n \t LOAD %s,R0", operand1);
fprintf(fp2, "\n\t JGT %s,label#%s", operand2, result);
label[no++] = atoi(result);
break;
case '<':
fscanf(fp1, "%s %s %s", operand1, operand2, result);
fprintf(fp2, "\n \t LOAD %s,R0", operand1);
fprintf(fp2, "\n\t JLT %s,label#%d", operand2, result);
label[no++] = atoi(result);
break;
}
}
fclose(fp2);
fclose(fp1);
fp2 = fopen("target.txt", "r");
if (fp2 == NULL)
{
printf("Error opening the file\n");
exit(0);
}
do
{
ch = fgetc(fp2);
printf("%c", ch);
}
while (ch != EOF);
fclose(fp1);
return 0;
}
int check_label(int k)
{
int i;
for (i = 0; i < no; i++)
{
if (k == label[i])
return 1;
}
return 0;
}
7) Design
%{#include<stdio.h>#include "y.tab.h"%}%%[0-9]+
{yylval.dval=atof(yytext);return DIGIT;}\ n | . r e t u r n
y y t e x t [ 0 ] ; %%
<parser.y>
%{/*This YACC specification file generates the LALR parser
for the programconsidered in experiment
4.*/#include<stdio.h>%}%union{double dval;}%token <dval>
DIGIT%type <dval> expr %type <dval> term%type <dval>
factor %%l i n e : e x p r ' \ n ' { printf("%g\n",$1);};e x p r :
e x p r ' + ' t e r m { $ $ = $ 1 + $ 3 ; } | term;term: term '*'
factor {$$=$1 * $3 ;}| factor ;f a c t o r : ' ( ' e x p r
')'{$$=$2 ;}
16
CD LAB PROGRAMS
| DIGIT;%%int main(){yyparse();}yyerror(char
*s){ printf("%s",s);}
Output: