CD Lab File by Sparsh
CD Lab File by Sparsh
CD Lab File by Sparsh
DESCRIPTION:
A Deterministic finite automaton (DFA) can be seen as a special kind of NFA, in which for
each state and alphabet, the transition function has exactly one state. Thus clearly every
formal language that can be recognized by a DFA can be recognized by an NFA.
PRE EXPERIMENT
ALGORITHM
Step1: Start the program.
Step2: Accept the number of state A and B.
Step3: Find the E-closure for node and name if as A.
Step4: Find v(a,a) and (a,b) and find a state.
Step5: Check whether a number new state is obtained.
Step6: Display all the state corresponding A and B.
Step7: Stop the program.
PROGRAM CODE :-
#include <stdlib.h>
#define STATES 25
#define SYMBOLS 20
char *NFAtab[STATES][SYMBOLS];
int DFAtab[STATES][SYMBOLS];
int i, j;
printf(" \n ");
printf("-----+--");
printf("-----");
1
printf(" ");
printf(" ");
int init_NFA_table()
NFAtab[0][0] = "12";
NFAtab[0][1] = "13";
NFAtab[1][0] = "12";
NFAtab[1][1] = "13";
NFAtab[2][0] = "4";
NFAtab[2][1] = "";
NFAtab[3][0] = "";
NFAtab[3][1] = "4";
NFAtab[4][0] = "4";
NFAtab[4][1] = "4";
NFA_states = 5;
DFA_states = 0;
N_symbols = 2;
2
}
if (*p == *t) {
*r++ = *p++;
} else
*r++ = *t++;
strcpy(s, temp);
int i;
char temp[STATES];
3
for (i = 0; i < strlen(cur_states); i++)
string_merge(temp, nfa[cur_states[i]-'0'][symbol]);
strcpy(nextstates, temp);
int i;
return (*pn)++;
char statename[STATES][STATES];
4
int n = 1; /* number of DFA states */
char nextstate[STATES];
int j;
void main()
clrscr();
init_NFA_table();
getch();
5
OUTPUT : -
6
EXPERIMENT NO - 5
Aim - Implementation of SHIFT REDUCE PARSING ALGORITHM.
INTRODUCTION :-
Algorithm
1. Include the nessary header files.
2. Declare the nessary variables with the operators defined before.
3. Get the input from the user and compare the string for any operators.
4. Find the precedence of the operator in the expression from the predefined
operator.
5. Set the operator with the maximum precedence accordingly and give the
relational operators for them.
6. Parse the given expression with the operators and values.
7. Display the parsed expression.
8. Exit the program.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<process.h>
void main()
{
char stack[50]={"$"},texpr[50],expr[50],optbl[50]={"$"};
char op[6][6][1]={/*+-
id$*//*+*/'>','>','<','<','<','>',/*_*/'<','>','<','<','<','>',/***/'>','>','>','>','<','>',/*/*/'>','>'
,'>','<','<','>',/*id*/'>','>','>','>','>','>',/*$*/'<','<','<','<','<','\0'};
int count=0,index=0,loop=0,idtbl[50],i,j,k;
clrscr();
printf("\n Constrains:");
printf("\n Operator should be{+,-,*,/}\n");
printf("\n operand should be of single digit number");
printf("\n Enter arithmetic Expression:");
strcat(gets(texpr),"$");
strcat(stack,texpr);
for(i=1,j=0;i<strlen(stack);i+=2,j++)
{
idtbl[j]=stack[i];
if(idtbl[j]>57)
{
printf("\n Invalid Expression:");
getch();
exit(0);
}
idtbl[j]=idtbl[j]%48;
optbl[j+1]=stack[i+1];
}
while(optbl[1]!='$'&&strlen(texpr)>2)
{
while(count<strlen(stack))
{
switch(stack[count])
{
case'+':
i=0;
break;
case'-':
i=1;
break;
case'*':
i=2;
break;
case'/':
i=3;
break;
case'$':
i=5;
break;
default:
i=4;
break;
}
switch(stack[count+1])
{
case'+':
j=0;
break;
case'-':
j=1;
break;
case'*':
j=2;
break;
case'/':
j=3;
break;
case'$':
j=5;
break;
default:
j=4;
break;
}
expr[index]=stack[count];
index+=1;
expr[index]=op[i][j][0];
count+=1;
index+=1;
if(i==4&&j==4)
{
printf("Invalid Expression:");
getch();
exit(0);
}
}
expr[index-1]='\0';
printf("\n %s",expr);
for(i=0;i<strlen(expr)&&loop>0;i++)
{
if(expr[i]=='<'&&expr[i+2]=='>')
{
for(j=0;j<strlen(optbl)-1;j++)
{
if(optbl[j+1]==expr[i+1])
{
switch(optbl[j+1])
{
case'+':
idtbl[j]=idtbl[j]+idtbl[j+1];
break;
case'-':
idtbl[j]=idtbl[j]-idtbl[j+1];
break;
case'*':
idtbl[j]=idtbl[j]*idtbl[j];
break;
case'/':
idtbl[j]=idtbl[j]/idtbl[j];
break;
}
for(k=j+1;k<strlen(optbl);k++)
{
optbl[k]=optbl[k+1];
idtbl[k]=idtbl[k+1];
}
optbl[k]='\0';
break;
}
}
}
}
strpcy(stack,optbl);
index=0;count=0;loop=1;
}
if(strlen(texpr)>2)
printf("\n value is %d",idtbl[0]);
else
printf("\n Invalid");
getch();
}
Algorithm
Step 1: Start the program.
Step 2: Get the expression from the user and call the parser() function.
Step 3: In lexer() get the input symbol and match with the look ahead pointer and
then return the token accordingly.
- else return syntax
error.
error.
Step 6:In F(),check whether the look ahead pointer is a member of any identifier.
Step 7:In main(), check if the current look ahead points to the token in a given
the return syntax error.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
char in_sym[15],in_ptr=0;
void edash();
void f();
void t();
void tdash();
void advance();
void e()
{
printf("\n\t\t E->TE'");
t();
edash();
}
void edash()
{
if(in_sym[in_ptr]=='+')
{
printf("\n\t\t E'->+TE'");
advance();
t();
edash();
}
else
printf("\n\t\t E'->e");
}
void t()
{
printf("\n\t\t T->FT'");
f();
tdash();
}
void tdash()
{
if(in_sym[in_ptr]=='*')
{
printf("\n\t\t T'->*FT'");
advance();
f();
tdash();
}
else
printf("\n\t\t T'->e");
}
void f()
{
if((in_sym[in_ptr]=='i')||(in_sym[in_ptr]=='I'))
{
printf("\n\t\t F->id");
advance();
}
else
{
if(in_sym[in_ptr]=='(')
{
printf("\n\t\t F->(E)");
advance();
e();
if(in_sym[in_ptr]==')')
{
advance();
}
}
else
{
printf("\n\t\t Syntax Error");
getch();
exit(1);
}
}
}
void advance()
{
in_ptr++;
}
void main()
{
int i;
clrscr();
printf("\n\t\
printf("\n\t\t E->TE'");
printf("\n\t\t E'->+TE'|e");
printf("\n\t\t T->FT'");
printf("\n\t\t T'->*FT'|e");
printf("\n\t\t F->(E)|id");
printf("\n Enter the Input String (use i for id:)");
gets(in_sym);
printf("\n Seq of production rules");
e();
for(i=0;i<strlen(in_sym);i++)
{
if((in_sym[i]!='+')&&(in_sym[i]!='*')&&
(in_sym[i]!='(')&&(in_sym[i]!=')')&&
(in_sym[i]!='i')&&(in_sym[i]!='I'))
{
printf("\n syntax Error");
break;
}
}
getch();
}
E-
-
T->FT'
T'
F->(E)|id
% &
' &
!
( ! )* + ,
)-
+- !
,- % , )
.- /0
1- 2 ( +
3- /
4- ( , (%&5
6-
7-
PROGRAM CODE :-
#include"stdio.h"
#include"conio.h"
#include"string.h"
int i=1,j=0,no=0,tmpch=90;
char str[100],left[15],right[15];
void findopr();
void explore();
void fleft(int);
void fright(int);
struct exp
int pos;
char op;
}k[15];
void main()
clrscr();
scanf("%s",str);
findopr();
explore();
1
getch();
void findopr()
for(i=0;str[i]!='\0';i++)
if(str[i]==':')
k[j].pos=i;
k[j++].op=':';
for(i=0;str[i]!='\0';i++)
if(str[i]=='/')
k[j].pos=i;
k[j++].op='/';
for(i=0;str[i]!='\0';i++)
if(str[i]=='*')
k[j].pos=i;
k[j++].op='*';
for(i=0;str[i]!='\0';i++)
if(str[i]=='+')
2
{
k[j].pos=i;
k[j++].op='+';
for(i=0;str[i]!='\0';i++)
if(str[i]=='-')
k[j].pos=i;
k[j++].op='-';
void explore()
i=1;
while(k[i].op!='\0')
fleft(k[i].pos);
fright(k[i].pos);
str[k[i].pos]=tmpch--;
printf("\t%c := %s%c%s\t\t",str[k[i].pos],left,k[i].op,right);
for(j=0;j <strlen(str);j++)
if(str[j]!='$')
printf("%c",str[j]);
printf("\n");
3
i++;
fright(-1);
if(no==0)
fleft(strlen(str));
printf("\t%s := %s",right,left);
getch();
exit(0);
printf("\t%s := %c",right,str[k[--i].pos]);
getch();
void fleft(int x)
int w=0,flag=0;
x--;
if(str[x]!='$'&& flag==0)
left[w++]=str[x];
left[w]='\0';
str[x]='$';
4
flag=1;
x--;
void fright(int x)
int w=0,flag=0;
x++;
if(str[x]!='$'&& flag==0)
right[w++]=str[x];
right[w]='\0';
str[x]='$';
flag=1;
x++;
5
OUTPUT:-