0% found this document useful (0 votes)
30 views

Lab 1

The document discusses programs to simulate deterministic finite automata (DFAs) for recognizing valid C identifiers, signed integer numbers, signed real numbers, and C keywords such as "if", "else", "int", etc. It includes the header file that defines the DFA structure and functions for creating and running a DFA. For each problem, it provides the C code that implements the DFA with states and transition functions. The document presents C programs that implement DFAs to recognize different language constructs in C like identifiers, integer numbers, real numbers, and keywords. It uses a common header file to define the DFA structure and includes specific C code for each problem that declares

Uploaded by

Ayush Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Lab 1

The document discusses programs to simulate deterministic finite automata (DFAs) for recognizing valid C identifiers, signed integer numbers, signed real numbers, and C keywords such as "if", "else", "int", etc. It includes the header file that defines the DFA structure and functions for creating and running a DFA. For each problem, it provides the C code that implements the DFA with states and transition functions. The document presents C programs that implement DFAs to recognize different language constructs in C like identifiers, integer numbers, real numbers, and keywords. It uses a common header file to define the DFA structure and includes specific C code for each problem that declares

Uploaded by

Ayush Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 19

System Programming and Compiler Design Laboratory – 1

-Submitted By:
Aayush Anand
19103001
B.Tech (CSE), Final Year
Q1) Write a program to simulate the behaviour of DFA for recognizing
valid C identifiers.
=>
DFA.h
#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 <ctype.h>

#include "DFA.h"

State q1, q2, q3;

State* q1Pred(char c)
{
if (c == '_' || isalpha(c))
{
return &q2;
}
else
return &q3;
}

State* q2Pred(char c)
{
if ((isdigit(c) || isalpha(c) || c == '_') && c != ' ')
return &q2;
else
return &q3;
}

State* q3Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.run = run_dfa;
dfa.cur_state = &q1;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, true, q2Pred);
create_state(&q3, 3, false, q3Pred);
}
Output

Q2) Write a program to simulate the behaviour of DFA for recognizing


signed integer numbers.
=>
DFA.h
#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 <ctype.h>

#include "DFA.h"

State q1, q2, q3;

State* q1Pred(char c)
{
if (c == '+' || c == '-' || isdigit(c))
{
return &q2;
}
else
return &q3;
}

State* q2Pred(char c)
{
if (isdigit(c))
return &q2;
else
return &q3;
}

State* q3Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, true, q2Pred);
create_state(&q3, 3, false, q3Pred);
}

Output

Q3) Write a program to simulate the behaviour of DFA for recognizing


signed real numbers.
=>
DFA.h
#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

q3.c

#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3;


bool decimal_found = false;

State* q1Pred(char c)
{
if (c == '+' || c == '-' || isdigit(c))
{
return &q2;
}
else
return &q3;
}

State* q2Pred(char c)
{
if (isdigit(c))
{
return &q2;
}
else if (c == '.' && !decimal_found)
{
decimal_found = true;
return &q2;
}
else
return &q3;
}

State* q3Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, true, q2Pred);
create_state(&q3, 3, false, q3Pred);
}

Output

Q4) Write a program to simulate the behaviour of DFA for recognizing following valid C
keywords-
if, else, int, float, void, char, do, while.
=>
Consider the following header file (DFA.h) to be included for every subsequent program.
DFA.h

#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

i) if
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4;

State* q1Pred(char c)
{
if (c == 'i')
return &q2;
else
return &q4;
}

State* q2Pred(char c)
{
if (c == 'f')
return &q3;
else
return &q4;
}

State* q3Pred(char c)
{
if (c)
return &q4;
else
return &q3;
}

State* q4Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, true, q3Pred);
create_state(&q4, 4, false, q4Pred);
}
Output

ii) else
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4, q5, q6;

State* q1Pred(char c)
{
if (c == 'e')
return &q2;
else
return &q6;
}

State* q2Pred(char c)
{
if (c == 'l')
return &q3;
else
return &q6;
}

State* q3Pred(char c)
{
if (c == 's')
return &q4;
else
return &q6;
}

State* q4Pred(char c)
{
if (c == 'e')
return &q5;
else
return &q6;
}

State* q5Pred(char c)
{
if (c)
return &q6;
else
return &q5;
}

State* q6Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();
char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, false, q3Pred);
create_state(&q4, 4, false, q4Pred);
create_state(&q5, 5, true, q5Pred);
create_state(&q6, 6, false, q6Pred);
}
Output

iii) float
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4, q5, q6, q7;

State* q1Pred(char c)
{
if (c == 'f')
return &q2;
else
return &q7;
}

State* q2Pred(char c)
{
if (c == 'l')
return &q3;
else
return &q7;
}

State* q3Pred(char c)
{
if (c == 'o')
return &q4;
else
return &q7;
}
State* q4Pred(char c)
{
if (c == 'a')
return &q5;
else
return &q7;
}

State* q5Pred(char c)
{
if (c == 't')
return &q6;
else
return &q7;
}

State* q6Pred(char c)
{
if (c)
return &q7;
else
return &q5;
}

State* q7Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, false, q3Pred);
create_state(&q4, 4, false, q4Pred);
create_state(&q5, 5, false, q5Pred);
create_state(&q6, 6, true, q6Pred);
create_state(&q7, 7, false, q7Pred);
}
Output

iv) int
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4, q5;

State* q1Pred(char c)
{
if (c == 'i')
return &q2;
else
return &q5;
}

State* q2Pred(char c)
{
if (c == 'n')
return &q3;
else
return &q5;
}

State* q3Pred(char c)
{
if (c == 't')
return &q4;
else
return &q5;
}

State* q4Pred(char c)
{
if (c)
return &q4;
else
return &q5;
}

State* q5Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();
char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, false, q3Pred);
create_state(&q4, 4, true, q4Pred);
create_state(&q5, 5, true, q5Pred);
}
Output

v) void
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4, q5, q6;

State* q1Pred(char c)
{
if (c == 'v')
return &q2;
else
return &q6;
}

State* q2Pred(char c)
{
if (c == 'o')
return &q3;
else
return &q6;
}

State* q3Pred(char c)
{
if (c == 'i')
return &q4;
else
return &q6;
}

State* q4Pred(char c)
{
if (c == 'd')
return &q5;
else
return &q6;
}

State* q5Pred(char c)
{
if (c)
return &q6;
else
return &q5;
}

State* q6Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, false, q3Pred);
create_state(&q4, 4, false, q4Pred);
create_state(&q5, 5, true, q5Pred);
create_state(&q6, 6, false, q6Pred);
}
Output

vi) char
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4, q5, q6;

State* q1Pred(char c)
{
if (c == 'c')
return &q2;
else
return &q6;
}

State* q2Pred(char c)
{
if (c == 'h')
return &q3;
else
return &q6;
}

State* q3Pred(char c)
{
if (c == 'a')
return &q4;
else
return &q6;
}

State* q4Pred(char c)
{
if (c == 'r')
return &q5;
else
return &q6;
}

State* q5Pred(char c)
{
if (c)
return &q6;
else
return &q5;
}

State* q6Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


printf(result ? "Accepted": "Rejected");
return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, false, q3Pred);
create_state(&q4, 4, false, q4Pred);
create_state(&q5, 5, true, q5Pred);
create_state(&q6, 6, false, q6Pred);
}
Output

vii) do
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4;

State* q1Pred(char c)
{
if (c == 'd')
return &q2;
else
return &q4;
}

State* q2Pred(char c)
{
if (c == 'o')
return &q3;
else
return &q4;
}

State* q3Pred(char c)
{
if (c)
return &q4;
else
return &q3;
}

State* q4Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, true, q3Pred);
create_state(&q4, 4, false, q4Pred);
}
Output

viii) while
=>
#include <stdio.h>
#include <ctype.h>

#include "DFA.h"

State q1, q2, q3, q4, q5, q6, q7;

State* q1Pred(char c)
{
if (c == 'w')
return &q2;
else
return &q7;
}

State* q2Pred(char c)
{
if (c == 'h')
return &q3;
else
return &q7;
}

State* q3Pred(char c)
{
if (c == 'i')
return &q4;
else
return &q7;
}

State* q4Pred(char c)
{
if (c == 'l')
return &q5;
else
return &q7;
}

State* q5Pred(char c)
{
if (c == 'e')
return &q6;
else
return &q7;
}

State* q6Pred(char c)
{
if (c)
return &q7;
else
return &q5;
}

State* q7Pred(char c)
{
// dead state
return NULL;
}

void init_states(void);

int main()
{
DFA dfa;
dfa.cur_state = &q1;
dfa.run = run_dfa;

init_states();

char i_str[100] = { 0 };
printf("Enter input string: ");
scanf("%s", i_str);

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


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

return 0;
}

void init_states()
{
create_state(&q1, 1, false, q1Pred);
create_state(&q2, 2, false, q2Pred);
create_state(&q3, 3, false, q3Pred);
create_state(&q4, 4, false, q4Pred);
create_state(&q5, 5, false, q5Pred);
create_state(&q6, 6, true, q6Pred);
create_state(&q7, 7, false, q7Pred);
}
Output

You might also like