compiler design lab programs

34
Mukesh Kumar 1110751908 CSE 7 th Sem Course: Bachelor of Technology Branch: Computer Science & Engineering Semester: 7 th GURU JAMBHESHWAR UNIVERSITY OF SCIENCE & TECHNOLOGY, HISAR Submitted To: - Submitted By:- Er. Isha Nagpal Mukesh Kumar Asstt. Professor in CSE Deptt. 1110751908 Department of Computer Science & Engineering Prannath Parnami Institute of Management & Technology, Hisar Prannath Parnami Universe, Website: ppu.edu.in

Upload: guru-janbheshver-university-hisar

Post on 16-Jul-2015

302 views

Category:

Education


8 download

TRANSCRIPT

Mukesh Kumar 1110751908 CSE 7th Sem

Course: Bachelor of Technology

Branch: Computer Science & Engineering

Semester: 7th

GURU JAMBHESHWAR UNIVERSITY OF

SCIENCE & TECHNOLOGY, HISAR

Submitted To: - Submitted By:-

Er. Isha Nagpal Mukesh Kumar

Asstt. Professor in CSE Deptt. 1110751908

Department of Computer Science & Engineering

Prannath Parnami Institute of Management & Technology, Hisar

Prannath Parnami Universe, Website: ppu.edu.in

Mukesh Kumar 1110751908 CSE 7th Sem

INDEX

Sr. No.

Name of Experiment Date Sign.

1. Write a program to design lexical analyzer.

2. Write a program to generate three address codes

for assignment, arithmetic and relational

expressions.

3.

Write a program to check whether a string to the

grammar or not.

4. Write a program to find the number of

whitespaces & newline characters.

5. Write a program to find the leading terminals.

6. Write a program to find the trailing terminals.

7. Write a program for computation of first.

8. Write a program to show the operations of stack.

9. Write a program to perform the operations on

stack by using linked list.

10. Write a program to perform the operations on file.

Mukesh Kumar 1110751908 CSE 7th Sem

Program-01

Write a program to design lexical analyzer.

#include<ctype.h>

#include<stdio.h>

#include<string.h>

void keyword(char str[10])

{

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("static",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;

printf("\n enter the c program");

gets(st1);

f1=fopen("input.txt","w");

while((c=getchar())!=EOF)

putc(c,f1);

fclose(f1);

f1=fopen ("input.txt","r");

f2=fopen ("identifier.txt","w");

f3=fopen ("specialchar.txt","w");

while((c=getc(f1))!=EOF)

{

if(isdigit(c))

{

tokenvalue=c-'0';

Mukesh Kumar 1110751908 CSE 7th Sem

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);//to get unsigned char to the spcified stream

}

else

if(c==' '||c=='\t')

printf("");

else

if(c=='\n')

lineno++;

else

putc(c,f3);

}

fclose(f2);

fclose(f3);

fclose(f1);

printf("\n the no's in the program are");

for(j=0;j<i;j++)

printf("%d",num[j]);

printf("\n");

f2=fopen("identifier.txt","r");

Mukesh Kumar 1110751908 CSE 7th Sem

k=0;

printf("the keyword and identifiers are:");

while((c=getc(f2))!=EOF)

{

if(c!=' ')

str[k++]=c;

else

{

str[k]='\0';

keyword(str);

k=0;

}

}

fclose(f2);

f3=fopen ("\n Specialchar.txt","r");

printf("\n special characters are");

while((c=getc(f3))!=EOF)

printf("%c",c);

printf("\n");

fclose(f3);

printf("Total no. os lines are: %d", lineno);

getch();

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-02

Write a program to generate three address code for assignment, arithmetic and

relational expressions.

#include<stdio.h>

#include<string.h>

int i,ch,j,l,addr=100;

char ex[10],exp[10],exp1[10],exp2[10],id1[5],op[5],id2[5];

void main()

{

clrscr();

while(1)

{

printf("\n 1. Assignment Expression or Arithmetic Expression \n 2. Relational or Expression \n

3. Exit \n Enter the choice:");

scanf("%d", &ch);

switch(ch)

{

case 1:

printf("\n Enter the expression with Assignment Expression Operator or Arithmetic Operator:");

scanf("%s",exp);

l=strlen(exp);

exp2[0]='\0';

i=0;

while(exp[i]!='=')

{

i++;

}

strncat(exp2,exp,i);

strrev(exp);

exp1[0]='\0';

strncat(exp1,exp,l-(i+1));

strrev(exp1);

printf("three address code:\ntemp=%s\n%s=temp\n",exp1,exp2);

break;

case 2:

Mukesh Kumar 1110751908 CSE 7th Sem

printf("Enter the expression with relational operator");

scanf("%s%s%s",&id1,&op,&id2);

if(((strcmp(op,"<")==0)||(strcmp(op,">")==0)||(strcmp(op,"<=")

==0)||(strcmp(op,">=")==0)||(strcmp(op,"==")==0)||(strcmp(op,"!=")==0))==0)

printf("Expression is error");

else

{

printf("\n %d\tif%s%s%s goto %d",addr,id1,op,id2,addr+3);

addr++;

printf("\n %d\t T:=0",addr);

addr++;

printf("\n %d\t goto %d",addr,addr+2);

addr++;

printf("\n %d\t T:=1",addr);

}

break;

case 3:

exit(0);

}

}

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-03

Write a program to check whether a string to the grammar or not.

#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

{

char a1[100],b1[100],c1[100],p1[10],s1;

int v=0,i,j,k,m,n=0,x,a=0,l1,l2,l3,loc,lx,lx1,loc1,s;

clrscr();

printf("enter the start symbol");

scanf("%c", & s1);

printf("enter the production");

scanf("%s",&a1);

printf("enter the string to be searching");

scanf("%s",&p1);

strcpy(b1,a1);

printf("b1=%s" ,b1);

l1=strlen(a1);

l2=strlen(b1);

l3=strlen(p1);

lx=l1;

lx1=l1;

printf("a1=%s", a1);

printf("l1=%d", l1);

while(((2*(13+2))>11))

{

for(i=0;i<11;i++)

{

if(a1[i]==s1)

{

loc=i;

}}

printf("\n loc=%d", loc);

x=strcmp(a1,p1);

if(x==0)

{

printf("\n string present");

Mukesh Kumar 1110751908 CSE 7th Sem

}

else

{

l1=l1+l1;

for(j=loc;j<11;j++)

{

s=j+lx1;

a1[s]=a1[j];

}

printf("shift=%s",a1);

v=loc;

for(i=0;i<lx;i++)

{

a1[v]=b1[i];

v=v+1;

}

v=0;

m=0;

printf("string with start symbol=%s",a1);

for(j=0;j<=11;j++)

{

if(a1[j]!=s1)

{

a1[m]=a1[j];

m=m+1;

}}

printf("\n string after removing start symbol=%s",a1);

}

if(x==0)

{

printf("\nstring found");

break;

}}

if(x!=0)

{

printf("\n string not found ");

}

getch();

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-04

Write a program to find the number of whitespaces & newline characters.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main ()

{

char str[200],ch;

int a=0, space=0,newline=0;

clrscr();

printf("enter a string ( press escape to quit entering)");

ch=getche();

while((ch!=27)&&(a<199))

{

str[a]=ch;

if (str[a] ==' ')

{

space++;

}

if(str[a]==13)

{

newline++;

printf("\n");

}

a++;

ch=getche();

}

printf("\n the no. of lines used =%d", newline);

printf("\n the no. of spaces used =%d", space);

getch();

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-05

Write a program to find the leading terminals.

#include<stdio.h>

#include<string.h>

#include<conio.h>

int nt,t,top=0;

char s[50],NT[10],T[10],st[50],l[10][10],tr[50][50];

int searchnt(char a)

{

int count=-1,i;

for(i=0;i<nt;i++)

{

if(NT[i]==a)

return i;

}

return count;

}

int searchter(char a)

{

int count=-1,i;

for(i=0;i<t;i++)

{

if(T[i]==a)

return i;

}

return count;

}

void push(char a)

{

s[top]=a;

top++;

}

char pop()

{

top--;

return s[top];

}

void installl(int a,int b)

Mukesh Kumar 1110751908 CSE 7th Sem

{

if(l[a][b]=='f')

{

l[a][b]='t';

push(T[b]);

push(NT[a]);

}}

void main()

{

int i,s,k,j,n;

char pr[30][30],b,c;

clrscr();

printf("Enter the no of production :");

scanf("%d",&n);

printf("Enter the productions one by one\n");

for(i=0;i<n;i++)

scanf("%s", &pr[i]);

nt=0;

t=0;

for(i=0;i<n;i++)

{

if((searchnt(pr[i][0]))==-1)

NT[nt++]=pr[i][0];

}

for(i=0;i<n;i++){

for(j=3;j<strlen(pr[i]);j++){

if(searchnt(pr[i][j])==-1)

{

if(searchter(pr[i][j])==-1)

T[t++]=pr[i][j];

}}}

for(i=0;i<nt;i++){

for(j=0;j<t;j++)

l[i][j]='f';

}

for(i=0;i<nt;i++){

for(j=0;j<t;j++)

tr[i][j]='f';

}

Mukesh Kumar 1110751908 CSE 7th Sem

for(i=0;i<nt;i++)

{

for(j=0;j<n;j++)

{

if(NT[(searchnt(pr[j][0]))]==NT[i])

{

if(searchter(pr[j][3])!=-1)

installl(searchnt(pr[j][0]),searchter(pr[j][3]));

else

{

for(k=3;k<strlen(pr[j]);k++){

if(searchnt(pr[j][k])==-1)

{

installl(searchnt(pr[j][0]),searchter(pr[j][k]));

break;

}}}}}}

while(top!=0)

{

b=pop();

c=pop();

for(s=0;s<n;s++){

if(pr[s][3]==b)

installl(searchnt(pr[s][0]),searchter(c));

}}

for(i=0;i<nt;i++)

{

printf("Leading %c:\t{", NT[i] );

for(j=0;j<t;j++){

if(l[i][j]=='t')

printf("%c , ", T[j]);

}

printf("}\n");

}

getch();

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-06

Write a program to find the trailing terminals.

#include<stdio.h>

#include<string.h>

#include<conio.h>

int nt,t,top=0;

char s[50],NT[10],T[10],st[50],l[10][10],tr[50][50];

int searchnt(char a)

{

int count=-1,i;

for(i=0;i<nt;i++)

{

if(NT[i]==a)

return i;

}

return count;

}

int searchter(char a)

{

int count=-1,i;

for(i=0;i<t;i++)

{

if(T[i]==a)

return i;

}

return count;

}

void push(char a)

{

s[top]=a;

top++;

}

char pop()

{

top--;

return s[top];

}

void installt(int a,int b){

Mukesh Kumar 1110751908 CSE 7th Sem

if(tr[a][b]=='f'){

tr[a][b]='t';

push(T[b]);

push(NT[a]);

}}

void main()

{

int i,s,k,j,n;

char pr[30][30],b,c;

clrscr();

printf("Enter the no of productions:");

scanf("%d",&n);

printf("Enter the productions one by one\n");

for(i=0;i<n;i++)

scanf("%s",&pr[i]);

nt=0;

t=0;

for(i=0;i<n;i++){

if((searchnt(pr[i][0]))==-1)

NT[nt++]=pr[i][0];

}

for(i=0;i<n;i++)

{

for(j=3;j<strlen(pr[i]);j++)

{

if(searchnt(pr[i][j])==-1)

{

if(searchter(pr[i][j])==-1)

T[t++]=pr[i][j];

}}}

for(i=0;i<nt;i++){

for(j=0;j<t;j++)

l[i][j]='f';

}

for(i=0;i<nt;i++){

for(j=0;j<t;j++)

tr[i][j]='f';

}

for(i=0;i<nt;i++){

Mukesh Kumar 1110751908 CSE 7th Sem

for(j=0;j<n;j++){

if(NT[(searchnt(pr[j][0]))]==NT[i])

{

if(searchter(pr[j][3])!=-1)

top=0;

for(i=0;i<nt;i++)

{

for(j=0;j<n;j++){

if(NT[searchnt(pr[j][0])]==NT[i])

{

if(searchter(pr[j][strlen(pr[j])-1])!=-1)

installt(searchnt(pr[j][0]),searchter(pr[j][strlen(pr[j])-1]));

else

{

for(k=(strlen(pr[j])-1);k>=3;k--)

{

if(searchnt(pr[j][k])==-1)

{

installt(searchnt(pr[j][0]),searchter(pr[j][k]));

break;

}}}}}}

while(top!=0)

{

b=pop();

c=pop();

for(s=0;s<n;s++){

if(pr[s][3]==b)

installt(searchnt(pr[s][0]),searchter(c));

}}

for(i=0;i<nt;i++){

printf("Trailing %c\t{", NT[i]);

for(j=0;j<t;j++){

if(tr[i][j]=='t')

printf("%c ,", T[j]);

}

printf("}\n");

}

getch();

}}}}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-07

Write a program for computation of first.

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char t[5],nt[10],p[5][5],first[5][5],temp;

int i,j,not,nont,k=0,f=0;

clrscr();

printf("Enter the no. of Non-terminals in the grammer:");

scanf("%d",&nont);

printf("\nEnter the Non-terminals in the grammer:");

for(i=0;i<nont;i++)

{

scanf("\n%c",&nt[i]);

}

printf("\nEnter the no. of Terminals in the grammer: ( Enter e for absiline ) ");

scanf("%d",&not);

printf("Enter the Terminals in the grammer:");

for(i=0;i<not||t[i]=='$';i++)

{

scanf("\n%c",&t[i]);

}

for(i=0;i<nont;i++)

{

p[i][0]=nt[i];

first[i][0]=nt[i];

}

printf("\nEnter the productions :\n");

for(i=0;i<nont;i++)

{

scanf("%c",&temp);

printf("\nEnter the production for %c ( End the production with '$' sign ) :",p[i][0]);

for(j=0;p[i][j]!='$';)

{

Mukesh Kumar 1110751908 CSE 7th Sem

j+=1;

scanf("%c",&p[i][j]);

}}

for(i=0;i<nont;i++)

{

printf("\nThe production for %c -> ",p[i][0]);

for(j=1;p[i][j]!='$';j++)

{

printf("%c",p[i][j]);

}}

for(i=0;i<nont;i++)

{

f=0;

for(j=1;p[i][j]!='$';j++)

{

for(k=0;k<not;k++){

if(f==1)

break;

if(p[i][j]==t[k])

{

first[i][j]=t[k]; first[i][j+1]='$'; f=1;

break;

}

else if(p[i][j]==nt[k])

{

first[i][j]=first[k][j];

if(first[i][j]=='e')

continue; first[i][j+1]='$'; f=1;

break;

}}}}

for(i=0;i<nont;i++)

{

printf("\n\nThe first of %c -> ",first[i][0]);

for(j=1;first[i][j]!='$';j++)

{

printf("%c\t",first[i][j]);

}}

getch();

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-08

Write a program to show the operations of stack.

#include<stdio.h>

#include<conio.h>

#define MAXSIZE 10

void push();

int pop();

void traverse();

int stack[MAXSIZE];

int Top=-1;

void main()

{

int choice;

char ch;

do

{

clrscr();

printf("\nENTER 1 FOR PUSH: ");

printf("\nENTER 2 TO POP THE ELEMENT: ");

printf("\nENTER 3 TO TRAVERSE THE ELEMENTS: ");

printf("\n ENTER YOUR CHOICE: ");

scanf("%d",&choice);

switch(choice)

{

case 1:push();

break;

case 2:printf("\nTHE DELETED ELEMENT IS %d",pop());

break;

case 3: traverse();

break;

default:printf("\n SORRY!!!!!YOU HAVE ENTERED A WRONG CHOICE");

}

printf("\n DO YOU WISH TO CONTINUE???(Y/N): ");

fflush(stdin);

scanf("%c",&ch);

}

while(ch=='y'|| ch=='Y');

}

Mukesh Kumar 1110751908 CSE 7th Sem

void push()

{

int item;

if(Top==MAXSIZE-1)

{

printf("\n SORRY STACK IS FULL");

getch();

exit();

}

else

{

printf("ENTER THE ELEMENT TO BE INSERTED: ");

scanf("%d",&item);

Top=Top+1;

stack[Top]=item;

}

}

int pop()

{

int item;

if(Top==-1)

{

printf("SORRY!! STACK IS EMPTY");

getch();

exit();

}

else

{

item=stack[Top];

Top=Top-1;

}

return(item);

}

void traverse()

{

int i;

if(Top==-1)

{

printf("OHH!! STACK IS EMPTY");

Mukesh Kumar 1110751908 CSE 7th Sem

getch();

exit();

}

else

{

for(i=Top;i>=0;i--)

{

printf("\nTRAVERSED ELEMENT(S) IS/ARE:");

printf(" %d",stack[i]);

}

}

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-09

Write a program to perform the operations on stack by using linked list.

#include<stdio.h>

#include<conio.h>

struct stack

{

int no;

struct stack *next;

}

*start=NULL;

typedef struct stack st;

void push();

int pop();

void display();

void main()

{

int choice, item;

char ch;

do

{

clrscr();

printf("\nENTER 1 FOR PUSH ITEM: ");

printf("\nENTER 2 TO POP THE ITEM FROM STACK: ");

printf("\nENTER 3 TO DISPLAY THE ITEMS OF STACK: ");

printf("\n ENTER YOUR CHOICE: ");

scanf("%d",&choice);

switch(choice)

{

case 1:push();

break;

case 2: item=pop();

printf("THE DELETED ITEM IS -->%d", item);

break;

case 3: display();

break;

default:printf("\n SORRY!!!!!YOU HAVE ENTERED A WRONG CHOICE");

}

printf("\n DO YOU WISH TO CONTINUE???(Y/N): ");

Mukesh Kumar 1110751908 CSE 7th Sem

fflush(stdin);

scanf("%c",&ch);

}

while(ch=='y'|| ch=='Y');

}

void push()

{

st *node ;

node=(st *)malloc(sizeof(st));

printf("\n ENTER THE NUMBER TO BE INSERTED");

scanf("%d",&node->no);

node->next =start;

start=node;

}

int pop()

{

st *temp;

temp=start;

if(start==NULL){

printf("STACK IS ALREADY EMPTY: ");

getch();

exit();

}

else

{

start=start->next;

free(temp);

}

return(temp->no);

}

void display(){

st *temp;

temp=start;

while(temp->next!=NULL){

printf("\n no=%d", temp->no);

temp=temp->next;

}

printf("\n no=%d",temp->no);

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT

Mukesh Kumar 1110751908 CSE 7th Sem

Program-10

Write a program to perform the operations on file.

#include<stdio.h>

#include<stdlib.h>

int main()

{

char ch,source_file[20],target_file[20];

FILE *source, *target;

printf("Enter the name of file to copy\n ");

gets(source_file);

source=fopen(source_file,"r");

if(source==NULL)

{

printf("Press any key to exit..\n ");

exit(EXIT_FAILURE);

}

printf("Enter the name of target file\n");

gets(target_file);

target=fopen(target_file,"w");

if(target==NULL)

{

fclose(source);

printf("Press any key to exit..\n");

exit(EXIT_FAILURE);

}

while((ch=fgetc(source))!=EOF)

fputc(ch,target);

printf("File copied successfully\n");

fclose(source);

fclose(target);

return 0;

}

Mukesh Kumar 1110751908 CSE 7th Sem

OUTPUT