28
CSE450 Translation of Programming Languages Automata, Simple Language Design Principles

lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

CSE450 Translation of Programming LanguagesAutomata, Simple Language Design Principles

Page 2: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Finite Automata State Graphs

A state:

The start state:

An accepting state:

A transition:a

Page 3: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

A Simple ExampleA finite automaton that accepts only "1":

1

Page 4: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Another Simple ExampleAlphabet: {0, 1}

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

0

1

Page 5: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

And Another ExampleAlphabet: {0, 1}

What language does this recognize?

0

1

0

0

1

1

Page 6: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Another Simple ExampleAlphabet still {0, 1}

1

1

The operation of the automaton is not completely defined by the input On input "11" the automaton could be in either state

Page 7: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Epsilon MovesAnother kind of transition: ε-moves

ε

Page 8: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Deterministic and Non-deterministic Automata

Deterministic Finite Automata (DFA) One transition per input per state No ε-moves

Non-deterministic Finite Automata (NFA) Can have multiple transitions for one input in a given state Can have ε-moves

Finite automata have finite memory Need only to encode the current state

Page 9: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Execution of Finite Automata

A DFA can take only one path through the state graph

Completely determined by the input

NFSs can choose

Whether to make ε-moves

Which of multiple transitions for a single input to take

Page 10: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Acceptance of NFAsAn NFA can get into multiple states

0

1

0

0Input: 100 Rule: NFA accepts if it can get in a final state.

Page 11: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

NFA vs. DFA (1)

NFAs and DFAs recognize the same set of languages (regular languages)

NFAs are easier to design You can just list all the rules you want to allow

DFAs are easier to implement There are no choices to consider

Page 12: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

For a given language the NFA can be simpler than the DFA

NFA

DFA

DFA can be exponentially larger than the NFA

NFA vs. DFA (2)

0

1

0

0

1

1

0

1

0

0

Page 13: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Lexical Analysis Overview

Lexical Specification Table-driven Implementation of DFA

DeterministicFinite Automata

Non-deterministicFinite Automata

Regular Expressions

Page 14: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

For each kind of regular expression, we can define an NFA. We wil do this by using recursive definition.

Notation: NFA for regular expression A:

Base cases:

For ε

For input 'a'

Regular Expressions to NFA (1)

A

ε

a

Page 15: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

For AB

For A | B

Regular Expressions to NFA (2)

A B

ε A

Bεε

ε

Page 16: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

For A*

How do we do "A+"?

What about "A?"?

Regular Expressions to NFA (3)

εA

ε

ε

Page 17: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Example of RE to NFA ConversionConsider the regular expression (1 | 0)* 1

The NFA is:

A JI0

ε

B HG

EC

FD

1

ε ε 1

ε

ε

εε

ε

Page 18: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Lexical Analysis Overview

Lexical Specification Table-driven Implementation of DFA

DeterministicFinite Automata

Non-deterministicFinite Automata

Regular Expressions

Page 19: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

NFA to DFA: The TrickSimulate the NFA Each state of DFA = a non-empty subset of states of the NFA Start state = the set of NFA states reachable through ε-moves from the NFA

start state Add a transition (S, a) ➜ S' to DFA if and only if:

S' is the set of NFA states reachable from the states in S after seeing the input a (remember to consider ε-moves as well!)

Page 20: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

A JI0

ε

B HG

EC

FD

1

ε ε 1

ε

ε

εε

ε

NFA to DFA Example

ABCDHI

FGABCDHI

EJGABCDHI

0

10 1

1

0

Page 21: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

NFA to DFA Remark

An NFA may be in many states at any time

How many different possible states are there?

If there are n states, the NFA must be in some subset of those n states

How many non-empty subsets are there?

2n - 1 = finitely many

Page 22: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Lexical Analysis Overview

Lexical Specification Table-driven Implementation of DFA

DeterministicFinite Automata

Non-deterministicFinite Automata

Regular Expressions

Page 23: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Table Implementation

A DFA can be implemented by a 2-dimensional table T One dimension is "states" Other dimension is "input symbols" For every transition (Si, a) ➜ Sk define T[i, a] = k

DFA "execution" If in state Si and input a, read T[i, a] = k and go to state Sk Very efficient

Page 24: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Table Implementation of DFA

S

T

U

0

10 1

1

0

0 1S T UT T UU T U

Page 25: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Final notes about Implementation

NFA to DFA conversion is at the heart of tools such as Lex

But, DFAs can be huge

In practice, lex-like tools trade off speed for space in the choice of NFA and DFA representations

Page 26: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Simple Language Design Principles

Page 27: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

Notes on Language Design

In 1986, Jon Bentley wrote a Programming Pearls column in Communications of the ACM about "Little Languages". In it, he lists seven criteria for designing a little language.

1. Orthogonality: Different statements in the language should each have their own purpose.

2. Generality: Each command in the language should be as versatile as possible as long as it does not make it much more complex.

Page 28: lecture-17-finite-automata - Michigan State University · 2019-12-18 · Deterministic Finite Automata (DFA) One transition per input per state No ε-moves ... Lexical Analysis Overview

3. Parsimony: Do not use more commands in your language that it really needs to have.

4. Completeness: Make sure your language can do everything you need it to.

5. Similarity: Use language components that the user will understand.

6. Extensibility: Your language should be easy to add new commands and features to.

7. Openness: You should be able to interface your language with other available languages.