Introduction to Programming -...

12
Alex Aiken Compilers Finite Automata

Transcript of Introduction to Programming -...

Page 1: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Compilers

Finite Automata

Page 2: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• Regular expressions = specification • Finite automata = implementation

• A finite automaton consists of

– An input alphabet – A set of states S – A start state n – A set of accepting states F S – A set of transitions state input state

Page 3: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• Transition

s1 a s2

• Is read

In state s1 on input a go to state s2

• If end of input and in accepting state => accept

• Otherwise => reject

Page 4: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• A state

• The start state

• An accepting state

• A transition a

Page 5: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• A finite automaton that accepts only “1”

Page 6: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• A finite automaton accepting any number of 1’s followed by a single 0

• Alphabet: {0,1}

Page 7: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Template vertLeft1

0

1

0

1

0

1 (0 + 1)*

(1* + 0)(1 + 0)

1* + (01)* + (001)* + (000*1)*

(0 + 1)*00

Select the regular language that denotes the same language as this finite automaton

Page 8: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• Another kind of transition: -moves

A B

Page 9: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• Deterministic Finite Automata (DFA)

– One transition per input per state

– No -moves

• Nondeterministic Finite Automata (NFA)

– Can have multiple transitions for one input in a given state

– Can have -moves

Page 10: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• A DFA takes only one path through the state graph

• An NFA can choose

Page 11: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• An NFA can get into multiple states

• Input:

• States:

0

1

0

0

1 0 0

Page 12: Introduction to Programming - OpenClassroomopenclassroom.stanford.edu/MainFolder/courses/Compilers/...Alex Aiken Finite Automata •Deterministic Finite Automata (DFA) –One transition

Alex Aiken

Finite Automata

• NFAs and DFAs recognize the same set of languages

– regular languages

• DFAs are faster to execute

– There are no choices to consider

• NFAs are, in general, smaller