Compiler Design Practical File
Compiler Design Practical File
7.
Write a program for operator Precedence Parser.
8.
Write a program to remove left recursion.
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
i=1;
break;
default:
printf("Invalid Choice\n");
}
}
getch();
}
void push()
{
int data;
if(top==max)
{
printf("Stack Overflow");
}
else
{
printf("Enter the element to insert\n");
scanf("%d",&data);
top=top+1;
a[top]=data;
}
}
void pop()
{
int temp;
if(top==0)
{
printf("Stack Underflow");
}
else
{
temp=a[top];
top=top-1;
printf("\nElement is deleted\n");
}
}
void display()
{
int j;
printf”**** stack elements are ****”);
for(j=1;j<=top;j++)
{
printf("%d\n",a[j]);
}
}
Output
Program: 2(a)
#include<conio.h>
void main(){char
array[32][20]={"if","else","break","continue","typedef","for","while","do","void","float","ch
ar","int","auto","case","const","default","double","enum","extern","goto","long","register","r
eturn","short","signed","sizeof","static","struct","switch","union","unsigned","volatile"};
int flag=1;
char input[1000];
char ch='y';
int i,j,k;
clrscr();
while(ch=='y' || ch=='Y'){
flag=1; // the input is not keyword
printf("Enter any word to check whether it is a keyword or not\n");
scanf("%s",&input);
i=0,j=0,k=0;
for(i=0;i<32;i++){
while(array[i][j]==input[k] && array[i][j] !='\0' && input[k] != '\0')
{
j++;k++;
}
if(array[i][j]=='\0' && input[k]=='\0')
{
flag=0;
break;
}
}// end of for loop
if(flag==0)
printf("The given word is a keyword\n");
else
printf("The given word is not a keyword \n");
Output
Program: 2(b)
else
{
d++;
break;
}
}
if(d==0)
{
printf("\nentered string is valid string constant");
}
else
printf("\nnot a valid string constant");
getch();
}
Output
Program: 3
Output
Program: 4
The main job of a lexical analyzer (scanner) is to break up an input stream into more
usable elements (tokens).
a = b + c * d;
ID ASSIGN ID PLUS ID MULT ID SEMI
Lex is an utility to help you rapidly generate your scanner.
Lexical analyzers tokenize input streams
Tokens are the terminals of a language
>English
words, punctuation marks
>Programming language
Identifiers, operators, keywords
Regular expressions define terminals/tokens.
Lex source is separated into three sections by %% delimiters and general format of Lex
source is:
{definitions}
%% (required)
{transition rules}
%% (optional)
{user subroutines}
digit [0-9]
letter [a-zA-Z]
%%
\n printf(“new line\n”);
%%
main() {
yylex();
}
LEX Source to C Program
– partitioning the input into strings which match the given expressions and
Overview of LEX
LEX Usage
%{
C declarations
%}
Yacc declarations
%%
Grammar rules
%% Additional C code
C compiler (cc)
Working of YACC:
y.tab.c
%{
#include <stdio.h>
%}
statement:
LEX v/s YACC:NAME '=' expression
| expression { printf("= %d\n", $1); }
Lex ;
int main(void)
LEX
{ with YACC:
yyparse();
Lex Source(Lexical Rules) YACC Rules(Grammar Rules)
return 0;
}
LEX YACC
Lex.yy.c y.tab.c
call
Yylex() Yyparse()
input Parsed input
return token
Program: 5
if(a[0][0]==c)f[m++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))f[m++]=c;
for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))f[m++]=a[k][2];
else first(a[k][2]);
}
}
Output
Program: 7
int ssm=0,row=0,col=0;
node *temp;
clrscr();
matrix_value();
while(exp[ssm] != '\0')
{
if(ssm==0)
{
tos++;
oprate[tos].op_name = exp[tos];
}
else
{
if(isOperator(exp[ssm]) == -1)
{
oprate[tos].t = (node*) malloc (sizeof(node));
oprate[tos].t->data = exp[ssm];
oprate[tos].t->lptr = '\0';
oprate[tos].t->rptr = '\0';
}
else
{
row = getOperatorPosition(oprate[tos].op_name);
col = getOperatorPosition(exp[ssm]);
if(matrix[row][col] == 0)
{
tos++;
oprate[tos].op_name = exp[ssm];
}
else if(matrix[row][col] == 1)
{
temp = (node*) malloc (sizeof(node));
temp->data = oprate[tos].op_name;
temp->lptr = (oprate[tos-1].t);
temp->rptr = (oprate[tos].t);
tos--;
oprate[tos].t = temp;
ssm--;
}
else if(matrix[row][col] == 2)
{
//temp = (node*) malloc (sizeof(node));
temp = oprate[tos].t;
tos--;
oprate[tos].t = temp;
}
else if(matrix[row][col] == 3)
{
printf("\nExpression is Invalid...\n");
printf("%c %c can not occur simultaneously\n",oprate[tos].op_name,exp[ssm]);
break;
}
}
}
ssm++;
}
printf("show tree \n\n\n");
show_tree(oprate[tos].t);
printf("Over");
getch();
getch();
}
int isOperator(char c)
{
int i=0;
for(i=0;i<5;i++)
{
if (c==cur_op[i] || c==stack_op[i])
break;
}
if(i==5)
return (-1);
else return i;
}
int getOperatorPosition(char c)
{
int i;
for(i=0;i<5;i++)
{
if (c==cur_op[i] || c==stack_op[i])
break;
}
return i;
if(start->rptr != NULL)
show_tree(start->rptr);
printf("%c \n",start->data);
}
void matrix_value(void)
{
int i,j;
printf("OPERATOR PRECEDENCE MATRIX\n");
printf("===========================\n ");
for(i=0;i<5;i++)
{
printf("%c ",cur_op[i]);
for(j=0;j<5;j++)
{
if(matrix[i][j] == 0)
printf("< ");
else if(matrix[i][j] == 1)
printf("> ");
else if(matrix[i][j] == 2)
printf("= ");
else if(matrix[i][j] == 3)
printf(" ");
}
printf("\n");
}
}
Output
Program: 8
#include<stdio.h>
#include<conio.h>
#include<string.h>
//Structure Declaration
struct production
char lf;
char rt[10];
int prod_rear;
int fl;
};
//Variables Declaration
int b=-1,d,f,q,n,m=0,c=0;
char terminal[20],nonterm[20],alpha[10],extra[10];
char epsilon='^';
void main()
clrscr();
cin>>q;
cout<<"Enter the special characters for your production: ";
for(int cnt=0;cnt<q;cnt++)
cin>>alpha[cnt];
//Input of Productions
cin>>n;
for(cnt=0;cnt<=n-1;cnt++)
cin>>prodn[cnt].lf;
cout<<"->";
cin>>prodn[cnt].rt;
prodn[cnt].prod_rear=strlen(prodn[cnt].rt);
prodn[cnt].fl=0;
for(int cnt1=0;cnt1<n;cnt1++)
for(int cnt2=cnt1+1;cnt2<n;cnt2++)
if(prodn[cnt1].lf==prodn[cnt2].lf)
cnt=0;
int p=-1;
while((prodn[cnt1].rt[cnt]!='\0')&&(prodn[cnt2].rt[cnt]!='\0'))
{
if(prodn[cnt1].rt[cnt]==prodn[cnt2].rt[cnt])
extra[++p]=prodn[cnt1].rt[cnt];
prodn[cnt1].fl=1;
prodn[cnt2].fl=1;
else
if(p==-1)
break;
else
int h=0,u=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
prodn_new[++b].lf=alpha[c];
for(g=cnt;g<=prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[u++]=prodn[cnt1].rt[g];
m=1;
break;
}
cnt++;
if((prodn[cnt1].rt[cnt]==0)&&(m==0))
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
if((prodn[cnt2].rt[cnt]==0)&&(m==0))
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt1].rt[g];
c++;
m=0;
//Display of Output
cout<<"\n\n********************************";
cout<<"\n********************************";
cout<<endl;
for(int cnt3=0;cnt3<=b;cnt3++)
cout<<prodn_new[cnt3].lf;
cout<<"->";
cout<<prodn_new[cnt3].rt;
cout<<endl<<endl;
for(int cnt4=0;cnt4<n;cnt4++)
if(prodn[cnt4].fl==0)
cout<<prodn[cnt4].lf;
cout<<"->";
cout<<prodn[cnt4].rt;
cout<<endl<<endl;
}
getch();
Output
Program: 9
#include<stdio.h>
#include<conio.h>
#include<string.h>
//Structure Declaration
struct production
char lf;
char rt[10];
int prod_rear;
int fl;
};
//Variables Declaration
int b=-1,d,f,q,n,m=0,c=0;
char terminal[20],nonterm[20],alpha[10],extra[10];
char epsilon='^';
void main()
clrscr();
cin>>q;
cout<<"Enter the special characters for your production: ";
for(int cnt=0;cnt<q;cnt++)
cin>>alpha[cnt];
//Input of Productions
cin>>n;
for(cnt=0;cnt<=n-1;cnt++)
cin>>prodn[cnt].lf;
cout<<"->";
cin>>prodn[cnt].rt;
prodn[cnt].prod_rear=strlen(prodn[cnt].rt);
prodn[cnt].fl=0;
for(int cnt1=0;cnt1<n;cnt1++)
for(int cnt2=cnt1+1;cnt2<n;cnt2++)
if(prodn[cnt1].lf==prodn[cnt2].lf)
cnt=0;
int p=-1;
while((prodn[cnt1].rt[cnt]!='\0')&&(prodn[cnt2].rt[cnt]!='\0'))
{
if(prodn[cnt1].rt[cnt]==prodn[cnt2].rt[cnt])
extra[++p]=prodn[cnt1].rt[cnt];
prodn[cnt1].fl=1;
prodn[cnt2].fl=1;
else
if(p==-1)
break;
else
int h=0,u=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
prodn_new[++b].lf=alpha[c];
for(g=cnt;g<=prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[u++]=prodn[cnt1].rt[g];
m=1;
break;
}
cnt++;
if((prodn[cnt1].rt[cnt]==0)&&(m==0))
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt2].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt2].rt[g];
if((prodn[cnt2].rt[cnt]==0)&&(m==0))
int h=0;
prodn_new[++b].lf=prodn[cnt1].lf;
strcpy(prodn_new[b].rt,extra);
prodn_new[b].rt[p+1]=alpha[c];
prodn_new[++b].lf=alpha[c];
prodn_new[b].rt[0]=epsilon;
prodn_new[++b].lf=alpha[c];
for(int g=cnt;g<prodn[cnt1].prod_rear;g++)
prodn_new[b].rt[h++]=prodn[cnt1].rt[g];
c++;
m=0;
//Display of Output
cout<<"\n\n********************************";
cout<<"\n********************************";
cout<<endl;
for(int cnt3=0;cnt3<=b;cnt3++)
cout<<prodn_new[cnt3].lf;
cout<<"->";
cout<<prodn_new[cnt3].rt;
cout<<endl<<endl;
for(int cnt4=0;cnt4<n;cnt4++)
if(prodn[cnt4].fl==0)
cout<<prodn[cnt4].lf;
cout<<"->";
cout<<prodn[cnt4].rt;
cout<<endl<<endl;
}
getch();
Output
Program: 10