Docsity

Implementing Regular Expressions - Lecture Slides | CMSC 330, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 330: Organization of
Programming Languages
Finite Automata
CMSC 330 2
Last Lecture
Languages
•Sets of strings
•Operations on languages
Regular expressions
•Constants
•Operators
•Precedence
CMSC 330 3
This Lecture
Finite automata
•States
•Transitions
•Examples
Types
•Deterministic (DFA)
•Non-deterministic (NFA)
CMSC 330 4
Implementing Regular Expressions
We can implement a regular expression by
turning it into a finite automaton
•A “machine” for recognizing a regular language
“String”
“String”
“String”
“String”
“String”
“String”
Yes
No
CMSC 330 5
Finite Automata
Machine starts in start or initial state
Repeat until the end of the string is reached
•Scan the next symbol sof the string
•Take transition edge labeled with s
String is accepted if automaton is in final state
when end of string reached
States
Start state Final state
Transition on 1
CMSC 330 6
Finite Automata: States
Start state
•State with incoming transition from no other state
•Can have only 1 start state
Final state
•State with double circle
•Can have 0 or more final states S1 S2
pf3
pf4
pf5

Partial preview of the text

Download Implementing Regular Expressions - Lecture Slides | CMSC 330 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 330: Organization of

Programming Languages

Finite Automata

CMSC 330 2

Last Lecture

Languages

  • Sets of strings
  • Operations on languages

Regular expressions

  • Constants
  • Operators
  • Precedence

CMSC 330 3

This Lecture

Finite automata

  • States
  • Transitions
  • Examples

Types

  • Deterministic (DFA)
  • Non-deterministic (NFA)

CMSC 330 4

Implementing Regular Expressions

We can implement a regular expression by

turning it into a finite automaton

  • A “machine” for recognizing a regular language

“String” “String”

“String”

“String”

“String”

“String”

Yes

No

CMSC 330 5

Finite Automata

Machine starts in start or initial state Repeat until the end of the string is reached

  • Scan the next symbol s of the string
  • Take transition edge labeled with s String is accepted if automaton is in final state when end of string reached

States

Start state (^) Final state

Transition on 1

CMSC 330 6

Finite Automata: States

Start state

  • State with incoming transition from no other state
  • Can have only 1 start state

Final state

  • State with double circle
  • Can have 0 or more final states S1^ S

CMSC 330 7

Finite Automaton: Example 1

accepted

CMSC 330 8

Finite Automaton: Example 2

not accepted

CMSC 330 9

What Language is This?

All strings over {0, 1} that end in 1

What is a regular expression for this language?

CMSC 330 10

Finite Automaton: Example 3

(a,b,c notation shorthand for three self loops)

acba S3 N

ε S0 Y

aa S0 Y

aabbb S1 Y

bbc S2 Y

acc S2 Y

aabcc S2 Y

accepts ?

state at end

string

CMSC 330 11

Finite Automaton: Example 3 (cont.)

What language does

this DFA accept?

abc*

S3 is a dead state

  • a nonfinal state

with no transition

to another state

CMSC 330 12

Dead State: Shorthand Notation

If a transition is omitted, assume it goes

to a dead state that is not shown

Language?

  • Strings over {0,1,2,3} with alternating even and odd digits, beginning with odd digit

is short for

CMSC 330 19

Formal Definition: Example

• Q = {S0, S1}
  • q 0 = S
  • F = {S1}
S
S
S0 S
S0 S

δ 0 1

CMSC 330 20

DFA Requirements

Can not have more than one transition leaving a

state on the same symbol

  • I.e., transition function must be a valid function

Can not have transitions with empty labels

  • Transitions must be labeled by alphabet symbols

NFAs do not have these requirements!

  • DFA is a special case of NFA

a a

ε ε-transition

CMSC 330 21

Nondeterministic Finite Automata (NFA)

An NFA is a 5-tuple (Σ, Q, q 0 , F, δ) where

  • Σ is an alphabet
  • Q is a nonempty set of states
  • q 0 ∊ Q is the start state
  • F ⊆ Q is the set of final states
  • δ ⊆ Q x (Σ ∪ {ε}) x Q specifies the NFA's transitions ¾ Transitions on ε are allowed – can optionally take these transitions without consuming any input ¾ Can have more than one transition for a given state and symbol

An NFA accepts s if there is at least one path from

its start to final state on s

CMSC 330 22

NFA for (a|b)*abb

ba

  • Has paths to either S0 or S
  • Neither is final, so rejected

babaabb

  • Has paths to different states
  • One leads to S3, so accepted

CMSC 330 23

Language?

  • (ab|aba)*

Another example DFA

CMSC 330 24

NFA for (ab|aba)*

aba

  • Has paths to states S0, S

ababa

  • Has paths to S0, S
  • Need to use ε-transition

CMSC 330 25

Relating REs to DFAs and NFAs

Regular expressions, NFAs, and DFAs accept

the same languages!

DFA NFA
RE

can transform

can transform

can transform

CMSC 330 26

Reducing Regular Expressions to NFAs

Goal: Given regular expression e, construct

NFA: = (Σ, Q, q 0 , F, δ)

  • Remember regular expressions are defined recursively from primitive RE languages
  • Invariant: |F| = 1 in our NFAs ¾ Recall F = set of final states

Base case: a

= ({a}, {S0, S1}, S0, {S1}, {(S0, a, S1)} )

CMSC 330 27

Reduction (cont.)

Base case: ε

<ε> = (ε, {S0}, S0, {S0}, ∅)

Base case: ∅

<∅> = (∅, {S0, S1}, S0, {S1}, ∅)

CMSC 330 28

Reduction: Concatenation

Induction: AB

CMSC 330 29

Reduction: Concatenation (cont.)

Induction: AB

  • = (ΣA, QA, qA, {f (^) A}, δA)
  • = (ΣB, QB, qB, {f (^) B}, δB)
  • = (ΣA∪ΣB, QA∪QB, q (^) A, {f (^) B}, δA∪ δB∪{(f (^) A, ε, q (^) B)} )

CMSC 330 30

Reduction: Union

Induction: (A|B)