You are on page 1of 27

NITRA TECHNICAL CAMPUS, GHAZIABAD

College Code-802

Department of Computer Science and Engineering


SESSION 2023-24

Year: 3rd Semester: 5th

Compiler Design Lab (KCS-552)


LAB FILE

Submitted to: Submitted by:


NITIN KUMAR SHARMA Aadarsh Pandey
Asst. Prof. CSE Department 2108020100001
CSE/3rd Year

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802

Department of Computer Science and Engineering


INDEX
S. No. Practical’s Name Date Remark
WAP to generate Symbol Table. 13/09/202
01. 3
Write a C program to recognize strings under 'a', 'a*b+', 20/09/202
02. 'abb'. 3
WAP to check whether it is Keyword or Valid Identifier. 27/09/202
03. 3
WAP to check the valid comment. 04/10/202
04. 3
WAP to count number of lines and spaces. 11/10/202
05. 3
WAP to check whether the syntax of “IF” statement is 25/10/202
06. valid or not. 3
WAP to find the FIRST of Non-Terminal in production. 01/11/202
07. 3
WAP to find the FOLLOW of Non-Terminal in 08/11/202
08. production. 3
SR Parser for Grammar E->E+E / E/E / E*E / E-E / id 29/11/202
09. 3
WAP for the Recursive Descent Parser for the given
grammar.
P ---> E '#'
06/12/202
10. E ---> T {('+'|'-') T}
3
T ---> S {('*'|'/') S}
S ---> F '^' S | F
F ---> char | '(' E ')'

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802

PROGRAM 1: WAP to generate Symbol Table.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#include<ctype.h>

int main() {
int i=0,j=0,x=0,flag=0,n;
void *p, *add[15];
char c, ch, srch, b[15],d[15],g[10];

printf("Expression Terminated by '$' :: \n");


while((c=getchar())!='$') {
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression :: ");
i=0;
while(i<=n) {
printf("%c",b[i]);
i++;
}
printf("\n\n\t---- SYMBOL TABLE ----\n");
printf("SYMBOLE \t ADDRESS \t TYPE \n");
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
while(j<=n) {
c=b[j];
if(isalpha(toascii(c))) {
if(j<=n) {
p=malloc(c);
add[x]=p;
d[x]=c;
printf("%c \t\t %d \t IDENTIFIER \n",c,p);
goto b;
} else {
b:
ch=b[j+1];
if(ch=='+' || ch=='-' || ch=='*' || ch=='/' || ch=='=' ) {
p=malloc(c);
add[x]=p;
g[x]=ch;
printf("%c \t\t %p \t OPERATOR \
n",g[x],p);
x++;
}
}
}
j++;
}
getch();
}

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802

PROGRAM 2: Write a C program to recognize strings under 'a', 'a*b+',


'abb'.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main() {

char s[50],c;
int state=0,i=0;

printf("\n Enter a string:");


scanf("%s", s);

while(s[i]!='\0') {

switch(state) {

case 0:
c=s[i++];
if(c=='a') state=1;
else if(c=='b') state=2;
else state=6;
break;

case 1:
c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;

case 2:
c=s[i++];
if(c=='a')
state=6;
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
else if(c=='b')
state=2;
else
state=6;
break;

case 3:
c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;

case 4:
c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else
state=6;
break;

case 5:
c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;

case 6:
printf("\n %s is not recognised.",s);
exit(0);
}
}

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}

Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 3: WAP to check whether it is Keyword or Valid Identifier.

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

int main() {
int flag,i=1,m;
char a[10], s[32][10]=
{"if","else","goto","continue","return","auto","double",
"int","struct", "break","long","switch","case","enum",
"register","typedef","char","extern","union","const",
"float","short","unsigned","sizeof","volatile","for",
"signed","void","default","do","static","while"
};

printf("\n Enter an String to find that it is an Identifier or Keyword ::


");
scanf("%s",a);
for(i=0; i<32; i++) {
m=strcmp(a,s[i]);
if(m==0)
flag=1;
}
if(flag==0) {
printf("\n It is not a keyword");
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
if(isalpha(a[0])||a[0]=='_')
flag=1;
else {
printf("\n Invalid identifier");
exit(1);
}
while(a[i]!='\0') {
if(!isdigit(a[i])&& !isalpha(a[i])|| a[i]=='_') {
flag=1;
break;
}
i++;
}
if(flag==1)
printf("\n Valid identifier");

} else
printf("\n It is a keyword, So it can't be an identifier");
getch();
}

Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 4: WAP to check the valid comment.

#include <stdio.h>
#include <conio.h>
struct process
{
int pid;
int btime;
int wtime;
int ttime;
} p[10];

int main() {
int i,j,k,n,ttur,twat;
float awat,atur;

printf("Enter no. of process : ");


scanf("%d", &n);

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


printf("Burst time for process P%d (in ms) : ",(i+1));
scanf("%d", &p[i].btime);
p[i].pid = i+1;
}

p[0].wtime = 0;

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


p[i+1].wtime = p[i].wtime + p[i].btime;
p[i].ttime = p[i].wtime + p[i].btime;
}

ttur = twat = 0;

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


ttur += p[i].ttime;
twat += p[i].wtime;
}

awat = (float)twat / n;
atur = (float)ttur / n;

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
printf("\n FCFS Scheduling\n\n");
for(i=0; i<28; i++)
printf("-");

printf("\nProcess B-Time T-Time W-Time\n");

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


printf("-");

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


printf("\n P%d\t%4d\t%3d\t%2d",
p[i].pid,p[i].btime,p[i].ttime,p[i].wtime);
printf("\n");

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


printf("-");

printf("\n\nGANTT Chart\n");
printf("-");

for(i=0; i<(p[n-1].ttime + 2*n); i++) printf("-");


printf("\n");
printf("|");

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


k = p[i].btime/2;
for(j=0; j<k; j++) printf(" ");
printf("P%d",p[i].pid);
for(j=k+1; j<p[i].btime; j++) printf(" ");
printf("|");
}

printf("\n");
printf("-");

for(i=0; i<(p[n-1].ttime + 2*n); i++)


printf("-");

printf("\n");
printf("0");

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


for(j=0; j<p[i].btime; j++)
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
printf(" ");
printf("%2d",p[i].ttime);
}

printf("\n\nAverage waiting time : %f ms", awat);


printf("\nAverage turn around time : %f ms\n", atur);

getch();
return 0;
}

Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 5: WAP to count number of lines and spaces.

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main() {
char str[200],ch;
int a=0,space=0,newline=0;

printf("\nEnter a string.(Press escape to quit entering)\n");


ch=getchar();
while((ch!=27)&&(a<199)) {
str[a]=ch;
if(str[a]==' ')
space++;
if(str[a]==10)
newline++;
a++;
ch=getchar();
}
printf("\nthe number of lines used is %d",newline+1);
printf("\nthe number of spaces used is %d",space);
}

Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 6: WAP to check whether the syntax of “IF” statement is valid or
not.

#include<stdio.h>
#include<conio.h>
#include<string.h>

int main() {
char ch[80];
int i=0, length, ter=0;
printf("Enter the if statement syntax (Press ESC to terminate your
expression) : \n");

while(ter!=27) {
ch[i]=getchar();
ter=(int)ch[i];
i++;
}
length = i;
if(ch[0]=='i'&&ch[1]=='f'&&ch[2]=='('&&ch[length-2]=='}'||
ch[length-3]=='}') {
for(i=3; i<length-1; i++) {
if(ch[i]==')'&&ch[i+1]=='{'||ch[i+2]=='{' || ch[i+3]=='{')
{
i=1000;
break; }}}
if(i==1000)
printf("\n\tYour if statement syntax is correct\n");
else
printf("\nWrong Syntax...!!!");
getch();
}

Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802

PROGRAM 7: WAP to find the FIRST of Non-Terminal in production.

#include<stdio.h>
#include<ctype.h>

void FIRST(char[],char );
void resultSet(char[],char);
int nop;
char proSet[10][10];
int main() {
int i;
char choice;
char c;
char result[20];
printf("Enter the Number of Production in Grammar ::");
scanf(" %d",&nop);
printf("\nEnter productions in form of S=A+B and for Epsilon A=$ \
n");
for(i=0; i<nop; i++) {
printf("Enter productions Number %d : ",i+1);
scanf(" %s",proSet[i]);
}
do {
printf("\n Find the FIRST of :");
scanf(" %c",&c);
FIRST(result,c);
printf("\n FIRST(%c)= { ",c);
for(i=0; result[i]!='\0'; i++)
printf(" %c ",result[i]);
printf("}\n");
printf("press 'y' to continue : ");
scanf(" %c",&choice);
} while(choice=='y'||choice =='Y');
}
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
void FIRST(char* Result,char c) {
int i,j,k;
char subResult[20];
int foundEpsilon;
subResult[0]='\0';
Result[0]='\0';

if(!(isupper(c))) {
resultSet(Result,c);
return ;
}

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

if(proSet[i][0]==c) {

if(proSet[i][2]=='$')
resultSet(Result,'$');
else {
j=2;
while(proSet[i][j]!='\0') {
foundEpsilon=0;
FIRST(subResult,proSet[i][j]);
for(k=0; subResult[k]!='\0'; k++)
resultSet(Result,subResult[k]);
for(k=0; subResult[k]!='\0'; k++)
if(subResult[k]=='$') {
foundEpsilon=1;
break;
}

if(!foundEpsilon)
break;
j++;
}
}
}
}

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
void resultSet(char Result[],char val) {
int k;
for(k=0 ; Result[k]!='\0'; k++)
if(Result[k]==val)
return;
Result[k]=val;
Result[k+1]='\0';
}

Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 8: WAP to find the FOLLOW of Non-Terminal in production.

#include<stdio.h>
#include<string.h>
#include<ctype.h>

int n,m=0,p,i=0,j=0;
char a[10][10],followResult[10];
void follow(char c);
void first(char c);
void addToResult(char);
int main() {
int i;
int choice;
char c,ch;
printf("Enter the no.of productions: ");
scanf("%d", &n);
printf(" Enter %d productions\nProduction with multiple terms should
be give as separate productions \n", n);
for(i=0; i<n; i++)
scanf("%s%c",a[i],&ch);
// gets(a[i]);
do {
m=0;
printf("Find FOLLOW of -->");
scanf(" %c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0; i<m; i++)
printf("%c ",followResult[i]);
printf(" }\n");
printf("Do you want to continue(Press 1 to continue....)?");
scanf("%d%c",&choice,&ch);
} while(choice==1);
}
void follow(char c) {
if(a[0][0]==c)addToResult('$');
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])
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
follow(a[i][0]);
}
}
}
}
void first(char c) {
int k;
if(!(isupper(c)))
//f[m++]=c;
addToResult(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];
addToResult(a[k][2]);
else first(a[k][2]);
}
}
}
void addToResult(char c) {
int i;
for( i=0; i<=m; i++)
if(followResult[i]==c)
return;
followResult[m++]=c;
}
Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 9: SR Parser for Grammar E->E+E / E/E / E*E / E-E / id

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>

char ip_sym[15],stack[15];
int ip_ptr=0,st_ptr=0,len,i;
char temp[2],temp2[2];
char act[15];
void check();

int main() {

printf("\n\t\t SHIFT REDUCE PARSER\n");


printf("\n GRAMMER\n");
printf("\n E->E+E\n E->E/E");
printf("\n E->E*E\n E->E-E\n E->id");
printf("\n enter the input symbol:\t");
gets(ip_sym);
printf("\n\t stack implementation table");
printf("\n stack\t\t input symbol\t\t action");
printf("\n______\t\t ____________\t\t ______\n");
printf("\n $\t\t%s$\t\t\t--",ip_sym);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
len=strlen(ip_sym);
for(i=0; i<=len-1; i++) {
stack[st_ptr]=ip_sym[ip_ptr];
stack[st_ptr+1]='\0';
ip_sym[ip_ptr]=' ';
ip_ptr++;
printf("\n $%s\t\t%s$\t\t\t%s",stack,ip_sym,act);
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
st_ptr++;
}
st_ptr++;
check();
}
void check() {
int flag=0;
temp2[0]=stack[st_ptr];
temp2[1]='\0';
if(islower(temp2[0])) {
stack[st_ptr]='E';
flag=1;
}
if((!strcmp(temp2,"+"))||(!strcmp(temp2,"*"))
||(!strcmp(temp2,"/"))||(!strcmp(temp2,"-"))) {
flag=1;
}
if((!strcmp(stack,"E+E"))||(!strcmp(stack,"E/E"))
||(!strcmp(stack,"E*E"))||(!strcmp(stack,"E-E"))) {
if(!strcmp(stack,"E+E")) {
strcpy(stack,"E");
printf("\n $%s\t\t%s$\t\t\tE->E+E",stack,ip_sym);
} else if(!strcmp(stack,"E/E")) {
strcpy(stack,"E");
printf("\n $%s\t\t %s$\t\t\tE->E/E",stack,ip_sym);
} else if(!strcmp(stack,"E-E")) {
strcpy(stack,"E");
printf("\n $%s\t\t %s$\t\t\tE->E-E",stack,ip_sym);
} else {
strcpy(stack,"E");
printf("\n $%s\t\t%s$\t\t\tE->E*E",stack,ip_sym);
}
flag=1;
st_ptr=0;
}
if(!strcmp(stack,"E")&&ip_ptr==len) {
printf("\n $%s\t\t%s$\t\t\tACCEPT",stack,ip_sym);
getch();
exit(0);
}
if(flag==0) {
printf("\n $%s\t\t\t%s\t\t reject",stack,ip_sym);
Roll No: 2108020100001
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
getch();
exit(0);
}
return;
}

Output:

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 10 : WAP for the Recursive Descent Parser for the given
grammar.
P ---> E '#'
E ---> T {('+'|'-') T}
T ---> S {('*'|'/') S}
S ---> F '^' S | F
F ---> char | '(' E ')'

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);
void T(void);
void S(void);
void F(void);
void error(int);
void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;

int main() {
printf("Enter the string:: ");
scan();
E();
if (next != '#') error(1);
else printf("Successful parse\n");
}

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
void E(void) {
enter('E');
T();
while (next == '+' || next == '-') {
scan();
T();
}
leave('E');
}

void T(void) {
enter('T');
S();
while (next == '*' || next == '/') {
scan();
S();
}
leave('T');
}
void S(void) {
enter('S');
F();
if (next == '^') {
scan();
S();
}
leave('S');
}

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
void F(void) {
enter('F');
if (isalpha(next)) {
scan();
} else if (next == '(') {
scan();
E();
if (next == ')') scan();
else error(2);
} else {
error(3);
}
leave('F');
}
void scan(void) {
while (isspace(next = getchar()))
;
}
void error(int n) {
printf("\n*** ERROR: %i\n", n);
exit(1);
}
void enter(char name) {
spaces(level++);
printf("+-%c: Enter, \t", name);
printf("Next == %c\n", next);
}
void leave(char name) {

Roll No: 2108020100001


NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
spaces(--level);
printf("+-%c: Leave, \t", name);
printf("Next == %c\n", next);
}

void spaces(int local_level) {


while (local_level-- > 0)
printf("| ");
}

Output:

Roll No: 2108020100001

You might also like