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

DRONACHARYA GROUP OF

INSTITUTIONS

B.TECH. COMPUTER
SCIENCE 5th SEMESTER

LAB PRACTICAL FILE: COMPILER


DESIGN(KCS552)

SUBMITTED TO:
PROF.NIHARIKA NAMDEV

NAME:Isha Rawat
CLASS:G1-A
ROLL.NO:16081
COMPILER DESIGN LAB (KCS 552)
List of Experiments

Sr Name Of Experiments Dates of


No Experimen
ts
1 WRITE A C PROGRAM TO RECOGNIZE STRING
UNDER
„a*‟,‟a*b+‟,‟abb‟.
2 Implementation of Lexical Analyzer using Lex Tool.

3 WRITE A LEX PROGRAM TO CHECK WHETHER


INPUT STRING IS VERB OR NOT.(LIST OF VERB
ARE GIVEN).
4 WRITE A LEX PROGRAM TO COUNT NUMBER
OF CHARACTER AND NUMBER OF LINES
5 Study the LEX and YACC tool and evaluate an arithmetic
expression with parentheses, unary and binary operators
using
Flex and Yacc (CALCULATOR)
6 Write program to find ε – closure of all states of any given
NFA
with ε transition.
7 Write program to convert NFA with ε transition to NFA
without
ε transition.
8 Write program to convert NFA to DFA.
9 Write program to minimize any given DFA.
10 Develop an operator precedence parser for a given
language.
GNERAL LABORATORY INSTRUCTIONS

1. Students are advised to come to the laboratory at least 5 minutes before (to
starting time), those who come after 5 minutes will not be allowed into the
lab.
2. Plan your task properly much before to the commencement, come
prepared to the lab withthe synopsis / program / experiment details.
3. Student should enter into the laboratory with:
a. Laboratory observation notes with all the details (Problem statement,
Aim, Algorithm, Procedure, Program, Expected Output, etc.,) filled in
for the lab session.
b. Laboratory Record updated up to the last session experiments and
other utensils (if any)needed in the lab.
c. Proper Dress code and Identity card.
4. Sign in the laboratory login register, write the TIME-IN, and occupy the
computer systemallotted to you by the faculty.
5. Execute your task in the laboratory, and record the results / output in
the lab observationnote book, and get certified by the concerned faculty.
6. All the students should be polite and cooperative with the laboratory staff,
must maintain thediscipline and decency in the laboratory.
7. Computer labs are established with sophisticated and high end branded
systems, which shouldbe utilized properly.
8. Students / Faculty must keep their mobile phones in SWITCHED OFF
mode during the lab sessions. Misuse of the equipment, misbehaviors
with the staff and systems etc., will attract severe punishment.
9. Students must take the permission of the faculty in case of any urgency to go
out ; if anybody found loitering outside the lab / class without permission
during working hours willbe treated seriously and punished appropriately.
10.Students should LOG OFF/ SHUT DOWN the computer system before he/
she leaves the lab after completing the task (experiment) in all aspects. He/
she must ensure the system / seat is kept properly.

\
SOFTWARE REQUIREMENTS

SOFTWARE

1. Operating System (Windows / Linux )

2. IDE for C language (Turbo C / C++, Borland C / C++ or any)

3. Flex & Bison


PROGRAM NO:-1

OBJECT:WRITE A C PROGRAM TO RECOGNIZE STRING UNDER


„a*‟,‟a*b+‟,‟abb‟.

#include<stdio.h>
#include<string.h
>
#include<stdlib.h
> void main()
{
char s[20],c;
int state=0,i=0;
printf("\n enter a
string:"); scanf("\n%s",s);
while(s[i]!='\0')
{
switch(state)
{
case 0:
c=s[i++];
if(c=='a'
) state
=1;
else
if(c=='b') state=2;
else
state=6
; break;
case 1:
c=s[i++];
if(c=='a'
) state=3;
else
if(c=='b') state=4;
els
e state=6;
break;
case 2:
c=s[i++];
if(c=='a'
) state=6;
else if(c=='b')
state=2;
els
e state=6;
break;
case 3:
c=s[i++];
if(c=='a'
) state=3;
else
if(c=='b') state=2;
els
e state=6;
break;
case 4:
c=s[i++];
if(c=='a'
) state=6;
else
if(c=='b') state=5;
els
e state=6;
break;
case 5:
c=s[i++];
if(c=='a'
) state=6;
else
if(c=='b') state=2;
els
e state=6;
break;
case 6:
printf("%s is not
recognised.",s); exit(0);
}
}
if((state==1)||(state==3)||(state==0))
printf("\n %s is accepted under rule
'a*'",s); else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);

PROGRAM N0:02

OBJECT: Implementation of Lexical Analyzer using Lex Tool


%{
#include <stdio.h>
%}

%%

int|float { printf("Type: %s\n", yytext); }


[0-9]+ { printf("Number: %s\n", yytext); }
[a-zA-Z][a-zA-Z0-9]* { printf("Identifier: %s\n", yytext); }
[ \t\n] ; // Ignore whitespace characters

. { printf("Unknown token: %s\n", yytext); }

%%

int main() {
yylex();
return 0;
}
lex lexer.l
gcc lex.yy.c -o lexer -ll
int x = 42;
float y = 3.14;
./lexer < input.txt
output
Type: int
Identifier: x
Unknown token: =
Number: 42
Type: float
Identifier: y
Unknown token: =
Number: 3.14

PROGRAM:03

OBJECT:WRITE A LEX PROGRAM TO CHECK WHETHER INPUT STRING


IS VERB OR NOT.(LIST OF VERB ARE GIVEN)

%%
[\t]
+ is |
am
| are|
was|
were{printf(“%s: is a verb”,yytext);}
[a-zA-Z]+{printf(“%s: is not a verb,yytext);}
%%
main()
{
yylex();
}

OUTPUT: $ ./a.out
are
are:is a verb
PROGRAM NO:04

PROGRAM:WRITE A LEX PROGRAM TO COUNT NUMBER OF


CHARACTER AND NUMBER OF LINES.

%{
#include<stdio.h>
int num_lines=0,num_chars=0;
%}
%%
\n {++num_lines; ++num_chars;}
. {++num_chars;}
%%
int main()
{
yylex();
printf("There are %d lines and %d characters. \n", num_lines,num_chars);
return 0;
}

OUTPUT: > ./a.out


Compiler Design.
There are 1 lines and 17 characters.
PROGRAM NO:05
Object: Study the LEX and YACC tool and evaluate an arithmetic
expression with parentheses, unary and binary operators using
Flex and Yacc (CALCULATOR)

Lex code:

%{
#include <stdio.h>
#include "y.tab.h"
%}

%%
[ \t]+ ; // ignore all whitespace

[a-z]+ { yylval.str = strdup(yytext); return ID; }

[0-9]+ { yylval.num = atoi(yytext); return NUMBER; }

"+" { return PLUS; }


"-" { return MINUS; }
"*" { return TIMES; }
"/" { return DIVIDE; }
"(" { return LPAREN; }
")" { return RPAREN; }
";" { return SEMICOLON; }

. { fprintf(stderr, "Invalid character: %s\

%%

/* You may add additional C code if needed */

Yacc:

%{
#include "lex.yy.c"
#include <stdio.h>

void yyerror(const char *str) {


fprintf(stderr, "Error: %s\n", str);
}

int yywrap() {
return 1;
}

int main(){
printf("Enter the expression:\n");
yyparse();
return 0;
}

%}

/* Debinitions */
%token NUMBER
%token ID

%left '-' '+'


%left '*' '/'
%right '**'
%left SIN COS TAN

%% /* Rules */
E : T {

T:
T '+' T { $$ = $1 + $3; }
| T '-' T { $$ = $1 - $3; }
| T '*' T { $$ = $1 * $3; }
| T '/' T { $$ = $1 / $3; }
| T '**' T { $$ = $1 * $3; }
| SIN T { $$ = sin($2); }
| COS T { $$ = cos($2); }
| TAN T { $$ = tan($2); }
| '(' T ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| ID { $$ = $1;

%%

/* For printing error messages */


int yyerror(char* s) {
printf("\nExpression is invalid\n");
return 0;
}

PROGRAM NO:06
PROGRAM:Write program to find ε – closure of all states of any given NFA
with ε transition
#include <stdio.h>
#include <stdlib.h>

#define MAX_STATES 10
#define MAX_TRANSITIONS 10
// Structure to represent a state in the NFA
struct State {
int id;
int transitions[MAX_TRANSITIONS];
};

// Function to compute ε-closure for a given state


void epsilonClosure(int state, int n, int epsilon[MAX_STATES][MAX_STATES],
int closure[MAX_STATES]) {
int i;
closure[state] = 1; // Mark the current state as visited

// Explore all states reachable through ε-transitions


for (i = 0; i < n; i++) {
if (epsilon[state][i] == 1 && closure[i] == 0) {
epsilonClosure(i, n, epsilon, closure);
}
}
}

// Function to print the ε-closure of all states


void printEpsilonClosure(int n, int epsilon[MAX_STATES][MAX_STATES]) {
int i, j;
printf("ε-Closure of States:\n");

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


int closure[MAX_STATES] = {0}; // Initialize closure array
epsilonClosure(i, n, epsilon, closure);

printf("ε-Closure(q%d) = {", i);


for (j = 0; j < n; j++) {
if (closure[j] == 1) {
printf("q%d ", j);
}
}
printf("}\n");
}
}

int main() {
int n; // Number of states in NFA
int epsilon[MAX_STATES][MAX_STATES]; // ε-transition matrix

printf("Enter the number of states in the NFA: ");


scanf("%d", &n);

// Input ε-transition matrix


printf("Enter the ε-transition matrix (1 for ε-transition, 0 for no ε-transition):
\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &epsilon[i][j]);
}
}

// Compute and print ε-closure for each state


printEpsilonClosure(n, epsilon);

return 0;
}

OUTPUT
gcc -o epsilon_closure epsilon_closure.c
./epsilon_closure

011
001
000

ε-Closure(q0) = { q0 q1 q2 }
ε-Closure(q1) = { q1 q2 }
ε-Closure(q2) = { q2 }

PROGRAM NO:07

PROGRAM: Write program to convert NFA with ε transition to NFA without


ε transition.
#include <stdio.h>
#include <stdlib.h>

#define MAX_STATES 10
#define MAX_ALPHABET 10

// Structure to represent a state in the NFA


struct State {
int id;
int transitions[MAX_ALPHABET][MAX_STATES];
};
// Function to compute ε-closure for a given state
void epsilonClosure(int state, int n, int epsilon[MAX_STATES][MAX_STATES],
int closure[MAX_STATES]) {
int i;
closure[state] = 1; // Mark the current state as visited

// Explore all states reachable through ε-transitions


for (i = 0; i < n; i++) {
if (epsilon[state][i] == 1 && closure[i] == 0) {
epsilonClosure(i, n, epsilon, closure);
}
}
}

// Function to convert NFA with ε-transitions to NFA without ε-transitions


void convertToNFAWithoutEpsilon(int n, int alphabetSize, int
epsilon[MAX_STATES][MAX_STATES], struct State nfaStates[]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < alphabetSize; j++) {
int closure[MAX_STATES] = {0}; // Initialize closure array
epsilonClosure(i, n, epsilon, closure);

// Compute transition for each possible input symbol


for (int k = 0; k < n; k++) {
if (closure[k] == 1) {
nfaStates[i].transitions[j][k] = nfaStates[k].transitions[j][i] = 1;
}
}
}
}
}

// Function to print the NFA without ε-transitions


void printNFAWithoutEpsilon(int n, int alphabetSize, struct State nfaStates[]) {
printf("NFA without ε-transitions:\n");

for (int i = 0; i < n; i++) {


printf("State q%d:\n", i);
for (int j = 0; j < alphabetSize; j++) {
printf(" on input %c: {", 'a' + j);
for (int k = 0; k < n; k++) {
if (nfaStates[i].transitions[j][k] == 1) {
printf("q%d ", k);
}
}
printf("}\n");
}
}
}

int main() {
int n; // Number of states in NFA
int alphabetSize; // Size of the input alphabet
int epsilon[MAX_STATES][MAX_STATES]; // ε-transition matrix
struct State nfaStates[MAX_STATES]; // NFA states

printf("Enter the number of states in the NFA: ");


scanf("%d", &n);

printf("Enter the size of the input alphabet: ");


scanf("%d", &alphabetSize);

// Input ε-transition matrix


printf("Enter the ε-transition matrix (1 for ε-transition, 0 for no ε-transition):
\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &epsilon[i][j]);
}
}

// Input transitions for each state


printf("Enter transitions for each state:\n");
for (int i = 0; i < n; i++) {
nfaStates[i].id = i;
for (int j = 0; j < alphabetSize; j++) {
printf("Enter transitions for state q%d on input %c (comma-separated
list of states, -1 to end): ", i, 'a' + j);
int nextState;
while (1) {
scanf("%d", &nextState);
if (nextState == -1) {
break;
}
nfaStates[i].transitions[j][nextState] = 1;
}
}
}
// Convert to NFA without ε-transitions
convertToNFAWithoutEpsilon(n, alphabetSize, epsilon, nfaStates);

// Print the NFA without ε-transitions


printNFAWithoutEpsilon(n, alphabetSize, nfaStates);

return 0;
}

OUTPUT
gcc -o nfa_conversion nfa_conversion.c
./nfa_conversion
011
001
000
State q0:
on input a: 1 -1
on input b: 2 -1
State q1:
on input a: 1 -1
on input b: 2 -1
State q2:
on input a: -1
on input b: -1

NFA without ε-transitions:


State q0:
on input a: {q1 q2 }
on input b: {q2 }
State q1:
on input a: {q1 q2 }
on input b: {q2 }
State q2:
on input a: {}
on input b: {}

PROGRAM NO:08
PROGRAM:Write program to convert NFA to DFA.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STATES 10
#define MAX_ALPHABET 10

// Structure to represent a state in the NFA


struct NFAState {
int id;
int transitions[MAX_ALPHABET][MAX_STATES];
};

// Structure to represent a state in the DFA


struct DFAState {
int id;
int nfaStates[MAX_STATES];
int transitions[MAX_ALPHABET];
};

// Function to compute ε-closure for a given set of NFA states


void epsilonClosure(int n, int epsilon[MAX_STATES][MAX_STATES], int
inputStates[], int closure[MAX_STATES]) {
int i, j;

// Initialize closure array


for (i = 0; i < n; i++) {
closure[i] = 0;
}

// Compute ε-closure for each state in the input set


for (i = 0; i < n; i++) {
int state = inputStates[i];
closure[state] = 1; // Mark the current state as visited

// Explore all states reachable through ε-transitions


for (j = 0; j < n; j++) {
if (epsilon[state][j] == 1 && closure[j] == 0) {
epsilonClosure(n, epsilon, &j, closure);
}
}
}
}
// Function to find the set of states reachable from a given set on a specific input
symbol
void move(int n, int inputStates[], int alphabetSize, int nfaStates[], int
epsilon[MAX_STATES][MAX_STATES], int resultStates[MAX_STATES]) {
int i, j, k;
int closure[MAX_STATES];

// Initialize result states array


for (i = 0; i < MAX_STATES; i++) {
resultStates[i] = -1;
}

// Compute ε-closure for the input set of NFA states


epsilonClosure(n, epsilon, inputStates, closure);

// Explore transitions on the input symbol


for (i = 0; i < n; i++) {
if (closure[i] == 1) {
for (j = 0; j < alphabetSize; j++) {
if (nfaStates[i].transitions[j] == 1) {
// Compute ε-closure for the set of states reached on the input symbol
for (k = 0; k < n; k++) {
if (resultStates[k] == -1) {
resultStates[k] = j;
break;
}
}
}
}
}
}
}

// Function to convert NFA to DFA


void convertToDFA(int n, int alphabetSize, int epsilon[MAX_STATES]
[MAX_STATES], struct NFAState nfaStates[], struct DFAState dfaStates[]) {
int i, j;
int dfaStateCount = 1;
int queue[MAX_STATES][MAX_STATES];
int front = -1, rear = -1;
int visited[MAX_STATES];

// Initialize visited array


for (i = 0; i < MAX_STATES; i++) {
visited[i] = 0;
}

// Initialize DFA state queue with the initial state


queue[++rear][0] = 0; // Start with the ε-closure of the initial state
visited[0] = 1; // Mark the initial state as visited

while (front != rear) {


int currentSet[MAX_STATES];
int resultStates[MAX_STATES];
int currentState = queue[++front][0];

// Get the set of NFA states represented by the current DFA state
for (i = 0; i < MAX_STATES; i++) {
currentSet[i] = -1;
}

int setIndex = 0;
for (i = 0; i < n; i++) {
if (visited[i] == 1 && queue[front][setIndex] != -1) {
currentSet[setIndex] = i;
setIndex++;
}
}

// For each input symbol, compute the set of NFA states reachable from the
current set on that symbol
for (i = 0; i < alphabetSize; i++) {
move(n, currentSet, alphabetSize, nfaStates, epsilon, resultStates);

// Check if the resulting set is already in the DFA


int existingState = -1;
for (j = 0; j < dfaStateCount; j++) {
if (memcmp(resultStates, dfaStates[j].nfaStates, sizeof(int) *
MAX_STATES) == 0) {
existingState = j;
break;
}
}

// If not, add it to the DFA states queue


if (existingState == -1) {
existingState = dfaStateCount;
dfaStateCount++;
for (j = 0; j < MAX_STATES; j++) {
dfaStates[existingState].nfaStates[j] = resultStates[j];
}
queue[++rear][0] = existingState;
for (j = 0; j < MAX_STATES; j++) {
queue[rear][j + 1] = resultStates[j];
}
}

// Set the transition from the current DFA state to the newly computed DFA
state
dfaStates[currentState].transitions[i] = existingState;
}
}
}

// Function to print the DFA


void printDFA(int n, int alphabetSize, struct DFAState dfaStates[]) {
printf("DFA:\n");

for (int i = 0; i < n; i++) {


printf("State q%d:\n", i);
for (int j = 0; j < alphabetSize; j++) {
printf(" on input %c: q%d\n", 'a' + j, dfaStates[i].transitions[j]);
}
}
}

int main() {
int n; // Number of states in NFA
int alphabetSize; // Size of the input alphabet
int epsilon[MAX_STATES][MAX_STATES]; // ε-transition matrix
struct NFAState nfaStates[MAX_STATES]; // NFA states
struct DFAState dfaStates[MAX_STATES]; // DFA states

printf("Enter the number of states in the NFA: ");


scanf("%d", &n);

printf("Enter the size of the input alphabet: ");


scanf("%d", &alphabetSize);

// Input ε-transition matrix


printf("Enter the ε-transition matrix (1 for ε-transition, 0 for no ε-transition):\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &epsilon[i][j]);
}
}

// Input transitions for each state


printf("Enter transitions for each state:\n");
for (int i = 0; i < n; i++) {
nfaStates[i].id = i;
for (int j = 0; j < alphabetSize; j++) {
printf("Enter transitions for state q%d on input %c (comma-separated list of
states, -1 to end): ", i, 'a' + j);
int nextState;
while (1) {
scanf("%d", &nextState);
if (nextState == -1) {
break;
}
nfaStates[i].transitions[j][nextState] = 1;
}
}
}

// Convert to DFA
convertToDFA(n, alphabetSize, epsilon, nfaStates, dfaStates);

// Print the DFA


printDFA(n, alphabetSize, dfaStates);

return 0;
}

OUTPUT
gcc -o nfa_to_dfa nfa_to_dfa.c
./nfa_to_dfa
011
001
000
DFA:
State q0:
on input a: q1
on input b: q2
State q1:
on input a: q1
on input b: q2
State q2:
on input a: q2
on input b: q2

PROGRAM NO:9
PROGRAM: Write program to minimize any given DFA.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX_STATES 10
#define MAX_ALPHABET 10

struct Partition {
int state;
struct Partition* next;
};

struct State {
int id;
int transitions[MAX_ALPHABET];
};

struct PartitionSet {
struct Partition* partitions[MAX_STATES];
};

// Function to initialize the partition set


void initializePartitionSet(int n, struct PartitionSet* partitionSet) {
for (int i = 0; i < n; i++) {
partitionSet->partitions[i] = NULL;
}
}

// Function to create a new partition


struct Partition* createPartition(int state) {
struct Partition* newPartition = (struct Partition*)malloc(sizeof(struct Partition));
newPartition->state = state;
newPartition->next = NULL;
return newPartition;
}

// Function to add a state to a partition


void addToPartition(struct Partition** partition, int state) {
struct Partition* newPartition = createPartition(state);
newPartition->next = *partition;
*partition = newPartition;
}

// Function to split a partition based on a distinguishing set of states


void splitPartition(int alphabetSize, int distinguishingSet[MAX_STATES], struct Partition* partition,
struct PartitionSet* newPartitionSet) {
struct Partition* current = partition;
while (current != NULL) {
int state = current->state;
addToPartition(&newPartitionSet->partitions[distinguishingSet[state]], state);
current = current->next;
}
}

// Function to determine if a state is in a partition


bool isInPartition(int state, struct Partition* partition) {
while (partition != NULL) {
if (partition->state == state) {
return true;
}
partition = partition->next;
}
return false;
}

// Function to check if two partitions are equal


bool arePartitionsEqual(struct Partition* partition1, struct Partition* partition2) {
while (partition1 != NULL && partition2 != NULL) {
if (partition1->state != partition2->state) {
return false;
}
partition1 = partition1->next;
partition2 = partition2->next;
}
return partition1 == NULL && partition2 == NULL;
}

// Function to check if a partition is present in a set of partitions


bool isPartitionInSet(struct Partition* partition, struct PartitionSet* partitionSet, int n) {
for (int i = 0; i < n; i++) {
if (arePartitionsEqual(partition, partitionSet->partitions[i])) {
return true;
}
}
return false;
}

// Function to find the set of states that are distinguishable from each other
void findDistinguishableStates(int n, int alphabetSize, struct State states[MAX_STATES], int
distinguishable[MAX_STATES][MAX_STATES]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
distinguishable[i][j] = 1; // Initialize as distinguishable
}
}

for (int i = 0; i < n; i++) {


for (int j = i + 1; j < n; j++) {
if ((states[i].transitions[0] != states[j].transitions[0]) || (states[i].transitions[1] !=
states[j].transitions[1])) {
distinguishable[i][j] = 0; // Mark as indistinguishable
}
}
}
}

// Function to minimize the DFA using Hopcroft's algorithm


void minimizeDFA(int n, int alphabetSize, struct State states[MAX_STATES], int* finalStates, int*
partitions[MAX_STATES], int* partitionCount) {
int distinguishable[MAX_STATES][MAX_STATES];

findDistinguishableStates(n, alphabetSize, states, distinguishable);

struct PartitionSet partitionSet1;


struct PartitionSet partitionSet2;

initializePartitionSet(n, &partitionSet1);
initializePartitionSet(n, &partitionSet2);

for (int i = 0; i < n; i++) {


if (finalStates[i] == 1) {
addToPartition(&partitionSet1.partitions[0], i);
} else {
addToPartition(&partitionSet1.partitions[1], i);
}
}

int p;
for (int i = 0; i < n; i++) {
if (isInPartition(i, partitionSet1.partitions[0])) {
p = 0;
} else {
p = 1;
}

partitions[i] = p;
}
int previousPartitionCount = 0;

do {
previousPartitionCount = *partitionCount;

initializePartitionSet(n, &partitionSet2);

for (int i = 0; i < n; i++) {


for (int j = i + 1; j < n; j++) {
if (distinguishable[i][j] == 0) {
int partition1 = partitions[i];
int partition2 = partitions[j];

if (partition1 != partition2) {
if (isInPartition(i, partitionSet2.partitions[partition1])) {
addToPartition(&partitionSet2.partitions[partition1], j);
} else {
addToPartition(&partitionSet2.partitions[partition2], j);
}
}
}
}
}

*partitionCount = 0;
for (int i = 0; i < n; i++) {
if (isPartitionInSet(partitionSet2.partitions[i], &partitionSet1, n)) {
partitions[i] = *partitionCount;
(*partitionCount)++;
} else {
partitions[i] = *partitionCount - 1;
}
}

for (int i = 0; i < *partitionCount; i++) {


partitionSet1.partitions[i] = partitionSet2.partitions[i];
}
} while (previousPartitionCount != *partitionCount);
}

// Function to print the minimized DFA


void printMinimizedDFA(int n, int alphabetSize, struct State states[MAX_STATES], int* finalStates,
int* partitions, int partitionCount) {
printf("Minimized DFA:\n");

for (int i = 0; i < partitionCount; i++) {


printf("State q%d:\n", i);
for (int j = 0; j < alphabetSize; j++) {
int nextPartition = partitions[states[i].transitions[j]];
printf(" on input %c: q%d\n", 'a' + j, nextPartition);
}
}

printf("Final States: ");


for (int i = 0; i < partitionCount; i++) {
if (finalStates[states[i].id] == 1) {
printf("q%d ", i);
}
}
printf("\n");
}

int main() {
int n; // Number of states in DFA
int alphabetSize; // Size of the input alphabet
struct State states[MAX_STATES]; // DFA states
int finalStates[MAX_STATES]; // Final states
int partitions[MAX_STATES]; // Partition for each state
int partitionCount = 0; // Number of partitions

printf("Enter the number of states in the DFA: ");


scanf("%d", &n);

printf("Enter the size of the input alphabet: ");


scanf("%d", &alphabetSize);

// Input transitions for each state


printf("Enter transitions for each state:\n");
for (int i = 0; i < n; i++) {
states[i].id = i;
for (int j = 0; j < alphabetSize; j++) {
printf("Enter transitions for state q%d on input %c: ", i, 'a' + j);
scanf("%d", &states[i].transitions[j]);
}
}

// Input final states


printf("Enter the final states (1 for final, 0 for non-final):\n");
for (int i = 0; i < n; i++) {
scanf("%d", &finalStates[i]);
}

// Minimize the DFA


minimizeDFA(n, alphabetSize, states, finalStates, partitions, &partitionCount);
// Print the minimized DFA
printMinimizedDFA(n, alphabetSize, states, finalStates, partitions, partitionCount);

return 0;
}

OUTPUT

Enter the number of states in the DFA: 3


Enter the size of the input alphabet: 2
Enter transitions for each state:
Enter transitions for state q0 on input a: 1
Enter transitions for state q0 on input b: 2
Enter transitions for state q1 on input a: 1
Enter transitions for state q1 on input b: 0
Enter transitions for state q2 on input a: 2
Enter transitions for state q2 on input b: 0
Enter the final states (1 for final, 0 for non-final):
100

Minimized DFA:
State q0:
on input a: q1
on input b: q2
State q1:
on input a: q1
on input b: q0
State q2:
on input a: q2
on input b: q0
Final States: q0
PROGRAM NO:10
PROGRAM: Develop an operator precedence parser for a given language .

#include<stdlib.h>
#include<stdio.h>
#include<string.h>

// function f to exit from the loop


// if given condition is not true
void f()
{
printf("Not operator grammar");
exit(0);
}

void main()
{
char grm[20][20], c;

// Here using flag variable,


// considering grammar is not operator grammar
int i, n, j = 2, flag = 0;

// taking number of productions from user


scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%s", grm[i]);

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


c = grm[i][2];

while (c != '&#092;&#048;') {

if (grm[i][3] == '+' || grm[i][3] == '-'


|| grm[i][3] == '*' || grm[i][3] == '/')

flag = 1;

else {
flag = 0;
f();
}

if (c == '$') {
flag = 0;
f();
}

c = grm[i][++j];
}
}

if (flag == 1)
printf("Operator grammar");
}

OUTPUT
Input :3
A=A*A
B=AA
A=$

Output : Not operator grammar

Input :2
A=A/A
B=A+A

Output : Operator grammar

You might also like