Practical File: Submitted by Sonali Yadav SCET: 2537 Cse 4 Year
Practical File: Submitted by Sonali Yadav SCET: 2537 Cse 4 Year
Submitted By
Sonali Yadav
SCET: 2537
CSE 4th year
Session 2017-18
PROGRAM -1
C PROGRAM TO DESIGN LEXICAL ANALYSER
#include<string.h>
#include<ctype.h>
#include<stdio.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("stat
ic",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("\nEnter the c program");/*gets(st1);*/
f1=fopen("input","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
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);
printf("\nThe no's in the program are");
for(j=0;j<i;j++)
printf("%d",num[j]);
printf("\n");
f2=fopen("identifier","r");
k=0;
printf("The keywords and identifiersare:");
while((c=getc(f2))!=EOF)
{
if(c!=' ')
str[k++]=c;
else
{
str[k]='\0';
keyword(str);
k=0;
}
}
fclose(f2);
f3=fopen("specialchar","r");
printf("\nSpecial characters are");
while((c=getc(f3))!=EOF)
printf("%c",c);
printf("\n");
fclose(f3);
printf("Total no. of lines are:%d",lineno);
}
Output:
Enter the C program
a+b*c
Ctrl-D
The nos in the program are:
The keywords and identifiers are:
a is an identifier and terminal
b is an identifier and terminal
c is an identifier and terminal
Special characters are:
+*
Total no. of lines are: 1
PROGRAM -2
PROGRAM TO FIND THE NUMBER OF WHITESPACES AND
NEWLINES 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("\n 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 number of lines used : %d",newline+1);
printf("\n the number of spaces used is : %d",space);
getch();
}
Output:
PROGRAM -3
O/P:
PROGRAM -4
PROGRAM TO CALCULATE LEADING FOR ALL THE NON-TERMINALS OF THE
GIVEN GRAMMAR
#include<conio.h>
#include<stdio.h>
char arr[18][3] =
{
{'E','+','F'},{'E','*','F'},{'E','(','F'},{'E',')','F'},{'E','i','F'},{'E','$','F'},
{'F','+','F'},{'F','*','F'},{'F','(','F'},{'F',')','F'},{'F','i','F'},{'F','$','F'},
{'T','+','F'},{'T','*','F'},{'T','(','F'},{'T',')','F'},{'T','i','F'},{'T','$','F'},
};
char prod[6] = "EETTFF";
char res[6][3]=
{
{'E','+','T'},{'T','\0'},
{'T','*','F'},{'F','\0'},
{'(','E',')'},{'i','\0'},
};
char stack [5][2];
int top = -1;
void install(char pro,char re)
{
int i;
for(i=0;i<18;++i)
{
if(arr[i][0]==pro && arr[i][1]==re)
{
arr[i][2] = 'T';
break;
}
}
++top;
stack[top][0]=pro;
stack[top][1]=re;
}
void main()
{
int i=0,j;
char pro,re,pri=' '; clrscr();
for(i=0;i<6;++i)
{
for(j=0;j<3 && res[i][j]!='\0';++j)
{
if(res[i][j]
=='+'||res[i][j]=='*'||res[i][j]=='('||res[i][j]==')'||res[i][j]=='i'||res[i][j]=='$')
{
install(prod[i],res[i][j]);
break;
}
}
}
while(top>=0)
{
pro = stack[top][0];
re = stack[top][1];
--top;
for(i=0;i<6;++i)
{
if(res[i][0]==pro && res[i][0]!=prod[i])
{
install(prod[i],re);
}
}
}
for(i=0;i<18;++i)
{
printf("\n\t");
for(j=0;j<3;++j)
printf("%c\t",arr[i][j]);
}
getch();
clrscr();
printf("\n\n");
for(i=0;i<18;++i)
{
if(pri!=arr[i][0])
{
pri=arr[i][0];
printf("\n\t%c -> ",pri);
}
if(arr[i][2] =='T')
printf("%c ",arr[i][1]);
}
getch();}
Output:
PROGRAM- 5
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("\nEnter the no. of Non-terminals in the grammer:");
scanf("%d",&nont);
printf("\nEnter the Non-terminals in the grammer:\n");
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",¬);
printf("\nEnter the Terminals in the grammer:\n");
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]!='$';)
{
j+=1;
scanf("%c",&p[i][j]);
}
}
for(i=0;i<nont;i++)
{
printf("\nThe productionfor %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();
}
OUTPUT
Enter the no. of Non-terminals in the grammer:3
Enter the Non-terminals in the grammer:
ERT
Enter the no. of Terminals in the grammer: ( Enter e for absiline ) 5
Enter the Terminals in the grammer:
ase*+
Enter the productions :
Enter the production for E ( End the production with '$' sign ) :a+s$
Enter the production for R ( End the production with '$' sign ) :e$
Enter the production for T ( End the production with '$' sign ) :Rs$
The production for E -> a+s
The production for R -> e
The production for T -> Rs
The first of E -> a
The first of R -> e
The first of T -> e s
PRACTICAL-6
#include <stdio.h>
#include<conio.h>
#define MAX 5
int top, status;
/*PUSH FUNCTION*/
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
/*POP FUNCTION*/
int pop (int stack[])
{
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
--top;
}
return ret;
}
/*MAIN PROGRAM*/
void main()
{
int stack [MAX], item;
int ch;
clrscr ();
top = -1;
do
{ do
{ printf ("\NMAIN MENU");
printf ("\n1.PUSH (Insert) in the Stack");
printf ("\n2.POP (Delete) from the Stack");
printf ("\n3.Exit (End the Execution)");
printf ("\nEnter Your Choice: ");
scanf ("%d", &ch);
if (ch<1 || ch>3)
printf ("\nInvalid Choice, Please try again");
}while (ch<1 || ch>3);
switch (ch)
{case 1:
printf ("\nEnter the Element to be pushed : ");
scanf ("%d", &item);
printf (" %d", item);
push (stack, item);
if (status)
{ printf ("\nAfter Pushing ");
display (stack);
if (top == (MAX-1))
printf ("\nThe Stack is Full");
}
else
printf ("\nStack overflow on Push");
break;
case 2:
item = pop (stack);
if (status)
{ printf ("\nThe Popped item is %d. After Popping: ");
display (stack);
}
else
printf ("\nStack underflow on Pop");
break;
default:
printf ("\nEND OF EXECUTION");
}
}while (ch != 3);
getch();
}
PRACTICAL-7
default:
printf("Invalid choice. Please try again.\n");
}
} while(1);
getch();
}
void push()
{
struct node *ptr;
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if (top == NULL)
{
top = (struct node *)malloc(sizeof(struct node));
top->info = item;
top->link = NULL;
}
else
{
ptr = top;
top = (struct node *)malloc(sizeof(struct node));
top->info = item;
top->link = ptr;
}
printf("\nItem inserted: %d\n", item);
}
void pop()
{
struct node *ptr;
if (top == NULL)
printf("\n\nStack is empty\n");
else
{
ptr = top;
item = top->info;
top = top->link;
free(ptr);
printf("\n\nItem deleted: %d", item);
}
}
void display()
{
struct node *ptr;
if (top == NULL)
printf("\n\nStack is empty\n");
else
{
ptr = top;
while(ptr != NULL)
{
printf("\n\n%d", ptr->info);
ptr = ptr->link;
}
}
}
Output:
PRACTICAL-8
#include<stdio.h>
#include<conio.h>
int isiden(char*);
int second(char*);
int third();
void main()
{
char *str;
int i = -1;
clrscr();
printf("\n\n\t\tEnter the desired String: ");
do
{
++i;
str[i] = getch(); if(str[i]!=10 && str[i]!=13) printf("%c",str[i]);
if(str[i] == '\b') {
--i;
printf(" \b");
}
}while(str[i] != 10 && str[i] != 13);
if(isident(str))
printf("\n\n\t\tThe given strig is an identifier"); else
printf("\n\n\t\tThe given string is not an identifier"); getch();
}
//To Check whether the given string is identifier or not
//This function acts like first stage of dfa
int isident(char *str)
{
if((str[0]>='a' && str[0]<='z') || (str[0]>='A' && str[0]<='Z'))
{
return(second(str+1));
}
else
return 0;
}
//This function acts as second stage of dfa
int second(char *str)
{
if((str[0]>='0' && str[0]<='9') || (str[0]>='a' && str[0]<='z') || (str[0]>='A' && str[0]<='Z'))
{
return(second(str+1)); //Implementing the loop from second stage to
second stage
}
else
{
if(str[0] == 10 || str[0] == 13) {
return(third(str));
}
else
{
return 0;
}
}
}
//This function acts as third stage of dfa
int third()
{
return 1; //Being final stage reaching it mean the string is identified
}
OUTPUT:
Enter the desired String: a123
The given strig is an identifier
______________________________________________________________________________
Enter the desired String: shailesh
The given strig is an identifier
______________________________________________________________________________
Enter the desired String: 1asd
The given string is not an identifier
______________________________________________________________________________
Enter the desired String: as-*l
The given string is not an identifier
PRACTICAL-9
OUTPUT
enter the string :return
it is a keyword
enter the string :hello
it is not a keyword
PRACTICAL-10
OUTPUT:
Input a string :23
It is a Constant
Input a string :a_123
It is not a Constant