Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
• Finite automata
– Deterministic Finite Automata (DFAs)
– Non-deterministic Finite Automata (NFAs)
• For convenience, we use a variation (allow user- • Last lecture: a specification for the predicate
defined abbreviations) in regular expression s ∈ L(R)
notation • But a yes/no answer is not enough !
• Instead: partition the input into tokens
• Union: A + B ≡ A|B
• Option: A + ε ≡ A? • We will adapt regular expressions to this goal
• Range: ‘a’+’b’+…+’z’ ≡ [a-z]
• Excluded range:
complement of [a-z] ≡ [^a-z]
Regular Expressions ⇒ Lexical Spec. (3) How to Handle Spaces and Comments?
Basic formal language theory result: A finite automaton is a recognizer for the
Regular expressions and finite automata both strings of a regular language
define the class of regular languages.
A finite automaton consists of
Thus, we are going to use: – A finite input alphabet Σ
• Regular expressions for specification – A set of states S
– A start state n
• Finite automata for implementation
– A set of accepting states F ⊆ S
(automatic generation of lexical analyzers)
– A set of transitions state →input state
• Transition • A state
s1 →a s2
• Is read
In state s1 on input “a” go to state s2 • The start state
• A finite automaton that accepts only “1” • A finite automaton accepting any number of 1’s
followed by a single 0
• Alphabet: {0,1}
1
1
1 0
1
0 0
• The operation of the automaton is not
1 completely defined by the input
1 – On input “11” the automaton could be in either state
0 1
• NFAs can choose
– Whether to make ε-moves
– Which of multiple transitions for a single input to
take
0
• Input: 1 0 1
• NFAs and DFAs recognize the same set of • For a given language the NFA can be simpler
languages (regular languages) than the DFA
1
0 0
NFA
• DFAs are easier to implement 0
NFA
M
Regular
expressions DFA • For ε
ε
• For AB • For A*
A ε ε
B
A
• For A + B ε
ε
B ε
ε
ε
ε A
• DFA “execution”
– If in state Si and input a, read T[i,a] = k and skip to 0 1
state Sk
S T U
– Very efficient
T T U
U T U