Regular Expressions
Sanjit A. Seshia
EECS, UC Berkeley
Acknowledgments: L.von Ahn, L. Blum, M. Blum
The Picture So Far
DFA
NFA
Regular language S. A. Seshia
2
1
Today’s Lecture
DFA
Regular language NFA
Regular expression S. A. Seshia
3
Regular Expressions
• What is a regular expression?
S. A. Seshia
4
2
Regular Expressions
• Q. What is a regular expression?
• A. It’s a “textual”/ “algebraic” representation of a regular language
– A DFA can be viewed as a “pictorial” /
“explicit” representation
• We will prove that a regular expressions
(regexps) indeed represent regular languages
S. A. Seshia
5
Regular Expressions: Definition σ is a regular expression representing {σ} σ (σ∈Σ) ε is a regular expression representing {ε}
∅ is a regular expression representing ∅
If R1 and R2 are regular expressions representing L1 and L2 then:
(R1R2) represents L1⋅L2
(R1 ∪ R2) represents L1 ∪ L2
(R1)* represents L1*
S. A. Seshia
6
3
Operator Precedence
1.
2.
3.
*
⋅
∪
( often left out; a·b ab )
S. A. Seshia
7
Example of Precedence
R1*R2 ∪ R3 = ( ( R1* ) R2 ) ∪ R3
S. A. Seshia
8
4
What’s the regexp?
{ w | w has exactly a single 1 }
0*10*
S. A. Seshia
9
What language does
∅* represent?
{ε}
S. A. Seshia
10
5
What’s the regexp?
{ w | w has length ≥ 3 and its 3rd symbol is 0 }
Σ2 0 Σ *
Σ = (0 ∪ 1)
S. A. Seshia
11
Some Identities
Let R, S, T be regular expressions
• R∪∅ = ?
• R·∅ =?
• Prove: R ( S ∪ T ) = R S ∪ R T
(what’s the proof idea?)
S. A. Seshia
12
6
Some Applications of
Regular Expressions
• String matching & searching
– Utilities like grep, awk, …
– Search in editors: emacs, …
• Programming Languages
– Perl
– Compiler design: lex/yacc
• Computer Security
– Virus signatures
S. A. Seshia
13
Virus Signature as String
…
pop