Professional Documents
Culture Documents
Lab 2
Lab 2
Laboratory – 2
Submitted By:
Aayush Anand
Roll Num: 19103001
Q1. Write a program to simulate the behaviour of DFA for recognizing constants.
=> Consider the given DFA.h as the header file for the subsequent program
#ifndef DFA_H
#define DFA_H
#include "stdbool.h"
#include "stdio.h"
struct State
{
int s_no;
bool is_final;
condition_func predicate;
};
// Implementations
void create_state(State* state, int state_number,
bool is_a_final_state, condition_func predicate_func)
{
state->s_no = state_number;
state->is_final = is_a_final_state;
state->predicate = predicate_func;
}
int idx = 0;
while(str[idx] != 0)
{
State* cur = dfa->cur_state;
if (!cur)
return false;
idx++;
}
return dfa->cur_state && dfa->cur_state->is_final;
}
#endif
q1.c
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include "DFA.h"
void init(DFA*);
int main()
{
DFA dfa;
init(&dfa);
char istr[30];
printf("Enter the string:");
scanf("%s", istr);
return 0;
}
Output
Q2. Write a program to simulate the behaviour of DFA for recognizing operators of the mini
language.
=> Consider the given DFA.h as the header file for the subsequent program
#ifndef DFA_H
#define DFA_H
#include "stdbool.h"
#include "stdio.h"
struct State
{
int s_no;
bool is_final;
condition_func predicate;
};
// Implementations
void create_state(State* state, int state_number,
bool is_a_final_state, condition_func predicate_func)
{
state->s_no = state_number;
state->is_final = is_a_final_state;
state->predicate = predicate_func;
}
int idx = 0;
while(str[idx] != 0)
{
State* cur = dfa->cur_state;
if (!cur)
return false;
idx++;
}
return dfa->cur_state && dfa->cur_state->is_final;
}
#endif
q2.c
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include "DFA.h"
void init(DFA*);
int main()
{
DFA dfa;
init(&dfa);
char istr[30];
printf("Enter the string:");
scanf("%s", istr);
return 0;
}
// State initialization
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, true, q2Pred);
}
Output