CD Lab Manual
CD Lab Manual
LAB MANUAL
(A412507)
B.TECH: II YEAR – II
SEMESTER (2023-2024)
Mission
• Our Mission is to create and sustain a community of learning in which students acquire
knowledge and learn to apply it professionally with a concern for the society.
• Pursue and Disseminate Research Findings and Offer Knowledge-BasedTechnological
Services to Satisfy the Needs of Society and the Industry.
• Promote Professional Ethics, Leadership Qualities and Social Responsibilities.
• To offer high quality graduate and post graduate programs in computer science education
and to prepare students for professional career and/or higher studies globally.
• To develop self learning abilities and professional ethics to serve the society.
REFERENCE BOOKS:
1. Lex & YACC – John R. Levine, Tony Mason, Doug Brown, O’Reilly
2. Compiler Construction, Louden, Thomson
B Program to recognize a valid variable which starts with a letter followed by any
number of letters or digits.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct table
{ char var[10]; int value;
};
struct table tbl[20];
int i,j,n;
void create();
void modify();
int search(char variable[],int n);
void insert();
void display();
void main()
{ int ch,result=0; char v[10]; do
{ printf("Enter your choice\n1.Create\n2.Insert\
n3.Modify\n4.Search\n5.Display\n6.Exit:");
scanf("%d",&ch); switch(ch)
CMR College of Engineering & Technology
10
{
case 1:
create();
break; case 2:
insert();
break; case 3:
modify();
break; case 4:
printf("Enter the variable to be searched for:\n");
scanf("%s",&v); result=search(v,n);
if(result==0)
printf("The variable does not belong to the table\
n");
else
printf("The location of the variable is %d\nThe
value of %s is%d\
n",result,tbl[result].var,tbl[result].value);
break; case 5:
display();
break; case 6:
exit(1);
}
}
while(ch!=6); getch();
}
void create()
{
printf("Enter the no. of entries:");
scanf("%d",&n);
printf("Enter the variable and the value:\n");
for(i=1;i<=n;i++)
{ scanf("%s%d",tbl[i].var,&tbl[i].value); check:
if(tbl[i].var[0]>='0'&&tbl[i].var[0]<='9')
{ printf("The variable should start with an alphabet\nEnter
correct variable name:\n"); scanf("%s
%d",tbl[i].var,&tbl[i].value); goto check;
} check1:
for(j=1;j<i;j++)
{ if(strcmp(tbl[i].var,tbl[j].var)==0)
{ printf("The variable already exists.Enter another
CMR College of Engineering & Technology
11
variable\n"); scanf("%s%d",tbl[i].var,&tbl[i].value); goto
check1;
}
}
}
printf("The table after creation is:\n"); display();
}
void insert()
{ if(i>=20) printf("Cannot insert.Table is full\n"); else {
n++;
printf("Enter the value and variable\n"); scanf("%s
%d",tbl[n].var,&tbl[n].value); check:
if(tbl[i].var[0]>='0'&&tbl[i].var[0]<='9')
{ printf("The variable should start with an alphabet\nEnter
correct variable name:\n"); scanf("%s
%d",tbl[i].var,&tbl[i].value); goto check;
} check1: for(j=1;j<n;j++)
{ if(strcmp(tbl[j].var,tbl[i].var)==0)
{ printf("The variable already exist.Enter another
variable\n"); scanf("%s%d",tbl[i].var,&tbl[i].value); goto
check1;
}
}
printf("The table after insertion is:\n"); display();
}
}
void modify()
{ char variable[10]; int result=0;
printf("Enter the variable to be modified\n");
scanf("%s",&variable); result=search(variable,n);
if(result==0) printf("%s does not belong to table\
n",variable); else
{ printf("The current value of the variable %s is %d\
nEnter the new variable and its value\
n",tbl[result].var,tbl[result].value); scanf("%s
%d",tbl[result].var,&tbl[result].value); check:
if(tbl[i].var[0]>='0'&&tbl[i].var[0]<='9')
{ printf("The variable should start with an alphabet\nEnter
correct variable name:\n"); scanf("%s
%d",tbl[i].var,&tbl[i].value); goto check;
OUTPUT
VARIABLE VALUE
AIM 45
RIM 40
BALL 56
SIM 25
Enter your choice
1.Create
2.Insert
3.Modify
4.Search
5.Display
6.Exit:6
RESULT:
Thus the C program to implement Symbol Table was executed and verified successfully.
3. If the character is a slash then call skipcomment() for skip the comment statements in the file.
4. If the first string matches with any delimiters operator then print as a delimiter.
5. If the first string matches with any header then print that string as a header file.
6. If the first string matches with any operator then print that as an operator.
7. If the first string matches with any keywords then print that string as a keyword.
8. If the string is not a keyword then print that as an identifier.
PROGRAM:
{
token[j]=c; //Combining the
characters to get the
token
j++;
} if(f==0) f=1;
}
else if(isalnum(c))
{ if(numflag==0) numflag=1;
token[j]=c; //combining the characters to get the token j++; }
else
{
if(isdelim(c)) //Checking for delimiters.
{
if(numflag==1)
{
token[j]=‘\0’; check(token); numflag=0;
CMR College of Engineering & Technology
17
} if(f==1) { token[j]=‘\0’;
numflag=0; check(token);
} j=0; f=0;
printf(“\nDelimitter\t %c”,c);
} else if(isop(c)) //Checking for operators
{ if(numflag==1)
{ token[j]=‘\0’; check(token);
numflag=0; j=0; f=0;
}
if(f==1) { token[j]=‘\0’;
j=0; f=0; numflag=0; check(token);
} if(fop==1)
{ fop=0;
printf(“\nOperator\t %c%c”,c,sop); //In case the operator is like ‘++’or ‘--
} else printf(“\nOperator\t %c”,c); //In other
cases.
}
else if(c==‘.’)
{ token[j]=c; j++;
}
}
}
}
int isdelim(char c) //Function to check if the character retrieved from the file is a delimiter.
{ int i; for(i=0;i<14;i++)
{ if(c==delim[i]) return 1;
} return 0;
}
int isop(char c) //Function to check if the character retrieved from the file is an operator.
{ int i,j; char ch; for(i=0;i<7;i++)
{ if(c==oper[i])
{ ch=getc(fp); for(j=0;j<6;j++) //In case the operator is like
‘++’ or ‘--’, etc.
{ if(ch==oper[j])
{ fop=1; sop=ch;
return 1;
} } ungetc(ch,fp); return 1;
} } return 0;
} void check(char t[]) //Function to check if the token is an identifier, keyword, header file name
{ int i; if(numflag==1)
{ printf(“\nNumber\t\t %s”,t); return; } for(i=0;i<2;i++)
{ if(strcmp(t,predirect[i])==0)
{
printf(“\nPreprocessor directive %s”,t); return;
} } for(i=0;i<6;i++)
{
if(strcmp(t,header[i])==0)
{
void skipcomment() //Function to skip over the comment statements in the file.
{ ch=getc(fp);
if(ch==‘/’) //Checking single line comments
{
while((ch=getc(fp))!=‘\n’);
} else if(ch==‘*’) //Checking multiple line comments.
{ while(f==0) { ch=getc(fp); if(ch==‘*’)
{ c=getc(fp); if(c==‘/’) f=1;
} } f=0; }
}