Syntax Specification: Regular Expressions
Syntax Specification: Regular Expressions
Regular Expressions
1
Phases of Compilation
2
Syntax Analysis
• Syntax:
– Webster’s definition: 1 a : the way in which linguistic
elements (as words) are put together to form constituents
(as phrases or clauses)
• The syntax of a programming language
– Describes its form
» i.e. Organization of tokens (elements)
– Formal notation
» Context Free Grammars (CFGs)
3
Review: Formal definition of tokens
4
Regular Expressions
5
Regular Expressions Basics
6
Building Regular Expressions
7
Description Regular Expression
8
Regular Expression Description
Same alphabet
• (aa)* even number of a’s
• (a b) (a b) (a b) (a b)
all strings of length 4
• ((a b) (a b) (a b) (a b))*
strings of length divisible by 4
• (aa)* ((a b) (a b) (a b) (a b))*
strings of a’s of length divisible by 4
9
Token Definition Example
digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
10
Exercise
digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
number …
11
Exercise
digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
12
Exercise
digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
identifier …
13
Exercise
digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
letter a | b | c | … | z
14