Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 4

System Programming and Compiler Design

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"

typedef struct State State;


typedef State* (*condition_func)(char input);

typedef struct DFA


{
State* cur_state;
bool (*run)(struct DFA*, const char*);
} DFA;

struct State
{
int s_no;
bool is_final;
condition_func predicate;
};

void create_state(State*, int state_number,


bool is_a_final_state, condition_func predicate_func);
bool run_dfa(DFA*, const char*);

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

bool run_dfa(DFA* dfa, const char* input_str)


{
const char* str = input_str;

int idx = 0;
while(str[idx] != 0)
{
State* cur = dfa->cur_state;

if (!cur)
return false;

State* next_state = cur->predicate(str[idx]);


dfa->cur_state = next_state;

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"

State q1, q2;

State* q1Pred(char ip)


{
if (ip == '+' || ip == '-' || isdigit(ip))
{
return &q1;
}
else
return &q2;
}

State* q2Pred(char ip)


{
// dead state
return NULL;
}

void init(DFA*);

int main()
{
DFA dfa;
init(&dfa);

char istr[30];
printf("Enter the string:");
scanf("%s", istr);

bool result = dfa.run(&dfa, istr);

printf(result ? "Accepted\n" : "Rejected\n");

return 0;
}

void init(DFA* dfa)


{
dfa->cur_state = &q1;
dfa->run = run_dfa;

// State initialization code here


create_state(&q1, 1, true, q1Pred);
create_state(&q2, 2, false, q2Pred);
}

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"

typedef struct State State;


typedef State* (*condition_func)(char input);

typedef struct DFA


{
State* cur_state;
bool (*run)(struct DFA*, const char*);
} DFA;

struct State
{
int s_no;
bool is_final;
condition_func predicate;
};

void create_state(State*, int state_number,


bool is_a_final_state, condition_func predicate_func);
bool run_dfa(DFA*, const char*);

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

bool run_dfa(DFA* dfa, const char* input_str)


{
const char* str = input_str;

int idx = 0;
while(str[idx] != 0)
{
State* cur = dfa->cur_state;

if (!cur)
return false;

State* next_state = cur->predicate(str[idx]);


dfa->cur_state = next_state;

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"

State q1, q2;

State* q1Pred(char ip)


{
if (ip == '+' || ip == '-' || ip == '/' || ip == '%' || ip == '*')
{
return &q2;
}
else
return &q1;
}

State* q2Pred(char ip)


{
// final state
return &q2;
}

void init(DFA*);

int main()
{
DFA dfa;
init(&dfa);

char istr[30];
printf("Enter the string:");
scanf("%s", istr);

bool result = dfa.run(&dfa, istr);

printf(result ? "Accepted\n" : "Rejected\n");

return 0;
}

void init(DFA* dfa)


{
dfa->cur_state = &q1;
dfa->run = run_dfa;

// State initialization
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, true, q2Pred);
}

Output

You might also like