0% found this document useful (0 votes)
4 views3 pages

Toturial

The document outlines the implementation of a finite automaton-based regular expression matcher in C for text processing. It includes structures for states and transitions, functions to create states, add transitions, and match strings against a regular expression. An example is provided to demonstrate matching the string 'abbbc' with the regular expression 'ab*c'.

Uploaded by

Siva Balan
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)
4 views3 pages

Toturial

The document outlines the implementation of a finite automaton-based regular expression matcher in C for text processing. It includes structures for states and transitions, functions to create states, add transitions, and match strings against a regular expression. An example is provided to demonstrate matching the string 'abbbc' with the regular expression 'ab*c'.

Uploaded by

Siva Balan
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/ 3

18CSC350T – Comprehension

Tutorial

Set B
Tutorial on Formal Language and Automata: A company wants to design a regular
expression engine in C for text processing tasks. Explain how you would implement a finite
automatonbased regular expression matcher to searc

Program:

#include <stdio.h>

#include <stdbool.h>

// Define structure for finite automaton state

typedef struct State {

int id;

bool isAccepting;

struct Transition* transitions;

} State;

// Define structure for transition between states

typedef struct Transition {

char input;

struct State* targetState;

struct Transition* next;

} Transition;

// Define structure for regular expression matcher

typedef struct RegexMatcher {

struct State* startState;

} RegexMatcher;

// Function to initialize a state


State* createState(int id, bool isAccepting) {

State* state = (State*)malloc(sizeof(State));

state->id = id;

state->isAccepting = isAccepting;

state->transitions = NULL;

return state;

// Function to add a transition to a state

void addTransition(State* fromState, char input, State* toState) {

Transition* transition = (Transition*)malloc(sizeof(Transition));

transition->input = input;

transition->targetState = toState;

transition->next = fromState->transitions;

fromState->transitions = transition;

// Function to match a string with the regular expression

bool match(RegexMatcher* matcher, const char* str) {

State* currentState = matcher->startState;

while (*str != '\0') {

Transition* transition = currentState->transitions;

while (transition != NULL) {

if (transition->input == *str) {

currentState = transition->targetState;

break;

transition = transition->next;

if (transition == NULL) {

return false; // No transition found for input character


}

str++;

return currentState->isAccepting; // Check if the final state is accepting

int main() {

// Example: Regular expression "ab*c"

State* q0 = createState(0, false);

State* q1 = createState(1, false);

State* q2 = createState(2, true);

addTransition(q0, 'a', q1);

addTransition(q1, 'b', q2);

addTransition(q1, 'c', q2); // Allow transition to accept "c" as well (optional)

RegexMatcher matcher;

matcher.startState = q0;

// Test matching

const char* testStr = "abbbc";

if (match(&matcher, testStr)) {

printf("'%s' matches the regular expression.\n", testStr);

} else {

printf("'%s' does not match the regular expression.\n", testStr);

return 0;

Output:

'abbbc' matches the regular expression.

You might also like