Skip to content

Commit 43c4985

Browse files
committed
Added NFA program and Lab 3 Programs extracted
1 parent 87febc6 commit 43c4985

20 files changed

+552
-2
lines changed

CompilerDesignInC/CompilerDesignInC.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
<ClInclude Include="RecognizeStrings.h" />
148148
</ItemGroup>
149149
<ItemGroup>
150-
<ClCompile Include="main.c" />
150+
<ClCompile Include="RepresentNfa.c" />
151151
</ItemGroup>
152152
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
153153
<ImportGroup Label="ExtensionTargets">

CompilerDesignInC/CompilerDesignInC.vcxproj.filters

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</Filter>
1616
</ItemGroup>
1717
<ItemGroup>
18-
<ClCompile Include="main.c">
18+
<ClCompile Include="RepresentNfa.c">
1919
<Filter>Source Files</Filter>
2020
</ClCompile>
2121
</ItemGroup>

CompilerDesignInC/RepresentNfa.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
3+
/**
4+
* \brief MAX_STATES maximum no of states
5+
*/
6+
#define MAX_STATES 10
7+
#define TRAP_STATE (-999)
8+
#define NOT_DEFINED (-1)
9+
#define FINAL_STATE 999
10+
#define SYMBOLS 3
11+
12+
typedef struct nfa
13+
{
14+
int a[10];
15+
int b[10];
16+
int epsilon[10];
17+
}nfa;
18+
19+
int main(void)
20+
{
21+
int i, j, k;
22+
23+
int no_of_states, no_of_final_state;
24+
const int no_of_relations = 0;
25+
int final_states[MAX_STATES];
26+
27+
// First, we are taking all the states as trap, to start our process
28+
// It also helps to remove garbage values
29+
30+
for (i = 0; i < MAX_STATES; i++)
31+
final_states[i] = TRAP_STATE;
32+
33+
// Taking all the user inputs
34+
35+
printf("Enter no of states: ");
36+
scanf_s("%d", &no_of_states);
37+
38+
printf("Enter no of final states: ");
39+
scanf_s("%d", &no_of_final_state);
40+
41+
printf("Enter index(State no) of final states: ");
42+
for (i = 0; i < no_of_final_state; i++)
43+
{
44+
int index;
45+
scanf_s("%d", &index);
46+
final_states[index] = FINAL_STATE;
47+
}
48+
49+
// Assuming that the first dimension indicating the states
50+
// second dimension indicating the symbols or edges
51+
// third dimension indicates the max no of relations defined for that state
52+
int matrix[MAX_STATES][SYMBOLS][MAX_STATES];
53+
54+
printf("We are assuming that the symbols of this NFA are \'a\' and \'b\' and \'E\'\n");
55+
printf("Here \'E\' is for epsilon (lambda NFA)\n");
56+
57+
// Again Garbage Collection and TRAP STATES
58+
for (i = 0; i < MAX_STATES; i++)
59+
for (j = 0; j < SYMBOLS; j++)
60+
for (k = 0; k < MAX_STATES; k++)
61+
matrix[i][j][k] = NOT_DEFINED;
62+
63+
// taking an array of symbols to get different edges for different symbols
64+
// We are assuming that edges are all of integer types
65+
int array_of_symbols[SYMBOLS];
66+
67+
// this part is not user input able right now as we are omitting user input in case of symbols
68+
// We are hard coding this part right now
69+
for (i = 0; i < SYMBOLS; i++)
70+
{
71+
// Assume 2 for epsilon
72+
array_of_symbols[i] = i;
73+
}
74+
75+
// Now taking the user input for creating the table
76+
for (i = 0; i < no_of_states; i++)
77+
{
78+
79+
80+
for (j = 0; j < SYMBOLS; j++)
81+
{
82+
printf("Enter no of relations from the state %d for the symbol %d (Enter -1 for no relations): ", i, array_of_symbols[j]);
83+
scanf_s("%d", no_of_relations);
84+
85+
if (no_of_relations != -1)
86+
{
87+
for (k = 0; k < no_of_relations; k++)
88+
{
89+
printf("Enter relation from the state %d for the symbol %d: ", i, array_of_symbols[j]);
90+
scanf_s("%d", matrix[i][j][k]);
91+
}
92+
}
93+
}
94+
}
95+
}

lab3/lab3/.~lock.questions.odt#

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
,lab,kiit-ThinkCentre-M72e,01.02.2019 10:41,file:///home/lab/.config/libreoffice/4;

lab3/lab3/a.out

8.19 KB
Binary file not shown.

lab3/lab3/a.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a
2+
b
3+
c
4+
d
5+
e

lab3/lab3/b.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Tell me your name
2+
I am here for you
3+
You know me
4+
Lorem Ipsum
5+
Egypt
6+
Go to hell
7+
bad programs

lab3/lab3/c.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a
2+
c
3+
d
4+
e

lab3/lab3/cfg.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A -> Bb | E
2+
B -> c

lab3/lab3/p1.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
// A program to print a file after deleting specified line no
6+
7+
int main(void)
8+
{
9+
FILE *fp1, *fp2;
10+
char ch;
11+
int i, n;
12+
13+
fp1 = fopen("a.txt", "r");
14+
fp2 = fopen("c.txt", "w");
15+
16+
i = i / 2;// Hello this is a comment
17+
18+
if (fp1 == NULL)
19+
{
20+
printf("File does not exist");
21+
exit(-1);
22+
}
23+
24+
printf("Enter line no you want to delete: ");
25+
scanf("%d", &n);
26+
27+
int line = 1;
28+
char *str;
29+
30+
ch = fgetc(fp1);
31+
32+
while(ch != EOF)
33+
{
34+
if (line != n)
35+
{
36+
putc(ch, fp2);
37+
}
38+
if (ch == '\n')
39+
{
40+
line++;
41+
}
42+
43+
ch = fgetc(fp1);
44+
}
45+
46+
fclose(fp1);
47+
fclose(fp2);
48+
49+
printf("File after removal of the line no %d\n", n);
50+
fp1 = fopen("c.txt", "r");
51+
52+
ch = fgetc(fp1);
53+
54+
while (ch != EOF)
55+
{
56+
printf("%c", ch);
57+
58+
ch = fgetc(fp1);
59+
}
60+
61+
fclose(fp1);
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)