ToA - Lecture 05 06 - Regular Expressions Finite Automata

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

Lecture – 05 & 06

Regular Expressions & Finite Automata


Building Regular Expressions
• Let be a regular expression and the language represented by is then:
Building Regular Expressions – Example
• is a binary string which does not contain two consecutive or two consecutive anywhere
• Four cases for :
• Case A: starts with and is even –
• Case B: starts with and is even –
• Case C: starts with and is odd –
• Case D: starts with and is odd –
• Since is the union of all four cases:
• Reg Exp for
• If we introduce then the regular expression can be simplified to:
• Reg Exp for
Precedence of Operators
• Parentheses may be used wherever needed to influence the grouping of operators.
• Order of precedence
• operator (star)
• . (concatenation)
• + operator
• Example:
Regular Expressions – Examples

• all strings of 0’s and 1’s without two consecutive 1’s


• all strings of 0’s and 1’s having 101 as a substring
• all strings of and having several that are multiple of .
Algebraic Laws of Regular Expressions
• Commutative

• Associative:

• Identity:

• Annihilator:
Algebraic Laws of Regular Expressions
• Distributive

• Idempotent

• Involving Kleene closures:


True or False?
• Let R and S be two regular expressions then:
• ?
• ?
• ?
Finite Automata (FA)
• Informally, a state diagram that comprehensively captures all possible states and transitions
that a machine can take while responding to a stream or sequence of input symbols
• Recognizer for “Regular Languages”
• Deterministic Finite Automata (DFA)
• The machine can exist in only one state at any given time
• Non-deterministic Finite Automata (NFA)
• The machine can exist in multiple states at the same time
Deterministic Finite Automata (DFA) – Definition
• A Deterministic Finite Automaton (DFA) consists of:
• a finite set of states
• a finite set of input symbols (alphabet)
• a start state
• set of accepting states
• a transition function, which is a mapping between
• A DFA is defined by the 5-tuple:
What does a DFA do on reading an input string?
• Input: a word w in
• Question: Is acceptable by the DFA?
• Steps:
• Start at the “start state”
• For every input symbol in the sequence do
• Compute the next state from the current state, given the current input symbol in and the transition function
• If after all symbols in are consumed, the current state is one of the accepting states () then accept ;
Otherwise, reject .

• Let be a language recognized by a DFA then is called a “Regular Language”


The Transition Function
• Takes two arguments: a state and an input symbol.
• = the state that the DFA goes to when it is in state and input is received.
Graph Representation of DFA’s
• Nodes = states
• Arcs represent transition function
• Arc from state to state labeled by all those input symbols that have transitions from to .
• Arrow labeled “Start” to the start state, Final states indicated by double circles

0 0,1
1 1
A B C

Start 0
Alternative Representation – Transition Table
• Columns = Input Symbols
• Arrow for Start States
• Final States Starred
• Rows = States
0 1
*A A B
*B A C
C C C
Extended Transition Function
• We describe the effect of a string of inputs on a DFA by extending to a state and a string
• Induction on length of string
• Basis:
• Induction:
• is a string; is an input symbol.

• Convention:
• w, x, y are strings.
• a, b, c are single symbols.
• Extended is computed for state and inputs by following a path in the transition graph,
starting at and selecting the arcs with labels in turn.
Extended Transition Function

• So, we can directly write

0 1
*A A B
*B A C
C C C
Delta-Hat
• Some people denote the extended with a “hat” to distinguish it from itself.
• Not needed, because both agree when the string is a single symbol.
DFA Example – Substring
• Build a DFA for the following language:
• L = is a binary string that contains as a substring
• Steps for building a DFA to recognize :

• Decide on the states:
• Designate start state and final/accepting state(s)
• : Decide on the transitions
DFA Example – Substring

• Start State =
• F=

0 1
• Regular Expression:

*
DFA Example – Clamping Logic
• Clamping Logic
• A clamping circuit waits for a ”” input and turns on forever. However, to avoid clamping on spurious
noise, we’ll design a DFA that waits for two consecutive in a row before clamping on.
• L = is a bit string which contains as a substring
• State Design:
• : start state (initially off), also means that the most recent input was not
• : has never seen but the most recent input was
• : has seen at least once
DFA Example – Even and
• Build a DFA for the following language:
• L = is a binary string that has even number of and even number of
Extension of transitions () to paths ()
• = destination state from state on input string

• Work out last example using the input sequence w=10010, a=1:
Language of a DFA
• A DFA accepts string if there is a path from to an accepting (or final) state that is labeled by

• all strings that lead to an accepting state from


Regular Languages
• A language is regular if it is the language accepted by some DFA.
• The DFA must accept only the strings in , no others.
• Some languages are not regular.
• Intuitively, regular languages “cannot count” to arbitrarily high integers.
• (The set of strings consisting of n followed by n such that n is at least )

• in and is balanced

• But many languages are Regular


• Regular Languages can be described in many ways, e.g., regular expressions.
• They appear in many contexts and have many useful properties.
• Example: the strings that represent floating point numbers in your favorite language is a regular language.
Regular Languages – Examples
• andviewed as a binary integer is divisible by
• The DFA:
• states, named .
• Correspond to the remainders of an integer divided by .
• Start and only final state is .
• and , viewed as the reverse of a binary integer is divisible by
• Example: is in , because its reverse, is in binary.
• Hard to construct the DFA.
• But theorem says the reverse of a regular language is also regular.
Thank You 
Any Questions?

You might also like