Download as pdf or txt
Download as pdf or txt
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 Abhishek Bharti
Asst. Prof. CSE Department Roll No:
2208020109002
Branch/Year
C.S.E/3rd
Department of Computer Science and Engineering

INDEX
S.No Practical’s Name Date Remark
1 WAP to generate Symbol Table. 13/09/23

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


'abb'. 20/09/23

3 WAP to check whether it is Keyword or Valid Identifier.


27/09/23

4 WAP to check the valid comment.


04/10/23

5 WAP to count number of lines and spaces.


11/10/23

6 WAP to check whether the syntax of “IF” statement is


25/10/23
valid or not.

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


01/11/23

8 WAP to find the FOLLOW of Non-Terminal in production.


08/11/23

9 SR Parser for Grammar E->E+E / E/E / E*E / E-E / i


29/11/23

10 WAP for the Recursive Descent Parser for the given


grammar.
P ---> E '#'
E ---> T {('+'|'-') T} 06/12/23
T ---> S {('*'|'/') S}
S ---> F '^' S | F
F ---> char | '(' E ')'
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>
void 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---- SYMBOL TABLE ---- \n");
printf("SYMBOLE \t ADDRESS \t TYPE \n");
while(j<=n){
c=b[j];
if(isalpha(toascii(c)))
{
if(j<=n){
p=malloc(c);
add[x]=p;
d[x]=c;
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
printf("%c \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 %p \t OPERATOR \n",g[x],p);
x++;
}
}
}j++;
}
getch();
}
Output:
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>
void 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')
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
state=6;
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;
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
break;

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

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:
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>
void 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 :: \n");


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");
if(isalpha(a[0])||a[0]=='_')
flag=1;
else
{
printf("\n Invalid identifier");
exit(1);
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
}
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:
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
PROGRAM 4:: WAP to check the valid comment.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void
main ()
{
char com[100];
printf ("\n Enter element\n");
scanf ("%[^;]s", com);
if (com[0] == '/')
{

if (com[1] == '/')
printf ("\n It is a Single Line comment");

else if (com[1] == '*')

{
if (com[(strlen(com)-2)] == '*' && com[(strlen(com)-1)] == '/')

printf ("\n It is a Multi Line comment");

else

printf ("\n It is not a right syntax for Multi-line comment");


}

else
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802

printf ("It is not a right syntax of comment");

else

printf ("\n It is not a right syntax of comment");

getch ();
}
Output:
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>
void main()
{
char str[200],ch;
int a=0,space=0,newline=0;
//clrscr();
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:
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:\n");
printf("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...!!!");if
getch();
}
Output:
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];
void 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');
}
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++;
}
}
}
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
}
return;
}
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:
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);
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);
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
}
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])
follow(a[i][0]);
}
}
}
}
void first(char c){
int k;
if(!(isupper(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;
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
}

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

#include<stdio.h>
#include<conio.h>
#include<string.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();
void 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->i");
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);
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
strcpy(act,"shift ");
temp[0]=ip_sym[ip_ptr];
temp[1]='\0';
strcat(act,temp);
check();
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
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
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);
getch();
exit(0);
}
return;
}
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
Output:
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;

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

void E(void)
{
enter('E');
T();
while (next == '+' || next == '-') {
scan();
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
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');
}
void F(void)
{
enter('F');
if (isalpha(next)) {
scan();
}
else if (next == '(') {
scan();
E();
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
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)
{
spaces(--level);
printf("+-%c: Leave, \t", name);
printf("Next == %c\n", next);
}

void spaces(int local_level)


{
while (local_level-- > 0)
NITRA TECHNICAL CAMPUS, GHAZIABAD
College Code-802
printf("| ");
}
Output:
C:\Dev Ashish\untitled1.exe

You might also like