CD Lab File by Sparsh

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

1

AIM: To write a C program to construct a DFA from the given NFA.

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

Q1. Differentiate between NFA and DFA


Q2. What do you mean by Transition Table?
Q3. What is Finite Automata?

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>

using namespace std;

#define STATES 25

#define SYMBOLS 20

int N_symbols; /* number of input symbols */

int NFA_states; /* number of NFA states */

char *NFAtab[STATES][SYMBOLS];

int DFA_states; /* number of DFA states */

int DFAtab[STATES][SYMBOLS];

void put_dfa_table(int tab[][SYMBOLS], int nstates,int nsymbols) /* number of input symbols */

int i, j;

puts("STATE TRANSITION TABLE");

printf(" \n ");

for( int i = 0; i< nsymbols; i++)

printf(" %c ", '0' + i);

printf("-----+--");

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

printf("-----");

1
printf(" ");

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

printf(" %c | ", 'A'+i); /* state */

for (j = 0; j < nsymbols; j++)

printf(" %c ", 'A'+tab[i][j]);

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
}

void string_merge(char *s, char *t)

char temp[STATES], *r=temp, *p=s;

while (*p && *t) {

if (*p == *t) {

*r++ = *p++; t++;

} else if (*p < *t) {

*r++ = *p++;

} else

*r++ = *t++;

if (*p) strcat(r, p);

else if (*t) strcat(r, t);

strcpy(s, temp);

void get_next_state(char *nextstates, char *cur_states,

char *nfa[STATES][SYMBOLS], int n_nfa, int symbol)

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 state_index(char *state, char statename[][STATES], int *pn)

int i;

if (!*state) return -1; /* no next state */

for (i = 0; i < *pn; i++)

if (!strcmp(state, statename[i])) return i;

strcpy(statename[i], state); /* new state-name */

return (*pn)++;

int nfa_to_dfa(char *nfa[STATES][SYMBOLS], int n_nfa,

int n_sym, int dfa[][SYMBOLS])

char statename[STATES][STATES];

int i = 0; /* current index of DFA */

4
int n = 1; /* number of DFA states */

char nextstate[STATES];

int j;

strcpy(statename[0], "0"); /* start state */

for (i = 0; i < n; i++) { /* for each DFA state */

for (j = 0; j < n_sym; j++) { /* for each input symbol */

get_next_state(nextstate, statename[i], nfa, n_nfa, j);

dfa[i][j] = state_index(nextstate, statename, &n);

return n; /* number of DFA states */

void main()

clrscr();

init_NFA_table();

DFA_states = nfa_to_dfa(NFAtab, NFA_states, N_symbols, DFAtab);

put_dfa_table(DFAtab, DFA_states, N_symbols);

getch();

5
OUTPUT : -

6
EXPERIMENT NO - 5
Aim - Implementation of SHIFT REDUCE PARSING ALGORITHM.

INTRODUCTION :-

Shift Reduce parser attempts for the construction of parse in a similar


manner as done in bottom up parsing i.e. the parse tree is constructed
from leaves(bottom) to the root(up). A more general form of shift
reduce parser is LR parser.
This parser requires some data structures i.e.
A input buffer for storing the input string.
A stack for storing and accessing the production rules.
OPERATION:-
Shift:
This involves moving of symbols from input buffer onto the stack.
Reduce:
If the handle appears on top of the stack then, its reduction by
using appropriate production rule is done i.e. RHS of production
rule is popped out of stack and LHS of production rule is pushed
onto the stack.
Accept:
If only start symbol is present in the stack and the input buffer
is empty then, the parsing action is called accept. When accept
action is obtained, it is means successful parsing is done.
Error:
This is the situation in which the parser can neither perform
shift action nor reduce action and not even accept action.
Consider the following grammar-
S→TL
T → int | float
L → L , id | id
Parse the input string int id , id ; using a shift-reduce parser.
Table :
StackInput BufferParsing Action
$ int id ,id $ Shift
$ int id,id $ Reduce T → int
$T id,id $ Shift
$ T id id $ Reduce L → id
$TL id$ Shift
$ T L, id $ Reduce L → L,id
$TL $ Shift
$TL $ Reduce S → T L
$S $ Accept
IMPLEMENTATION OF OPERATOR PRECEDENCE PARSER

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

put String (use i for id): ( i + i ) * i


Seq of production rules
E->TE'
T->FT'
F-
E->TE'
T->FT'
F->id
T'->e
E'->+TE'
T->FT'
F->id
T'->e
E'->e
T'->*FT'
F->id
T'->e
E'->e
!
" #
$ $

% &
' &

!
( ! )* + ,

)-
+- !
,- % , )
.- /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();

printf("\t\tINTERMEDIATE CODE GENERATION\n\n");

printf("Enter the Expression :");

scanf("%s",str);

printf("The intermediate code:\t\tExpression\n");

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--;

while(x!= -1 &&str[x]!= '+' &&str[x]!='*'&&str[x]!='='&&str[x]!='\0'&&str[x]!='-'&&str[x]!


='/'&&str[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++;

while(x!= -1 && str[x]!= '+'&&str[x]!='*'&&str[x]!='\0'&&str[x]!='='&&str[x]!=':'&&str[x]!


='-'&&str[x]!='/')

if(str[x]!='$'&& flag==0)

right[w++]=str[x];

right[w]='\0';

str[x]='$';

flag=1;

x++;

5
OUTPUT:-

You might also like