You are on page 1of 14

Go, change the world

RV College of
Engineering

FINITE AUTOMATA AND FORMAL


LANGUAGES (18CS52)
“Programming Finite State Machines”

By:-
KIRTI NANDAN -1RV18CS071
PRIYANK KUMAR SINGH -1RV18CS122
RV College of
Engineering PROGRAMMING FSMs Go, change the world

CONTENTS:
• Introduction to FSM (Finite State Machine)
• Relevance to our Curriculum
• Technologies & Concepts
• Applications
❑ Text Sentiment Classifier
❑ FSM for Regular Expression
• Conclusion
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Introduction:
A finite state machine is a mathematical abstraction used to design algorithms.
In simpler terms, a state machine will read a series of inputs. When it reads an input, it will switch to a different state.
Each state specifies which state to switch to, for a given input.

The circles are “states” that the machine can be in. The arrows are the transitions. So, if you are in state s and read
an ‘a’, you’ll transition to state q. If you read a ‘b’, you’ll stay in state s. So if we start on s and read the paper tape
above from left to right, we will read the ‘a’ and move to state q.
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Introduction:

A very simple example would be to determine if a page of HTML contains these tags in this order:

<html> <head> </head> <body> </body> </html>

The state machine can move to a state that shows it has read the html tag, loop until it gets to the head tag, loop until it gets to
the head close tag, and so on.

Finite state machines can also be used to represent many other systems — such as the mechanics of a parking meter, pop
machine, automated gas pump, hardware logics and embedded programming and many more.
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Introduction:

Deterministic FSM - From any state, there is only one transition for any allowed input. (example shown in previous slide)
Non-Deterministic FSM - Non-deterministic finite state machines are finite state machines where a given input from a
particular state can lead to more than one different state. (example shown beside)

Regular Expressions:
- Regular expressions and finite state machines are functionally equivalent. Anything
you can accept or match with a regular expression, can be accepted or matched with
a state machine.
- The language accepted by finite automata can be easily described by simple
expressions called Regular Expressions. It is the most effective way to represent any
Pattern matching finite state
language. machine

- E.g. – L={w| w belongs to (a,b)*}


ab, abb, aabb, abababb, etc.
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Relevance to Our Curriculum:

We have studied right from the beginning starting from the basics of Automation, Finite Automata & Regular Language.
These are the building blocks for moving further up the hierarchy of languages (as shown above) for automating more
complex logics and problem solutions.
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Relevance to Our Curriculum:

The concept of Finite State Machine (FSM) is a building block towards the understanding and
deriving of various other complex and advanced algorithms, emerged and yet emerging
technologies.

The understanding of STATES changing w.r.t. to processed inputs as we have studied in the
beginning chapters of our course, paves the way for programming many hardware logics, event
driven machines,etc.
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Technologies & Concepts


• The concept of FSM is used in many representational works for many works such as in
Software Development, this plays an important role in understanding different phases of
SDLC in the form of UML diagrams – Activity Diagram especially.
Few famous tools for such works: State Diagram Maker, LucidChart, Creatly etc.

• Industrially important Hardware and in a digital circuit, an FSM may be built using
a programmable logic device, a programmable logic controller, logic gates and flip
flops or relays. More specifically, a hardware implementation requires a register to store
state variables, a block of combinational logic that determines the state transition, and a
second block of combinational logic that determines the output of an FSM.
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Applications:
The FSMs are used in designing Compilers, Linguistics Processing, Step workflows, Game Design, Protocols Procedures
(like TCP/IP), Event-driven programming, Conversational AI and many more.

As we discussed so far about the concept of FSM and it’s concept involved in various fields. In order to demonstrate FSM as
to how the changing inputs change the “states” and eventually the outputs, we are considering two examples that we have
worked on:
❑ Text Sentiment Classifier
❑ FSM for Regular Expression
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Applications: Demonstration 1: Text Sentiment Classifier

Problem Statement in the domain of Engineering:


Sentiment analysis (opinion mining) is a text mining technique that uses machine learning and natural language processing
(NLP) to automatically analyze text for the sentiment of the writer (positive, negative, neutral, and beyond).

As a technique, sentiment analysis is both interesting and useful.


Application:
- Web Scraping (such as for Tweets)
- Analyzing Reviews – Stateful info. (Positive, Negative, Neutral)
- Many more!
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Applications: Demonstration 1: Text Sentiment Classifier


OUR WORK

We want to recognize the meaning of very small sentences with an extremely limited vocabulary and syntax:

These sentences should start with "Python is" followed by

● an adjective or

● the word "not" followed by an adjective.

e.g.

"Python is great" → positive meaning

"Python is stupid" → negative meaning

"Python is not ugly" → positive meaning

(Representation of State Chart for the above mentioned FSM)


RV College of
Engineering PROGRAMMING FSMs Go, change the world

Applications: Demonstration 2: FSM for Regular Expression (Here, R.E.= ab*c)

Problem Statement in the domain of Engineering:


To check whether the given string inputs satisfies/matches with the general R.E. stated above.

- Program is coded in Python Language.


- Return Boolean value for the above problem.
- Matches the input with the conditions state-by-state.

State Chart representation of Finite


Automata equivalent for the above
Regular Expression
RV College of
Engineering PROGRAMMING FSMs Go, change the world

Conclusion:

Even though this may not be the most efficient way to implement and build FSM but it is the most
intuitive way indeed. The edges and state transitions, translate well into if and elif statements or the
decision functions, while each state is being modeled as an independent coroutine and we still do
things in a sequential manner.

The entire execution is like a relay race where the baton of execution is being passed from one
coroutine (state) to another.
RV College of
Engineering FAFL (18CS52) Go, change the world

THANKYOU!

You might also like