Madhav Institute of Technology and Science, Gwalior
Madhav Institute of Technology and Science, Gwalior
Practical File
On
Compiler Design
Session: Jan – Jun 2020
#include<stdio.h>
#include<string.h>
#include<math.h>
int ninputs;
int dfa[100][2][100] = {0};
int state[10000] = {0};
char ch[10], str[1000];
int go[10000][2] = {0};
int arr[10000] = {0};
int main()
{
int st, fin, in;
int f[10];
int i,j=3,s=0,final=0,flag=0,curr1,curr2,k,l;
int c;
printf("\nFollow the one based indexing\n");
printf("\nEnter the number of states::");
scanf("%d",&st);
printf("\nGive state numbers from 0 to %d",st-1);
for(i=0;i<st;i++)
state[(int)(pow(2,i))] = 1;
printf("\nEnter number of final states\t");
scanf("%d",&fin);
printf("\nEnter final states::");
for(i=0;i<fin;i++)
scanf("%d",&f[i]);
int p,q,r,rel;
printf("\nEnter the number of rules according to NFA::");
scanf("%d",&rel);
printf("\n\nDefine transition rule as \"initial state input symbol final state\"\n");
Input :
9
2
- - BH
- - CE
D--
--G
-F-
--G
- - BH
I--
-- -
OUTPUT:
STATES OF NFA : A, B, C, D, E, F, G, H, I,
GIVEN SYMBOLS FOR NFA: 0, 1, eps
NFA STATE TRANSITION TABLE
STATES |0 |1 eps
--------+------------------------------------
A |- |- |BH
B |- |- |CE
C |D |- |-
D |- |- |G
E |- |F |-
F |- |- |G
G |- |- |BH
H |I |- |-
I |- |- |-
e-Closure (C) : C
e-Closure (E) : E
e-Closure (H) : H
e-Closure (I) : I
Program 2
Write a program to minimize dfa
#include <stdio.h>
#include <string.h>
#define STATES 99
#define SYMBOLS 20
printf("\n-----+--");
for (i = 0; i < nsymbols; i++) printf("-----");
printf("\n");
void load_DFA_table()
{
while (*s) {
if (*s != '@' && *s != equiv_class) return 0;
s++;
}
if (i=is_one_nextstate(state_flags))
return i-'0'; /* deterministic next states */
else {
strcpy(stnt[*pn], state_flags);
return (*pn)++;
}
}
int init_equiv_class(char statename[][STATES+1], int n, char *finals)
{
int i, j;
return 2;
}
int get_optimized_DFA(char stnt[][STATES+1], int n,
int dfa[][SYMBOLS], int n_sym, int newdfa[][SYMBOLS])
{
int n2=n; /* 'n' + <num. of state-division info> */
int i, j;
char nextstate[STATES+1];
return n2;
}
*(s+n) = ch;
*(s+n+1) = '\0';
}
return n;
}
while (1) {
print_equiv_classes(stnt, n);
n2 = get_optimized_DFA(stnt, n, dfa, n_sym, newdfa);
if (n != n2)
n = set_new_equiv_class(stnt, n, newdfa, n_sym, n_dfa);
else break; /* equiv. class segmentation ended!!! */
}
void main()
{
load_DFA_table();
print_dfa_table(DFAtab, N_DFA_states, N_symbols, DFA_finals);
#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||
strcmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("printf",str)==0||
strcmp("switch",str)==0||strcmp("case",str)==0)
printf("\n%s is a keyword",str);
else
printf("\n%s is an identifier",str);
void main()
FILE *f1,*f2,*f3;
char c,str[10],st1[10];
int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;
f1=fopen("input","r");
f2=fopen("identifier","w");
f3=fopen("specialchar","w");
while((c=getc(f1))!=EOF)
if(isdigit(c))
tokenvalue=c-'0';
c=getc(f1);
while(isdigit(c))
tokenvalue*=10+c-'0';
c=getc(f1);
} num[i++]=tokenvalue;
ungetc(c,f1);
} else
if(isalpha(c))
{ putc(c,f2);
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{ putc(c,f2);
c=getc(f1);
} putc(' ',f2);
ungetc(c,f1);
} else
if(c==' '||c=='\t')
printf(" ");
else if(c=='\n')
lineno++;
else
putc(c,f3);
} fclose(f2);
fclose(f3);
fclose(f1);
for(j=0;j<i;j++)
printf("\t%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
while((c=getc(f2))!=EOF)
if(c!=' ')
str[k++]=c;
else
{ str[k]='\0';
keyword(str);
k=0;
} fclose(f2);
f3=fopen("specialchar","r");
while((c=getc(f3))!=EOF)
printf("\t%c",c);
printf("\n");
fclose(f3);
OUTPUT
Program 4
Write a program to parse using brute force technique of top down parsing
#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;
cin>>n;
for(i=0;i<n;i++)
a[i]=0;
cin>>a[0];
for(i=1;i<=n/2;i++)
cin>>noofc;
for(int j=1;j<=noofc;j++)
{z=(i)*2+j-2;
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();
}
Program 5
Develop LL(1) parser
#include<stdio.h>
#include<conio.h>
void main()
char pro[10][10],first[10][10],follow[10][10],nt[10],ter[10],res[10][10][10],temp[10];
int npro,noter=0,nont=0,i,j,k,flag=0,count[10][10],row,col,l,m,n,index;
clrscr();
for(i=0;i<10;i++)
for(j=0;j<10;j++)
count[i][j]=NULL;
for(k=0;k<10;k++){
res[i][j][k]=NULL; }
scanf("%d",&npro);
for(i=0;i<npro;i++)
scanf("%s",pro[i]);
for(i=0;i<npro;i++)
flag=0;
for(j=0;j<nont;j++)
{
if(nt[j]==pro[i][0])
flag=1;
if(flag==0)
nt[nont]=pro[i][0];
nont++;
for(i=0;i<nont;i++)
printf("First value(%c):",nt[i]);
scanf("%s",first[i]);
for(i=0;i<nont;i++)
printf("Follow value(%c):",nt[i]);
scanf("%s",follow[i]);
for(i=0;i<nont;i++)
flag=0;
for(j=0;j<strlen(first[i]);j++)
{
for(k=0;k<noter;k++)
if(ter[k]==first[i][j])
flag=1;
if(flag==0)
if(first[i][j]!='#')
ter[noter]=first[i][j];
noter++;
for(i=0;i<nont;i++)
flag=0;
for(j=0;j<strlen(follow[i]);j++)
for(k=0;k<noter;k++)
if(ter[k]==follow[i][j])
flag=1;
}
if(flag==0)
ter[noter]=follow[i][j];
noter++;
for(i=0;i<nont;i++)
for(j=0;j<strlen(first[i]);j++)
flag=0;
if(first[i][j]=='#')
col=i;
for(m=0;m<strlen(follow[col]);m++)
for(l=0;l<noter;l++)
if(ter[l]==follow[col][m])
row=l;
temp[0]=nt[col];
temp[1]='-' ;
temp[2]='>';
temp[3]='#';
temp[4]='\0';
printf("temp %s",temp);
strcpy(res[col][row],temp);
count[col][row]+=1;
for(k=0;k<10;k++){
temp[k]=NULL; }
else{
for(l=0;l<noter;l++)
if(ter[l]==first[i][j])
row=l;
for(k=0;k<npro;k++){
if(nt[i]==pro[k][0])
col=i;
if((pro[k][3]==first[i][j])&&(pro[k][0]==nt[col]))
strcpy(res[col][row],pro[k]);
count[col][row]+=1;
}
else
if((isupper(pro[k][3]))&&(pro[k][0]==nt[col]))
flag=0;
for(m=0;m<nont;m++)
if(nt[m]==pro[k][3]){index=m;flag=1;}
if(flag==1){
for(m=0;m<strlen(first[index]);m++)
{if(first[i][j]==first[index][m])
{strcpy(res[col][row],pro[k]);
count[col][row]+=1;}
}}}}}
}}
printf("LL1 Table\n\n");
flag=0;
for(i=0;i<noter;i++)
printf("\t%c",ter[i]);
for(j=0;j<nont;j++)
printf("\n\n%c",nt[j]);
for(k=0;k<noter;k++)
{
printf("\t%s",res[j][k]);
if(count[j][k]>1){flag=1;}
getch();
Program 6
Develop an operator precedence parser
#include <stdio.h>
#include <string.h>
// function f to exit from the loop
// if given condition is not true
void f()
{
printf("Not operator grammar");
exit(0);
}
void main()
{
char grm[20][20], c;
int i, n, j = 2, flag = 0;
// taking number of productions from user
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%s", grm[i]);
for (i = 0; i < n; i++) {
c = grm[i][2];
while (c != '\0') {
if (grm[i][3] == '+' || grm[i][3] == '-'
|| grm[i][3] == '*' || grm[i][3] == '/')
flag = 1;
else { flag = 0;
f();
}
if (c == '$') { flag = 0;
f();}
c = grm[i][++j];
}
}
if (flag == 1)
printf("Operator grammar");
}}
Input :3
A=A*A
B=AA
A=$
Output : Not operator grammar
Input :2
A=A/A
B=A+A
Output : Operator grammar
Program 7
Develop a recursive descent parser
int main()
{
// E is a start symbol.
E();
// if lookahead = $, it represents the end of the string
// Here l is lookahead.
if (l == '$')
printf("Parsing Successful");
}
// Definition of E, as per the given production
E()
{
if (l == 'i') {
match('i');
E'();
}
}
// Definition of E' as per the given production
E'()
{
if (l == '+') {
match('+');
match('i');
E'();
}
else
return ();
}
// Match function
match(char t)
{
if (l == t) {
l = getchar();
}
else
printf("Error");
}
Example:
OUTPUT
Parsing Successful
Program 8
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct three
{
char data[10],temp[7];
}s[30];
void main()
{
char d1[7],d2[7]="t";
int i=0,j=1,len=0;
FILE *f1,*f2;
clrscr();
f1=fopen("sum.txt","r");
f2=fopen("out.txt","w");
while(fscanf(f1,"%s",s[len].data)!=EOF)
len++;
itoa(j,d1,7);
strcat(d2,d1);
strcpy(s[j].temp,d2);
strcpy(d1,"");
strcpy(d2,"t");
if(!strcmp(s[3].data,"+"))
{
fprintf(f2,"%s=%s+%s",s[j].temp,s[i+2].data,s[i+4].data);
j++;
}
else if(!strcmp(s[3].data,"-"))
{
fprintf(f2,"%s=%s-%s",s[j].temp,s[i+2].data,s[i+4].data);
j++;
}
for(i=4;i<len-2;i+=2)
{
itoa(j,d1,7);
strcat(d2,d1);
strcpy(s[j].temp,d2);
if(!strcmp(s[i+1].data,"+"))
fprintf(f2,"\n%s=%s+%s",s[j].temp,s[j-1].temp,s[i+2].data);
else if(!strcmp(s[i+1].data,"-"))
fprintf(f2,"\n%s=%s-%s",s[j].temp,s[j-1].temp,s[i+2].data);
strcpy(d1,"");
strcpy(d2,"t");
j++;
}
fprintf(f2,"\n%s=%s",s[0].data,s[j-1].temp);
fclose(f1);
fclose(f2);
getch();
}
Input: sum.txt
OUTPUT: out.txt
t1=in1+in2
t2=t1+in3
t3=t2-in4
out=t3
(2.)Polish notation
#include<stdio.h>
char stack[20];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
}
Void main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c",pop());
}
}
Output:
Enter the expression :: a+b*c
abc*+
Program 10
Write a program to simulate heap storage allocation strategy
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
{int data;
}node;
node *create();
void main()
{int choice,val;
char ans;
node *head;
head=NULL;
do
printf("\n1.create");
printf("\n2.display");
printf("\n5.quit");
scanf("%d",&choice);
switch(choice)
{case 1:head=create();break;
case 2:display(head);break;
case 3:head=insert(head);break;
case 4:dele(&head);
break;
case 5:exit(0);
default:
while(choice!=5);}
node* create(){
node *temp,*New,*head;
int val,flag;
char ans='y';
node *get_node();
temp=NULL;
flag=TRUE;
do{
scanf("%d",&val);
New=get_node();
if(New==NULL)
New->data=val;
if(flag==TRUE)
{head=New;
temp=head;
flag=FALSE;
}else
{temp->next=New;
temp=New;
while(ans=='y');
return head;
}node *get_node()
{node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return temp;
{node *temp;
temp=head;
if(temp==NULL)
return;
}while(temp!=NULL)
{printf("%d->",temp->data);
temp=temp->next;
}printf("NULL");
{node *temp;
int found;temp=head;
if(temp==NULL)
return NULL;}found=FALSE;
{if(temp->data!=key)
temp=temp->next;
else found=TRUE;
}if(found==TRUE)
scanf("%d",&choice);
switch(choice)
{case 1:head=insert_head(head);break;
case 2:insert_last(head);break;
case 3:insert_after(head);break;
}return head;
{node *New,*temp;
New=get_node();
scanf("%d",&New->data);
if(head==NULL)
head=New;
else
{temp=head;
New->next=temp;
head=New;}
return head;
{node *New,*temp;
New=get_node();
printf("\nenter the element which you want to insert");
scanf("%d",&New->data);
if(head==NULL)
head=New;
else
{temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=New;
New->next=NULL;
{int key;
node *New,*temp;
New=get_node();
scanf("%d",&New->data);
if(head==NULL)
{head=New;
}else
scanf("%d",&key);
temp=head;
do
{if(temp->data==key)
{New->next-temp->next;
temp->next=New;
return;
}else
temp=temp->next;
}while(temp!=NULL);
int flag;
temp=head;
if(temp==NULL)
return NULL;
flag=FALSE;
prev=NULL;
{if(temp->data!=val)
{prev=temp;
temp=temp->next;
}else
flag=TRUE;
}if(flag)
return prev;
else
return NULL;
{node *temp,*prev;
int key;
temp=*head;
if(temp==NULL)
return;
scanf("%d",&key);
temp=search(*head,key);
if(temp!=NULL)
{prev=get_prev(*head,key);
if(prev!=NULL)
{prev->next=temp->next;
free(temp);}
else
{*head=temp->next;
free(temp);}
OUTPUT
Program 11
int COMMENT=0;
%}
identifier [a-zA-Z][a-zA-Z0-9]*
%%
int |
float |
char |
double |
while |
for |
struct |
typedef |
do |
if |
break |
continue |
void |
switch |
return |
else |
\)(\:)? {if(!COMMENT)printf("\n\t");ECHO;printf("\n");}
\( ECHO;
\<= |
\>= |
\< |
== |
%%
FILE *file;
file=fopen("var.c","r");
if(!file)
exit(0);
yyin=file;
yylex();
printf("\n");
return(0);
int yywrap()
{
return(1);
INPUT:
//var.c
#include<stdio.h>
#include<conio.h>
void main()
int a,b,c;
a=1;
b=2;
c=a+b;
printf("Sum:%d",c);
OUTPUT
11. Generate YACC specification for a syntactic categories
Program name:arith_id.l
%{
#include “y.tab.h”
%}
%%
[a-z A-Z]*[0-9]* {
return ID;
return yytext[0];
\n return 0;
%%
int yywrap()
return 1;
%{
#include
%}
%%
statement: A’=’E
|E{
$$ = $1;
};
E: E’+’ID
| E’-’ID
| E’*’ID
| E’/’ID
| ID
%%
main()
do
yyparse();
}while(!feof(yyin));
yyerror(char*s)
{
}
OUTPUT
[root@localhost]# lex arith_id.1
[root@localhost]# ./a.out
x=a+b;
Identifier is x
Operator is EQUAL
Identifier is a
Operator is PLUS
Identifier is b
Program 12