Skip to content

Commit e1079ff

Browse files
committed
55
1 parent 7c7d3f6 commit e1079ff

File tree

7 files changed

+82
-94
lines changed
  • cd
    • 11. Construct a Shift Reduce Parser for a given language.
    • 5. Write program to convert NFA with ε transition to NFA without ε transition
  • daa
    • 4. Program for Selection Sort.
    • 6. Program for Quick Sort.

7 files changed

+82
-94
lines changed

cd.c

Whitespace-only changes.
Binary file not shown.
Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
1-
// 1. Construct a Shift Reduce Parser for a given language
21

3-
// Including Libraries
2+
3+
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <string.h>
77

8-
// Global Variables
98
int z = 0, i = 0, j = 0, c = 0;
10-
11-
// Modify array size to increase
12-
// length of string to be parsed
139
char a[16], ac[20], stk[15], act[10];
14-
15-
// This Function will check whether
16-
// the stack contain a production rule
17-
// which is to be Reduce.
18-
// Rules can be E->2E2 , E->3E3 , E->4
1910
void check()
2011
{
21-
// Copying string to be printed as action
12+
2213
strcpy(ac, "REDUCE TO E -> ");
2314

24-
// c=length of input string
15+
2516
for (z = 0; z < c; z++)
2617
{
27-
// checking for producing rule E->4
18+
2819
if (stk[z] == '4')
2920
{
3021
printf("%s4", ac);
3122
stk[z] = 'E';
3223
stk[z + 1] = '\0';
3324

34-
// printing action
25+
3526
printf("\n$%s\t%s$\t", stk, a);
3627
}
3728
}
3829

3930
for (z = 0; z < c - 2; z++)
4031
{
41-
// checking for another production
32+
4233
if (stk[z] == '2' && stk[z + 1] == 'E' &&
4334
stk[z + 2] == '2')
4435
{
@@ -53,7 +44,7 @@ void check()
5344

5445
for (z = 0; z < c - 2; z++)
5546
{
56-
// checking for E->3E3
47+
5748
if (stk[z] == '3' && stk[z + 1] == 'E' &&
5849
stk[z + 2] == '3')
5950
{
@@ -65,64 +56,61 @@ void check()
6556
i = i - 2;
6657
}
6758
}
68-
return; // return to main
59+
return;
6960
}
7061

71-
// Driver Function
62+
7263
int main()
7364
{
7465
printf("GRAMMAR is -\nE->2E2 \nE->3E3 \nE->4\n");
7566

76-
// a is input string
77-
strcpy(a, "32423");
7867

79-
// strlen(a) will return the length of a to c
68+
strcpy(a, "23");
69+
70+
8071
c = strlen(a);
8172

82-
// "SHIFT" is copied to act to be printed
73+
8374
strcpy(act, "SHIFT");
8475

85-
// This will print Labels (column name)
76+
8677
printf("\nstack \t input \t action");
8778

88-
// This will print the initial
89-
// values of stack and input
79+
80+
9081
printf("\n$\t%s$\t", a);
9182

92-
// This will Run upto length of input string
83+
9384
for (i = 0; j < c; i++, j++)
9485
{
95-
// Printing action
86+
9687
printf("%s", act);
9788

98-
// Pushing into stack
89+
9990
stk[i] = a[j];
10091
stk[i + 1] = '\0';
10192

102-
// Moving the pointer
93+
10394
a[j] = ' ';
10495

105-
// Printing action
96+
10697
printf("\n$%s\t%s$\t", stk, a);
10798

108-
// Call check function ..which will
109-
// check the stack whether its contain
110-
// any production or not
99+
100+
101+
111102
check();
112103
}
113104

114-
// Rechecking last time if contain
115-
// any valid production then it will
116-
// replace otherwise invalid
105+
106+
107+
117108
check();
118109

119-
// if top of the stack is E(starting symbol)
120-
// then it will accept the input
110+
111+
121112
if (stk[0] == 'E' && stk[1] == '\0')
122113
printf("Accept\n");
123-
else // else reject
114+
else
124115
printf("Reject\n");
125116
}
126-
// This code is contributed by Ritesh Aggarwal
127-
128-
// https://www.geeksforgeeks.org/shift-reduce-parser-compiler/

cd/5. Write program to convert NFA with ε transition to NFA without ε transition/program.c

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ struct node
66
struct node *link;
77
};
88

9-
void findclosure(int, int);
10-
void insert_trantbl(int, char, int);
11-
int findalpha(char);
12-
void findfinalstate(void);
13-
void unionclosure(int);
14-
void print_e_closure(int);
15-
static int set[20], nostate, noalpha, s, notransition, nofinal, start, finalstate[20], c, r, buffer[20];
9+
void fc(int, int);
10+
void it(int, char, int);
11+
int fa(char);
12+
void ffs(void);
13+
void uc(int);
14+
void pec(int);
15+
static int set[20], ns, na, s, nt, nf, start, fs[20], c, r, b[20];
1616
char alphabet[20];
1717
static int e_closure[20][20] = {0};
1818
struct node *transition[20][20] = {NULL};
@@ -22,72 +22,72 @@ void main()
2222

2323
struct node *temp;
2424
printf("enter the number of alphabets?\n");
25-
scanf("%d", &noalpha);
25+
scanf("%d", &na);
2626
getchar();
2727
printf("NOTE:- [ use letter e as epsilon]\n");
2828

2929
printf("NOTE:- [e must be last character ,if it is present]\n");
3030

3131
printf("\nEnter alphabets?\n");
32-
for (i = 0; i < noalpha; i++)
32+
for (i = 0; i < na; i++)
3333
{
3434

3535
alphabet[i] = getchar();
3636
getchar();
3737
}
3838
printf("Enter the number of states?\n");
39-
scanf("%d", &nostate);
39+
scanf("%d", &ns);
4040
printf("Enter the start state?\n");
4141
scanf("%d", &start);
4242
printf("Enter the number of final states?\n");
43-
scanf("%d", &nofinal);
43+
scanf("%d", &nf);
4444
printf("Enter the final states?\n");
45-
for (i = 0; i < nofinal; i++)
46-
scanf("%d", &finalstate[i]);
45+
for (i = 0; i < nf; i++)
46+
scanf("%d", &fs[i]);
4747
printf("Enter no of transition?\n");
48-
scanf("%d", &notransition);
49-
printf("NOTE:- [Transition is in the form--> qno alphabet qno]\n", notransition);
48+
scanf("%d", &nt);
49+
printf("NOTE:- [Transition is in the form--> qno alphabet qno]\n", nt);
5050
printf("NOTE:- [States number must be greater than zero]\n");
5151
printf("\nEnter transition?\n");
52-
for (i = 0; i < notransition; i++)
52+
for (i = 0; i < nt; i++)
5353
{
5454

5555
scanf("%d %c%d", &r, &c, &s);
56-
insert_trantbl(r, c, s);
56+
it(r, c, s);
5757
}
5858

5959
printf("\n");
6060

61-
for (i = 1; i <= nostate; i++)
61+
for (i = 1; i <= ns; i++)
6262
{
6363
c = 0;
6464
for (j = 0; j < 20; j++)
6565

6666
{
67-
buffer[j] = 0;
67+
b[j] = 0;
6868
e_closure[i][j] = 0;
6969
}
70-
findclosure(i, i);
70+
fc(i, i);
7171
}
7272
printf("Equivalent NFA without epsilon\n");
7373
printf("-----------------------------------\n");
7474
printf("start state:");
75-
print_e_closure(start);
75+
pec(start);
7676
printf("\nAlphabets:");
77-
for (i = 0; i < noalpha; i++)
77+
for (i = 0; i < na; i++)
7878
printf("%c ", alphabet[i]);
7979
printf("\n States :");
80-
for (i = 1; i <= nostate; i++)
81-
print_e_closure(i);
80+
for (i = 1; i <= ns; i++)
81+
pec(i);
8282

8383
printf("\nTnransitions are...:\n");
8484

85-
for (i = 1; i <= nostate; i++)
85+
for (i = 1; i <= ns; i++)
8686
{
8787

88-
for (j = 0; j < noalpha - 1; j++)
88+
for (j = 0; j < na - 1; j++)
8989
{
90-
for (m = 1; m <= nostate; m++)
90+
for (m = 1; m <= ns; m++)
9191
set[m] = 0;
9292
for (k = 0; e_closure[i][k] != 0; k++)
9393
{
@@ -97,15 +97,15 @@ void main()
9797
while (temp != NULL)
9898
{
9999

100-
unionclosure(temp->st);
100+
uc(temp->st);
101101
temp = temp->link;
102102
}
103103
}
104104
printf("\n");
105-
print_e_closure(i);
105+
pec(i);
106106
printf("%c\t", alphabet[j]);
107107
printf("{");
108-
for (n = 1; n <= nostate; n++)
108+
for (n = 1; n <= ns; n++)
109109
{
110110
if (set[n] != 0)
111111
printf("q%d,", n);
@@ -114,33 +114,33 @@ void main()
114114
}
115115
}
116116
printf("\n Final states:");
117-
findfinalstate();
117+
ffs();
118118
}
119119

120-
void findclosure(int x, int sta)
120+
void fc(int x, int sta)
121121
{
122122
struct node *temp;
123123
int i;
124-
if (buffer[x])
124+
if (b[x])
125125
return;
126126
e_closure[sta][c++] = x;
127-
buffer[x] = 1;
128-
if (alphabet[noalpha - 1] == 'e' && transition[x][noalpha - 1] != NULL)
127+
b[x] = 1;
128+
if (alphabet[na - 1] == 'e' && transition[x][na - 1] != NULL)
129129
{
130-
temp = transition[x][noalpha - 1];
130+
temp = transition[x][na - 1];
131131
while (temp != NULL)
132132
{
133-
findclosure(temp->st, sta);
133+
fc(temp->st, sta);
134134
temp = temp->link;
135135
}
136136
}
137137
}
138138

139-
void insert_trantbl(int r, char c, int s)
139+
void it(int r, char c, int s)
140140
{
141141
int j;
142142
struct node *temp;
143-
j = findalpha(c);
143+
j = fa(c);
144144
if (j == 999)
145145
{
146146
printf("error\n");
@@ -152,17 +152,17 @@ void insert_trantbl(int r, char c, int s)
152152
transition[r][j] = temp;
153153
}
154154

155-
int findalpha(char c)
155+
int fa(char c)
156156
{
157157
int i;
158-
for (i = 0; i < noalpha; i++)
158+
for (i = 0; i < na; i++)
159159
if (alphabet[i] == c)
160160
return i;
161161

162162
return (999);
163163
}
164164

165-
void unionclosure(int i)
165+
void uc(int i)
166166
{
167167
int j = 0, k;
168168
while (e_closure[i][j] != 0)
@@ -172,26 +172,26 @@ void unionclosure(int i)
172172
j++;
173173
}
174174
}
175-
void findfinalstate()
175+
void ffs()
176176
{
177177
int i, j, k, t;
178-
for (i = 0; i < nofinal; i++)
178+
for (i = 0; i < nf; i++)
179179
{
180-
for (j = 1; j <= nostate; j++)
180+
for (j = 1; j <= ns; j++)
181181
{
182182
for (k = 0; e_closure[j][k] != 0; k++)
183183
{
184-
if (e_closure[j][k] == finalstate[i])
184+
if (e_closure[j][k] == fs[i])
185185
{
186186

187-
print_e_closure(j);
187+
pec(j);
188188
}
189189
}
190190
}
191191
}
192192
}
193193

194-
void print_e_closure(int i)
194+
void pec(int i)
195195
{
196196
int j;
197197
printf("{");
-8 Bytes
Binary file not shown.

daa/6. Program for Quick Sort./qs

-40 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)