0% found this document useful (0 votes)
17 views12 pages

Compiler Assesment 1

The document outlines an assessment task for implementing a Deterministic Finite Automaton (DFA) using C and LEX code. It includes the aim, algorithm, source code, and input/output examples for the DFA that accepts strings based on specific criteria. The conclusion confirms the successful implementation of the DFA as per the given requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

Compiler Assesment 1

The document outlines an assessment task for implementing a Deterministic Finite Automaton (DFA) using C and LEX code. It includes the aim, algorithm, source code, and input/output examples for the DFA that accepts strings based on specific criteria. The conclusion confirms the successful implementation of the DFA as per the given requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Name: Amritansh Sharma

Reg No: 22BCE1479

Subject: Compiler Design Lab

Task: ASSESSMENT-1

____________________
Dr. Sureshkumar WI
Aim:

• To implement a DFA from a given regular grammar using the C programming language.

• To implement a DFA using LEX code that accepts strings with an odd number of 0's
and an even number of 1's.

• To implement a DFA using LEX code that accepts strings over the alphabet {0, 1}
ending with "11".

Algorithm:

a)

1. Define the states, alphabet, and transition functions.

2. Initialize the DFA with the start state and accepting states.

3. Read the input string character by character.

4. Transition between states based on the input and transition functions.

5. Check if the final state after processing the input string is an accepting state.

b)

1. Define the states and transitions for odd 0's and even 1's and another for ending with
two 1’s.

2. Use LEX to specify the regular expressions and corresponding actions.

3. Track the count of 0's and 1's and transition between states accordingly.

4. Accept the string if it ends in the state corresponding to odd 0's and even 1's and for
the other case ending with two 1’s .

Source Code:

a)

#include <stdio.h>

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

#define MAX_STATES 100

#define MAX_ALPHABET 26

#define MAX_STATE_NAME_LENGTH 10

typedef struct {

int numStates;

int numFinalStates;

char states[MAX_STATES][MAX_STATE_NAME_LENGTH];

char finalStates[MAX_STATES][MAX_STATE_NAME_LENGTH];

int transition[MAX_STATES][MAX_ALPHABET];

char startState[MAX_STATE_NAME_LENGTH];

char alphabet[MAX_ALPHABET];

} DFA;

void initializeDFA(DFA* dfa) {

dfa->numStates = 0;

dfa->numFinalStates = 0;

memset(dfa->transition, -1, sizeof(dfa->transition));

int getAlphabetIndex(DFA* dfa, char symbol) {

for (int i = 0; i < strlen(dfa->alphabet); i++) {

if (dfa->alphabet[i] == symbol) {

return i;
}

return -1;

int getStateIndex(DFA* dfa, char* state) {

for (int i = 0; i < dfa->numStates; i++) {

if (strcmp(dfa->states[i], state) == 0) {

return i;

return -1;

void inputDFA(DFA* dfa) {

printf("Enter number of states: ");

scanf("%d", &dfa->numStates);

printf("Enter the state names:\n");

for (int i = 0; i < dfa->numStates; i++) {

scanf("%s", dfa->states[i]);

printf("Enter alphabet symbols: ");

scanf("%s", dfa->alphabet);
printf("Enter number of final states: ");

scanf("%d", &dfa->numFinalStates);

printf("Enter final states:\n");

for (int i = 0; i < dfa->numFinalStates; i++) {

scanf("%s", dfa->finalStates[i]);

printf("Enter start state: ");

scanf("%s", dfa->startState);

printf("Enter transition rules:\n");

char fromState[MAX_STATE_NAME_LENGTH], toState[MAX_STATE_NAME_LENGTH];

for (int i = 0; i < dfa->numStates; i++) {

for (int j = 0; j < strlen(dfa->alphabet); j++) {

printf("δ(%s, %c) = ", dfa->states[i], dfa->alphabet[j]);

scanf("%s", toState);

int toStateIndex = getStateIndex(dfa, toState);

dfa->transition[i][j] = toStateIndex;

void printTransitionTable(DFA* dfa) {

printf("\nTransition Table:\n");

printf("State\\Symbol\t");
for (int i = 0; i < strlen(dfa->alphabet); i++) {

printf("%c\t", dfa->alphabet[i]);

printf("\n");

for (int i = 0; i < dfa->numStates; i++) {

printf("%s\t\t", dfa->states[i]);

for (int j = 0; j < strlen(dfa->alphabet); j++) {

if (dfa->transition[i][j] != -1) {

printf("%s\t", dfa->states[dfa->transition[i][j]]);

} else {

printf("NULL\t");

printf("\n");

printf("\n");

int isAccepted(DFA* dfa, char* inputString) {

int currentStateIndex = getStateIndex(dfa, dfa->startState);

for (int i = 0; i < strlen(inputString); i++) {

int alphabetIndex = getAlphabetIndex(dfa, inputString[i]);

if (alphabetIndex == -1) {

return 0;

}
currentStateIndex = dfa->transition[currentStateIndex][alphabetIndex];

if (currentStateIndex == -1) {

return 0;

for (int i = 0; i < dfa->numFinalStates; i++) {

if (strcmp(dfa->states[currentStateIndex], dfa->finalStates[i]) == 0) {

return 1;

return 0;

int main() {

DFA dfa;

initializeDFA(&dfa);

inputDFA(&dfa);

printTransitionTable(&dfa);

char inputString[100];

while(1) {

printf("Enter the string to be checked: ");

scanf("%s", inputString);
if(strcmp(inputString, "end") == 0) {

return 0;

if (isAccepted(&dfa, inputString)) {

printf("The string is accepted by the DFA.\n");

} else {

printf("The string is not accepted by the DFA.\n");

return 0;

b) i)

%{

%}

%s Q1 Q2 Q3 DEAD

%%

<INITIAL>1 BEGIN Q1;

<INITIAL>0 BEGIN Q2;

<INITIAL>[^01\n] BEGIN DEAD;

<INITIAL>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<Q1>1 BEGIN INITIAL;


<Q1>0 BEGIN Q3;

<Q1>[^01\n] BEGIN DEAD;

<Q1>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<Q2>1 BEGIN Q3;

<Q2>0 BEGIN INITIAL;

<Q2>[^01\n] BEGIN DEAD;

<Q2>\n BEGIN INITIAL; {printf("Accepted\n");}

<Q3>1 BEGIN Q2;

<Q3>0 BEGIN Q1;

<Q3>[^01\n] BEGIN DEAD;

<Q3>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<DEAD>[^\n] BEGIN DEAD;

<DEAD>\n BEGIN INITIAL; {printf("Invalid\n");}

%%

int main()

printf("Enter String\n");

yylex();

return 0;

b) ii)
%{

%}

%s Q1 Q2 DEAD

%%

<INITIAL>1 BEGIN Q1;

<INITIAL>0 BEGIN INITIAL;

<INITIAL>[^01\n] BEGIN DEAD;

<INITIAL>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<Q1>1 BEGIN Q2;

<Q1>0 BEGIN INITIAL;

<Q1>[^01\n] BEGIN DEAD;

<Q1>\n BEGIN INITIAL; {printf("Not Accepted\n");}

<Q2>1 BEGIN Q2;

<Q2>0 BEGIN INITIAL;

<Q2>[^01\n] BEGIN DEAD;

<Q2>\n BEGIN INITIAL; {printf("Accepted\n");}

<DEAD>[^\n] BEGIN DEAD;

<DEAD>\n BEGIN INITIAL; {printf("Invalid\n");}

%%
int main()

printf("Enter String\n");

yylex();

return 0;

Input and Output: a)

b) i)
b) ii)

Conclusion:

• Successfully implemented a DFA in C that transitions according to the given regular


grammar and correctly identifies if a string is accepted or rejected.

• Successfully implemented a DFA in LEX that accepts strings with an odd number of
0's and an even number of 1's.

• Successfully implemented a DFA in LEX that accepts strings over {0, 1} ending with
"11".

You might also like