Transcript
Page 1: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES, FINITE AUTOMATA, ANDLEXICAL ANALYSISPRINCIPLES OF PROGRAMMING LANGUAGES

Norbert ZehWinter 2018

Dalhousie University

1/65

Page 2: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROGRAM TRANSLATION FLOW CHART

Scanner (lexical analysis)

Parser (syntactic analysis)

Semantic analysis andcode generation

Source program(Character stream)

Token stream

Parse treeFrontend

Machine-independentcode improvement

Target code generation

Machine-specificcode improvement

Modifiedintermediate form

Target language(e.g., assembly)

Modifiedtarget language

Backend

Abstract syntax tree orother intermediate form

Symboltable

2/65

Page 3: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

LEXICAL ANALYSIS

GoalTransform input text into much more compact token stream (keywords,parentheses, operators, identifiers, …).

class DictEntry {int key ; // The keyfloat value ; // The associated value

} ;

kwClass identifier ‘{’identifier identifier ‘;’identifier identifier ‘;’

‘}’ ‘;’

Tools

• Regular expressions• Finite automata: Very simple and efficient machines just powerful enoughto carry out lexical analysis

3/65

Page 4: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

LEXICAL ANALYSIS

GoalTransform input text into much more compact token stream (keywords,parentheses, operators, identifiers, …).

class DictEntry {int key ; // The keyfloat value ; // The associated value

} ;

kwClass identifier ‘{’identifier identifier ‘;’identifier identifier ‘;’

‘}’ ‘;’

Tools

• Regular expressions• Finite automata: Very simple and efficient machines just powerful enoughto carry out lexical analysis

3/65

Page 5: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

LEXICAL ANALYSIS

GoalTransform input text into much more compact token stream (keywords,parentheses, operators, identifiers, …).

class DictEntry {int key ; // The keyfloat value ; // The associated value

} ;

kwClass identifier ‘{’identifier identifier ‘;’identifier identifier ‘;’

‘}’ ‘;’

Tools

• Regular expressions• Finite automata: Very simple and efficient machines just powerful enoughto carry out lexical analysis

3/65

Page 6: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

LEXICAL ANALYSIS

GoalTransform input text into much more compact token stream (keywords,parentheses, operators, identifiers, …).

class DictEntry {int key ; // The keyfloat value ; // The associated value

} ;

kwClass identifier ‘{’identifier identifier ‘;’identifier identifier ‘;’

‘}’ ‘;’

Tools

• Regular expressions• Finite automata: Very simple and efficient machines just powerful enoughto carry out lexical analysis

3/65

Page 7: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

4/65

Page 8: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

4/65

Page 9: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular Language

Base cases:

∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 10: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases:

∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 11: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅,

{ϵ}, and {a}

are regular languages

, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 12: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅, {ϵ},

and {a}

are regular languages

, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 13: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 14: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 15: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,

• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 16: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 17: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language:

This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 18: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR LANGUAGES

Definition: Regular LanguageBase cases: ∅, {ϵ}, and {a} are regular languages, where a ∈ Σ.

Induction: If A and B are regular languages, then

• A ∪ B is a regular language,• AB = {ab | a ∈ A,b ∈ B} is a regular language,(ab is the concatenation of a and b)

• A∗ = A0 ∪ A1 ∪ A2 ∪ · · · is a regular language: This is the only wayto produce infiniteregular languages!

• A0 = {ϵ}

• Ai = {σ1σ2 | σ1 ∈ Ai−1, σ2 ∈ A}

5/65

Page 19: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 20: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}

• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 21: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!

• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 22: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 23: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 24: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 25: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}

• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 26: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation

• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 27: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}

• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 28: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}

• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 29: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}

• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 30: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}

• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 31: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}

• All syntactically correct C programs

6/65

Page 32: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}

• All syntactically correct C programs

6/65

Page 33: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 34: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR LANGUAGES

• {a,b, ab}• Any finite language!• {0}∗

• {0, 1}∗

• {a,b, c}∗

• {01n0 | n ≥ 0}• Set of all positive integers in decimal representation• {0m1n | m ≥ 0,n ≥ 0}• {akbmcn | k ≥ 0,m ≥ 0,n ≥ 0}• {(n)n | n ≥ 0}• {ap | p is a prime number}• All syntactically correct C programs

6/65

Page 35: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

7/65

Page 36: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

7/65

Page 37: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expression

Base cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 38: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expression

Base cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 39: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expressionBase cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 40: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expressionBase cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 41: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expressionBase cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,

• AB is a regular expression,• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 42: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expressionBase cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,

• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 43: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expressionBase cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,• (A) is a regular expression,

• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 44: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expressionBase cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 45: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS

… are a notation for specifying regular languages.

Definition: Regular expressionBase cases: ∅, ϵ, and a are regular expressions, where a ∈ Σ.

Induction: If A and B are regular expressions, then

• A|B is a regular expression,• AB is a regular expression,• (A) is a regular expression,• A∗ is a regular expression.

Interpretation:

• Precedence: Kleene star (*), Concatenation, Union (|)• Parentheses indicate grouping

8/65

Page 46: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}R = A|B =⇒ L(R) = L(A) ∪ L(B)R = AB =⇒ L(R) = L(A)L(B)R = (A) =⇒ L(R) = L(A)R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 47: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅

R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}R = A|B =⇒ L(R) = L(A) ∪ L(B)R = AB =⇒ L(R) = L(A)L(B)R = (A) =⇒ L(R) = L(A)R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 48: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}R = A|B =⇒ L(R) = L(A) ∪ L(B)R = AB =⇒ L(R) = L(A)L(B)R = (A) =⇒ L(R) = L(A)R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 49: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}

R = A|B =⇒ L(R) = L(A) ∪ L(B)R = AB =⇒ L(R) = L(A)L(B)R = (A) =⇒ L(R) = L(A)R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 50: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}R = A|B =⇒ L(R) = L(A) ∪ L(B)

R = AB =⇒ L(R) = L(A)L(B)R = (A) =⇒ L(R) = L(A)R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 51: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}R = A|B =⇒ L(R) = L(A) ∪ L(B)R = AB =⇒ L(R) = L(A)L(B)

R = (A) =⇒ L(R) = L(A)R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 52: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}R = A|B =⇒ L(R) = L(A) ∪ L(B)R = AB =⇒ L(R) = L(A)L(B)R = (A) =⇒ L(R) = L(A)

R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 53: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS MODEL REGULAR LANGUAGES

Every regular expression R defines a corresponding regular language L(R):

R = ∅ =⇒ L(R) = ∅R = ϵ =⇒ L(R) = {ϵ}

R = a =⇒ L(R) = {a}R = A|B =⇒ L(R) = L(A) ∪ L(B)R = AB =⇒ L(R) = L(A)L(B)R = (A) =⇒ L(R) = L(A)R = A∗ =⇒ L(R) = L(A)∗

9/65

Page 54: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗

all binary strings(0|1)∗0 all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 55: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings

(0|1)∗0 all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 56: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0

all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 57: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0 all binary strings that end in 0

(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 58: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0 all binary strings that end in 0(0|1)00∗

all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 59: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0 all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s

0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 60: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0 all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗

all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 61: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0 all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 62: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0 all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 63: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF REGULAR EXPRESSIONS

(0|1)∗ all binary strings(0|1)∗0 all binary strings that end in 0(0|1)00∗ all binary strings that start with 0 or 1, followed by one or more 0s0|1(0|1)∗ all binary numbers without leading 0s

What regular expression describes the set L of binary strings that do notcontain 101 as a substring?

• 1001001110 ∈ L• 00010010100 /∈ L

(0|ϵ)(1|000∗)∗(0|ϵ)

10/65

Page 64: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R• R? = (R|): zero or one repetition of R• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 65: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R• R? = (R|): zero or one repetition of R• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 66: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R• R? = (R|): zero or one repetition of R• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 67: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R• R? = (R|): zero or one repetition of R• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 68: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R

• R? = (R|): zero or one repetition of R• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 69: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R• R? = (R|): zero or one repetition of R

• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 70: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R• R? = (R|): zero or one repetition of R• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 71: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (1)

No ϵ or ∅:

• The empty string is represented as the empty string: a(b|) instead of a(b|ϵ)to express the language {a, ab}.

• The empty language is not very useful in practice.

Additional repetition constructs:

• R+ = RR∗: one or more repetitions of R• R? = (R|): zero or one repetition of R• R{n}, R{,n}, R{m, }, R{m,n}: n, up to n, at least m, between m and n repetitionsof R

Some capabilities beyond regular languages:

• Allow, for example, recognition of languages such as αβα, for α,β ∈ Σ∗.

11/65

Page 72: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))

→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗

→ [a–zA–Z_][a–zA–Z0–9_]∗

• Anything but a lowercase letter: [^a–z]• Any letter: .• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 73: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))

→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗

→ [a–zA–Z_][a–zA–Z0–9_]∗

• Anything but a lowercase letter: [^a–z]• Any letter: .• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 74: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗

→ [a–zA–Z_][a–zA–Z0–9_]∗• Anything but a lowercase letter: [^a–z]• Any letter: .• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 75: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗→ [a–zA–Z_][a–zA–Z0–9_]∗

• Anything but a lowercase letter: [^a–z]• Any letter: .• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 76: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗→ [a–zA–Z_][a–zA–Z0–9_]∗• Anything but a lowercase letter: [^a–z]

• Any letter: .• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 77: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗→ [a–zA–Z_][a–zA–Z0–9_]∗• Anything but a lowercase letter: [^a–z]• Any letter: .

• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 78: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗→ [a–zA–Z_][a–zA–Z0–9_]∗• Anything but a lowercase letter: [^a–z]• Any letter: .• Digit, non-digit: \d, \D

• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 79: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗→ [a–zA–Z_][a–zA–Z0–9_]∗• Anything but a lowercase letter: [^a–z]• Any letter: .• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S

• Word character, non-word character: \w, \W

12/65

Page 80: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS IN PRACTICE (2)

Character classes allow us to write tedious expressions such as a|b| · · · |z moreeasily.

Examples:

• “Recent” years: 199(6|7|8|9)|20(0(0|1|2|3|4|5|6|7|8|9)|1(0|1|2|3|4|5|6|7|8))→ 199[6–9]|20(0[0–9]|1[0–8])• Identifier in C: (a|b| · · · |z|A|B| · · · |Z|_)(a|b| · · · |z|A|B| · · · |Z|0|1| · · · |9|_)∗→ [a–zA–Z_][a–zA–Z0–9_]∗• Anything but a lowercase letter: [^a–z]• Any letter: .• Digit, non-digit: \d, \D• Whitespace, non-whitespace: \s, \S• Word character, non-word character: \w, \W

12/65

Page 81: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 82: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’

• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 83: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’

• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 84: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’

• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 85: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’

• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 86: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’

• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 87: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’

• …

13/65

Page 88: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 89: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

REGULAR EXPRESSIONS FOR LEXICAL ANALYSIS

• Special characters: ‘(’ ‘)’ ‘{’ ‘}’ ‘[’ ‘]’ ‘,’ ‘;’ ‘:’• Mathematical operators: ‘+|-|*|/|=|+=|-=|*=|/=’• Keywords (C++): ‘class|struct|union|if|else|for|while|…’• Integer: ‘[+-]?\d+’• Float: ‘[+-]?\d*\.?\d+([Ee][+-]?\d+)?’• Identifier (C++): ‘[a-zA-Z_][a-zA-Z0-9_]*’• …

13/65

Page 90: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

14/65

Page 91: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

14/65

Page 92: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ 0 1

S→ s1 s1 s2∗s2 s2 s1

Graph representation:

s1start s21

0

1

0

15/65

Page 93: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ 0 1

S→ s1 s1 s2∗s2 s2 s1

Graph representation:

s1start s21

0

1

0

15/65

Page 94: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ 0 1

S→ s1 s1 s2∗s2 s2 s1

Graph representation:

s1start s21

0

1

0

15/65

Page 95: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ 0 1

S

s1

s1 s2

∗s2

s2 s1

Graph representation:

s1

s1start

s2

10

1

0

15/65

Page 96: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ

0 1

S→

s1

s1 s2

∗s2

s2 s1

Graph representation:

s1

s1start

s2

10

1

0

15/65

Page 97: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ 0 1

S→

s1 s1 s2∗s2 s2 s1

Graph representation:

s1

s1start

s21

0

1

0

15/65

Page 98: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ 0 1

S

→ s1 s1 s2∗s2 s2 s1

Graph representation:

s1start s21

0

1

0

15/65

Page 99: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

DETERMINISTIC FINITE AUTOMATON (DFA)

… is a simple type of machine that can be used to decide regular languages.

Definition:Deterministic finite automaton (DFA)A tuple D = (S, Σ, δ, s0, F):

• Set S of states• Finite alphabet Σ• Transition function δ : S× Σ→ S• Initial state s0 ∈ S• Set of final states F ⊆ S

Tabular representation:

Σ

δ 0 1

S

→ s1 s1 s2∗s2 s2 s1

Graph representation:

s1start

s2

s21

0

1

0

15/65

Page 100: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 101: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 102: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 103: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 104: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 105: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 106: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 107: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 108: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 109: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 110: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 111: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 112: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 113: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 114: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 115: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 116: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 117: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 118: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 119: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 120: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 121: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 122: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with

16/65

Page 123: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with …

16/65

Page 124: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with an odd number of 1s.

16/65

Page 125: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (1)

Acceptance/rejection of a string:IntuitionA DFA D = (S, Σ, δ, s0, F) accepts astring σ ∈ Σ∗ if, after starting instate s0 and reading σ, it finishesin an accepting state. Otherwise, itrejects σ.

Language decided by a DFAL(D) = {σ ∈ Σ∗ | D accepts σ}

s1start s21

0

1

0

I’ve seen an evennumber of 1s so far.

I’ve seen an oddnumber of 1s so far.

0 0 0 1 0 1 1 0

1 0 1 1 0 0 1 0

This DFA decides the language of allbinary strings with an odd number of 1s.

16/65

Page 126: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for strings

δ∗ : S× Σ∗ → Sδ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s• δ∗(s, xσ) = δ∗(δ(s, x), σ)

δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 127: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for strings

δ∗ : S× Σ∗ → Sδ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s• δ∗(s, xσ) = δ∗(δ(s, x), σ)

δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 128: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for stringsδ∗ : S× Σ∗ → S

δ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s• δ∗(s, xσ) = δ∗(δ(s, x), σ)

δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 129: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for stringsδ∗ : S× Σ∗ → Sδ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s• δ∗(s, xσ) = δ∗(δ(s, x), σ)

δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 130: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for stringsδ∗ : S× Σ∗ → Sδ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s

• δ∗(s, xσ) = δ∗(δ(s, x), σ)

δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 131: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for stringsδ∗ : S× Σ∗ → Sδ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s• δ∗(s, xσ) = δ∗(δ(s, x), σ)

δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 132: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for stringsδ∗ : S× Σ∗ → Sδ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s• δ∗(s, xσ) = δ∗(δ(s, x), σ)δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 133: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY A DFA (2)

A transition function for stringsδ∗ : S× Σ∗ → Sδ∗(s, σ): the state reached after consuming σ if we start in state s.

• δ∗(s, ϵ) = s• δ∗(s, xσ) = δ∗(δ(s, x), σ)δ∗(s, x1x2 . . . xn) = δ∗(δ(s, x1), x2x3 . . . xn)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

17/65

Page 134: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF DFA

• {σ ∈ {0, 1}∗ | σ does not contain the substring 101}

s1start s2 s3 s4

0

1

1

0

0

1 0, 1

I have not seen any partof a 101 substring

The current string ends in 1 The current string ends in 10

I’ve seen 101 as a substring“Death trap”

(Non-accepting state we cannot leave)

• Valid C comments (/*…*/)

s1start s2 s3 s4 s5/ *

*[^*]

/

*

[^*/]

I haven’t seen anything

I have seen ‘/’,may be start of a comment.

I have seen ‘/*’,inside the comment now.

I have seen ‘*’,may be end of comment.

Done reading the comment.Accept if end of string;reject otherwise.

18/65

Page 135: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF DFA

• {σ ∈ {0, 1}∗ | σ does not contain the substring 101}

s1start s2 s3 s4

0

1

1

0

0

1 0, 1

I have not seen any partof a 101 substring

The current string ends in 1 The current string ends in 10

I’ve seen 101 as a substring“Death trap”

(Non-accepting state we cannot leave)• Valid C comments (/*…*/)

s1start s2 s3 s4 s5/ *

*[^*]

/

*

[^*/]

I haven’t seen anything

I have seen ‘/’,may be start of a comment.

I have seen ‘/*’,inside the comment now.

I have seen ‘*’,may be end of comment.

Done reading the comment.Accept if end of string;reject otherwise.

18/65

Page 136: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF DFA

• {σ ∈ {0, 1}∗ | σ does not contain the substring 101}

s1start s2 s3 s4

0

1

1

0

0

1 0, 1

I have not seen any partof a 101 substring

The current string ends in 1 The current string ends in 10

I’ve seen 101 as a substring

“Death trap”(Non-accepting state we cannot leave)• Valid C comments (/*…*/)

s1start s2 s3 s4 s5/ *

*[^*]

/

*

[^*/]

I haven’t seen anything

I have seen ‘/’,may be start of a comment.

I have seen ‘/*’,inside the comment now.

I have seen ‘*’,may be end of comment.

Done reading the comment.Accept if end of string;reject otherwise.

18/65

Page 137: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF DFA

• {σ ∈ {0, 1}∗ | σ does not contain the substring 101}

s1start s2 s3 s4

0

1

1

0

0

1 0, 1

I have not seen any partof a 101 substring

The current string ends in 1 The current string ends in 10

I’ve seen 101 as a substring

“Death trap”(Non-accepting state we cannot leave)

• Valid C comments (/*…*/)

s1start s2 s3 s4 s5/ *

*[^*]

/

*

[^*/]

I haven’t seen anything

I have seen ‘/’,may be start of a comment.

I have seen ‘/*’,inside the comment now.

I have seen ‘*’,may be end of comment.

Done reading the comment.Accept if end of string;reject otherwise.

18/65

Page 138: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF DFA

• {σ ∈ {0, 1}∗ | σ does not contain the substring 101}

s1start s2 s3

s4

0

1

1

0

0

1 0, 1

I have not seen any partof a 101 substring

The current string ends in 1 The current string ends in 10

I’ve seen 101 as a substring“Death trap”

(Non-accepting state we cannot leave)• Valid C comments (/*…*/)

s1start s2 s3 s4 s5/ *

*[^*]

/

*

[^*/]

I haven’t seen anything

I have seen ‘/’,may be start of a comment.

I have seen ‘/*’,inside the comment now.

I have seen ‘*’,may be end of comment.

Done reading the comment.Accept if end of string;reject otherwise.

18/65

Page 139: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF DFA

• {σ ∈ {0, 1}∗ | σ does not contain the substring 101}

s1start s2 s3

s4

0

1

1

0

0

1 0, 1

I have not seen any partof a 101 substring

The current string ends in 1 The current string ends in 10

I’ve seen 101 as a substring“Death trap”

(Non-accepting state we cannot leave)

• Valid C comments (/*…*/)

s1start s2 s3 s4 s5/ *

*[^*]

/

*

[^*/]

I haven’t seen anything

I have seen ‘/’,may be start of a comment.

I have seen ‘/*’,inside the comment now.

I have seen ‘*’,may be end of comment.

Done reading the comment.Accept if end of string;reject otherwise.

18/65

Page 140: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXAMPLES OF DFA

• {σ ∈ {0, 1}∗ | σ does not contain the substring 101}

s1start s2 s3

s4

0

1

1

0

0

1 0, 1

I have not seen any partof a 101 substring

The current string ends in 1 The current string ends in 10

I’ve seen 101 as a substring“Death trap”

(Non-accepting state we cannot leave)

• Valid C comments (/*…*/)

s1start s2 s3 s4 s5/ *

*[^*]

/

*

[^*/]I haven’t seen anything

I have seen ‘/’,may be start of a comment.

I have seen ‘/*’,inside the comment now.

I have seen ‘*’,may be end of comment.

Done reading the comment.Accept if end of string;reject otherwise.

18/65

Page 141: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

19/65

Page 142: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

19/65

Page 143: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (1)

For a DFA D = (S, Σ, δ, s0, F), reading a string σ puts the DFA into a unique stateδ∗(s0, σ). That’s why it’s called “deterministic”.

A non-deterministic finite automaton (NFA) has the ability to choose betweenmultiple states to transition to after reading a character and may even“spontaneously” transition to a new state without reading anything.

⇒ δ∗(s0, σ) is potentially one of many states.Formally, δ∗(s0, σ) is a set of states.

An NFA N = (S, Σ, δ, s0, F) accepts σ if δ∗(s0, σ) ∩ F ̸= ∅.(N has the ability to reach an accepting state while reading σ, assuming it makesthe right choices.)

20/65

Page 144: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (1)

For a DFA D = (S, Σ, δ, s0, F), reading a string σ puts the DFA into a unique stateδ∗(s0, σ). That’s why it’s called “deterministic”.

A non-deterministic finite automaton (NFA) has the ability to choose betweenmultiple states to transition to after reading a character and may even“spontaneously” transition to a new state without reading anything.

⇒ δ∗(s0, σ) is potentially one of many states.Formally, δ∗(s0, σ) is a set of states.

An NFA N = (S, Σ, δ, s0, F) accepts σ if δ∗(s0, σ) ∩ F ̸= ∅.(N has the ability to reach an accepting state while reading σ, assuming it makesthe right choices.)

20/65

Page 145: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (1)

For a DFA D = (S, Σ, δ, s0, F), reading a string σ puts the DFA into a unique stateδ∗(s0, σ). That’s why it’s called “deterministic”.

A non-deterministic finite automaton (NFA) has the ability to choose betweenmultiple states to transition to after reading a character and may even“spontaneously” transition to a new state without reading anything.

⇒ δ∗(s0, σ) is potentially one of many states.

Formally, δ∗(s0, σ) is a set of states.

An NFA N = (S, Σ, δ, s0, F) accepts σ if δ∗(s0, σ) ∩ F ̸= ∅.(N has the ability to reach an accepting state while reading σ, assuming it makesthe right choices.)

20/65

Page 146: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (1)

For a DFA D = (S, Σ, δ, s0, F), reading a string σ puts the DFA into a unique stateδ∗(s0, σ). That’s why it’s called “deterministic”.

A non-deterministic finite automaton (NFA) has the ability to choose betweenmultiple states to transition to after reading a character and may even“spontaneously” transition to a new state without reading anything.

⇒ δ∗(s0, σ) is potentially one of many states.Formally, δ∗(s0, σ) is a set of states.

An NFA N = (S, Σ, δ, s0, F) accepts σ if δ∗(s0, σ) ∩ F ̸= ∅.(N has the ability to reach an accepting state while reading σ, assuming it makesthe right choices.)

20/65

Page 147: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (1)

For a DFA D = (S, Σ, δ, s0, F), reading a string σ puts the DFA into a unique stateδ∗(s0, σ). That’s why it’s called “deterministic”.

A non-deterministic finite automaton (NFA) has the ability to choose betweenmultiple states to transition to after reading a character and may even“spontaneously” transition to a new state without reading anything.

⇒ δ∗(s0, σ) is potentially one of many states.Formally, δ∗(s0, σ) is a set of states.

An NFA N = (S, Σ, δ, s0, F) accepts σ if δ∗(s0, σ) ∩ F ̸= ∅.(N has the ability to reach an accepting state while reading σ, assuming it makesthe right choices.)

20/65

Page 148: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (2)

Definition:Non-deterministic finite automaton (NFA)A tuple D = (S, Σ, δ, s0, F):

• Set of states S• Finite alphabet Σ• Transition function δ : S× (Σ ∪ {ϵ})→ 2S

• Initial state s0 ∈ S• Set of final states F ⊆ S

s1start s2 s3

0,1

0 1 s1start s2 s3 s4

0,1

ϵ 0 1

21/65

Page 149: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (2)

Definition:Non-deterministic finite automaton (NFA)A tuple D = (S, Σ, δ, s0, F):

• Set of states S• Finite alphabet Σ• Transition function δ : S× (Σ ∪ {ϵ})→ 2S

• Initial state s0 ∈ S• Set of final states F ⊆ S

s1start s2 s3

0,1

0 1 s1start s2 s3 s4

0,1

ϵ 0 1

21/65

Page 150: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (2)

Definition:Non-deterministic finite automaton (NFA)A tuple D = (S, Σ, δ, s0, F):

• Set of states S• Finite alphabet Σ• Transition function δ : S× (Σ ∪ {ϵ})→ 2S

• Initial state s0 ∈ S• Set of final states F ⊆ S

δ 0 1 ϵ→ s1 {s1, s2} {s1} ∅s2 ∅ {s3} ∅

∗s3 ∅ ∅ ∅

s1start s2 s3

0,1

0 1

s1start s2 s3 s4

0,1

ϵ 0 1

21/65

Page 151: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NON-DETERMINISTIC FINITE AUTOMATON (NFA) (2)

Definition:Non-deterministic finite automaton (NFA)A tuple D = (S, Σ, δ, s0, F):

• Set of states S• Finite alphabet Σ• Transition function δ : S× (Σ ∪ {ϵ})→ 2S

• Initial state s0 ∈ S• Set of final states F ⊆ S

δ 0 1 ϵ→ s1 {s1} {s1} {s2}s2 {s3} ∅ ∅s3 ∅ {s4} ∅

∗s4 ∅ ∅ ∅

s1start s2 s3

0,1

0 1 s1start s2 s3 s4

0,1

ϵ 0 1

21/65

Page 152: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2

s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 153: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2

s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 154: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 155: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 156: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 157: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 158: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 159: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 160: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 161: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 162: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 163: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2

s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 164: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 165: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 166: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 167: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 168: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 169: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 170: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 171: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 172: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 173: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (1)

s1start

s1start

s2

s2 s3

s3

0,1

0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

22/65

Page 174: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3

s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 175: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3

s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 176: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2 s3

s3

s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 177: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 178: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 179: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 180: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 181: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 182: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 183: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 184: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 185: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 186: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 187: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 188: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 189: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 190: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 191: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 192: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1

0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 193: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3

s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 194: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2 s3

s3

s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 195: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 196: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 197: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 198: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 199: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 200: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 201: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 202: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 203: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 204: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 205: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 206: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 207: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 208: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 209: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start s2

s2 s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 210: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 211: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 212: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (2)

s1start

s1start

s2

s2

s3

s3 s4

s4

0,1

ϵ 0 1

0 0 1 0 1 0 0 1 0 0 1 0 1 0 1 0

L(N) = L((0|1)∗01)

23/65

Page 213: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (3)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

What should this definition look like for an NFA?

• What is δ∗(s0, σ)?

The set of states reachable from s0 by reading σ.

• How should it be related to F for N to accept σ?

24/65

Page 214: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (3)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

What should this definition look like for an NFA?

• What is δ∗(s0, σ)?

The set of states reachable from s0 by reading σ.

• How should it be related to F for N to accept σ?

24/65

Page 215: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (3)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

What should this definition look like for an NFA?

• What is δ∗(s0, σ)?

The set of states reachable from s0 by reading σ.

• How should it be related to F for N to accept σ?

24/65

Page 216: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (3)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

What should this definition look like for an NFA?

• What is δ∗(s0, σ)?

The set of states reachable from s0 by reading σ.

• How should it be related to F for N to accept σ?

24/65

Page 217: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (3)

Language decided by a DFAFor a DFA D = (S, Σ, δ, s0, F),

L(D) = {σ ∈ Σ∗ | δ∗(s0, σ) ∈ F}.

What should this definition look like for an NFA?

• What is δ∗(s0, σ)?

The set of states reachable from s0 by reading σ.

• How should it be related to F for N to accept σ?

24/65

Page 218: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (3)

Language decided by an NFAFor an NFA N = (S, Σ, δ, s0, F),

L(N) = {σ ∈ Σ∗ | δ∗(s0, σ) ∩ F ̸= ∅}.

What should this definition look like for an NFA?

• What is δ∗(s0, σ)?

The set of states reachable from s0 by reading σ.

• How should it be related to F for N to accept σ?

24/65

Page 219: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ϵ-CLOSURE

Definition: ϵ-ClosureFor some subset S ′ ⊆ S of states, ECLOSE(S ′) is the set of all states that can bereached from states in S ′ using only ϵ-transitions.

Formally, ECLOSE(S ′) is the smallest superset ECLOSE(S ′) ⊇ S ′ such thatδ(s, ϵ) ⊆ ECLOSE(S ′) for all s ∈ ECLOSE(S ′).

s0start s1 s2 s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

25/65

Page 220: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ϵ-CLOSURE

Definition: ϵ-ClosureFor some subset S ′ ⊆ S of states, ECLOSE(S ′) is the set of all states that can bereached from states in S ′ using only ϵ-transitions.

Formally, ECLOSE(S ′) is the smallest superset ECLOSE(S ′) ⊇ S ′ such thatδ(s, ϵ) ⊆ ECLOSE(S ′) for all s ∈ ECLOSE(S ′).

s0start s1 s2 s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

25/65

Page 221: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ϵ-CLOSURE

Definition: ϵ-ClosureFor some subset S ′ ⊆ S of states, ECLOSE(S ′) is the set of all states that can bereached from states in S ′ using only ϵ-transitions.

Formally, ECLOSE(S ′) is the smallest superset ECLOSE(S ′) ⊇ S ′ such thatδ(s, ϵ) ⊆ ECLOSE(S ′) for all s ∈ ECLOSE(S ′).

s0start s1 s2 s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

25/65

Page 222: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (4)

A transition function for stringsδ∗(s, σ) = the set of states reachable from s by reading σ.

• δ∗(s, ϵ) =

ECLOSE({s})

• δ∗(s, xσ) =

∪s1∈ECLOSE({s})

∪s2∈δ(s1,x) δ

∗(s2, σ)

δ∗(s, ϵ) s

σxϵ

δ∗(s, xσ)

26/65

Page 223: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (4)

A transition function for stringsδ∗(s, σ) = the set of states reachable from s by reading σ.

• δ∗(s, ϵ) =

ECLOSE({s})• δ∗(s, xσ) =

∪s1∈ECLOSE({s})

∪s2∈δ(s1,x) δ

∗(s2, σ)

s

ϵ

δ∗(s, ϵ) s

σxϵ

δ∗(s, xσ)

26/65

Page 224: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (4)

A transition function for stringsδ∗(s, σ) = the set of states reachable from s by reading σ.

• δ∗(s, ϵ) = ECLOSE({s})

• δ∗(s, xσ) =

∪s1∈ECLOSE({s})

∪s2∈δ(s1,x) δ

∗(s2, σ)

δ∗(s, ϵ)

s

σxϵ

δ∗(s, xσ)

26/65

Page 225: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (4)

A transition function for stringsδ∗(s, σ) = the set of states reachable from s by reading σ.

• δ∗(s, ϵ) = ECLOSE({s})• δ∗(s, xσ) =

∪s1∈ECLOSE({s})

∪s2∈δ(s1,x) δ

∗(s2, σ)

δ∗(s, ϵ) s

σxϵ

δ∗(s, xσ)

26/65

Page 226: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (4)

A transition function for stringsδ∗(s, σ) = the set of states reachable from s by reading σ.

• δ∗(s, ϵ) = ECLOSE({s})• δ∗(s, xσ) =

∪s1∈ECLOSE({s})

∪s2∈δ(s1,x) δ

∗(s2, σ)

δ∗(s, ϵ) s

σx

ϵ

δ∗(s, xσ)

26/65

Page 227: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (4)

A transition function for stringsδ∗(s, σ) = the set of states reachable from s by reading σ.

• δ∗(s, ϵ) = ECLOSE({s})• δ∗(s, xσ) =

∪s1∈ECLOSE({s})

∪s2∈δ(s1,x)

δ∗(s2, σ)

δ∗(s, ϵ) s

σ

δ∗(s, xσ)

26/65

Page 228: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

THE LANGUAGE DECIDED BY AN NFA (4)

A transition function for stringsδ∗(s, σ) = the set of states reachable from s by reading σ.

• δ∗(s, ϵ) = ECLOSE({s})• δ∗(s, xσ) =

∪s1∈ECLOSE({s})

∪s2∈δ(s1,x) δ

∗(s2, σ)

δ∗(s, ϵ) s

σxϵ

δ∗(s, xσ)

26/65

Page 229: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

27/65

Page 230: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

27/65

Page 231: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (1)

• All binary strings that have 101 as a substring.

s1start s2 s3 s4

0

1

1

0 1

0

0, 1DFA:

s1start s2 s3 s4

.

1 0 1

.NFA:

• All binary strings that do not have 101 as a substring.

s1start s2 s3

0

1

1

0

0

28/65

Page 232: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (1)

• All binary strings that have 101 as a substring.

s1start s2 s3 s4

0

1

1

0 1

0

0, 1DFA:

s1start s2 s3 s4

.

1 0 1

.NFA:

• All binary strings that do not have 101 as a substring.

s1start s2 s3

0

1

1

0

0

28/65

Page 233: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (1)

• All binary strings that have 101 as a substring.

s1start s2 s3 s4

0

1

1

0 1

0

0, 1DFA:

s1start s2 s3 s4

.

1 0 1

.NFA:

• All binary strings that do not have 101 as a substring.

s1start s2 s3

0

1

1

0

0

28/65

Page 234: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (2)

A more compelling example: L(.∗1..)

NFA

DFA

s1start s2 s3 s4

.

1 . .

s1start s2

s3

s4

s5

s6

s7

s8

0

10

1

0 1

0

1

01

0

1 1

0

01

29/65

Page 235: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (2)

A more compelling example: L(.∗1..)

NFA

DFA

s1start s2 s3 s4

.

1 . .

s1start s2

s3

s4

s5

s6

s7

s8

0

10

1

0 1

0

1

01

0

1 1

0

01

29/65

Page 236: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (2)

A more compelling example: L(.∗1..)

NFA

DFA

s1start s2 s3 s4

.

1 . .

s1start s2

s3

s4

s5

s6

s7

s8

0

10

1

0 1

0

1

01

0

1 1

0

01

29/65

Page 237: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (3)

When testing for the presence of patterns, NFA are more convenient than DFA.They only have to guess right where the pattern starts! This does not work fortesting for their absence.

Testing for the presence of patterns is the common case in parsing programminglanguages (keywords, identifiers, …).

But … computers are not good at guessing!→ We need to construct DFA.

30/65

Page 238: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (3)

When testing for the presence of patterns, NFA are more convenient than DFA.They only have to guess right where the pattern starts! This does not work fortesting for their absence.

Testing for the presence of patterns is the common case in parsing programminglanguages (keywords, identifiers, …).

But … computers are not good at guessing!→ We need to construct DFA.

30/65

Page 239: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (3)

When testing for the presence of patterns, NFA are more convenient than DFA.They only have to guess right where the pattern starts! This does not work fortesting for their absence.

Testing for the presence of patterns is the common case in parsing programminglanguages (keywords, identifiers, …).

But … computers are not good at guessing!

→ We need to construct DFA.

30/65

Page 240: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NFA CAN BE MORE CONVENIENT THAN DFA (3)

When testing for the presence of patterns, NFA are more convenient than DFA.They only have to guess right where the pattern starts! This does not work fortesting for their absence.

Testing for the presence of patterns is the common case in parsing programminglanguages (keywords, identifiers, …).

But … computers are not good at guessing!→ We need to construct DFA.

30/65

Page 241: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

31/65

Page 242: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

31/65

Page 243: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ARE NFA MORE POWERFUL THAN DFA?

No!TheoremThe following statements are equivalent:

• L is a regular language.• L can be decided by a DFA.• L can be decided by an NFA.

Regular expression DFA

NFA

?

Proof outline:

• Given an NFA N, construct a DFA D with L(D) = L(N).• Given a regular expression R, construct an NFA that decides L(R).• Given an NFA N, construct a regular expression R with L(R) = L(N).

32/65

Page 244: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ARE NFA MORE POWERFUL THAN DFA?

No!

TheoremThe following statements are equivalent:

• L is a regular language.• L can be decided by a DFA.• L can be decided by an NFA.

Regular expression DFA

NFA

?

Proof outline:

• Given an NFA N, construct a DFA D with L(D) = L(N).• Given a regular expression R, construct an NFA that decides L(R).• Given an NFA N, construct a regular expression R with L(R) = L(N).

32/65

Page 245: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ARE NFA MORE POWERFUL THAN DFA?

No!TheoremThe following statements are equivalent:

• L is a regular language.• L can be decided by a DFA.• L can be decided by an NFA.

Regular expression DFA

NFA

?

Proof outline:

• Given an NFA N, construct a DFA D with L(D) = L(N).• Given a regular expression R, construct an NFA that decides L(R).• Given an NFA N, construct a regular expression R with L(R) = L(N).

32/65

Page 246: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ARE NFA MORE POWERFUL THAN DFA?

No!TheoremThe following statements are equivalent:

• L is a regular language.• L can be decided by a DFA.• L can be decided by an NFA. Regular expression DFA

NFA

?

Proof outline:

• Given an NFA N, construct a DFA D with L(D) = L(N).• Given a regular expression R, construct an NFA that decides L(R).• Given an NFA N, construct a regular expression R with L(R) = L(N).

32/65

Page 247: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ARE NFA MORE POWERFUL THAN DFA?

No!TheoremThe following statements are equivalent:

• L is a regular language.• L can be decided by a DFA.• L can be decided by an NFA. Regular expression DFA

NFA

?

Proof outline:

• Given an NFA N, construct a DFA D with L(D) = L(N).• Given a regular expression R, construct an NFA that decides L(R).• Given an NFA N, construct a regular expression R with L(R) = L(N).

32/65

Page 248: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ARE NFA MORE POWERFUL THAN DFA?

No!TheoremThe following statements are equivalent:

• L is a regular language.• L can be decided by a DFA.• L can be decided by an NFA. Regular expression DFA

NFA

?

Proof outline:

• Given an NFA N, construct a DFA D with L(D) = L(N).• Given a regular expression R, construct an NFA that decides L(R).• Given an NFA N, construct a regular expression R with L(R) = L(N).

32/65

Page 249: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

• 2S = {S ′ | S ′ ⊆ S} (set of all subsets of S)

Problem: |2S| = 2|S|→ Can we construct only the subset of states in 2S that are reachable from t0?

33/65

Page 250: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

• 2S = {S ′ | S ′ ⊆ S} (set of all subsets of S)

Problem: |2S| = 2|S|→ Can we construct only the subset of states in 2S that are reachable from t0?

33/65

Page 251: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

• 2S = {S ′ | S ′ ⊆ S} (set of all subsets of S)

Problem: |2S| = 2|S|

→ Can we construct only the subset of states in 2S that are reachable from t0?

33/65

Page 252: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

• 2S = {S ′ | S ′ ⊆ S} (set of all subsets of S)

Problem: |2S| = 2|S|→ Can we construct only the subset of states in 2S that are reachable from t0?

33/65

Page 253: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

INITIAL STATE AND ACCEPTING STATES OF THE DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

Initial state of DFA: The states the NFA can reach without consuming any input.

t0 = ECLOSE({s0})

Accepting states of the DFA: All subsets of S that include an accepting state.

G = {S ′ ⊆ S | S ′ ∩ F ̸= ∅}

34/65

Page 254: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

INITIAL STATE AND ACCEPTING STATES OF THE DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

Initial state of DFA:

The states the NFA can reach without consuming any input.

t0 = ECLOSE({s0})

Accepting states of the DFA: All subsets of S that include an accepting state.

G = {S ′ ⊆ S | S ′ ∩ F ̸= ∅}

34/65

Page 255: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

INITIAL STATE AND ACCEPTING STATES OF THE DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

Initial state of DFA: The states the NFA can reach without consuming any input.

t0 = ECLOSE({s0})

Accepting states of the DFA: All subsets of S that include an accepting state.

G = {S ′ ⊆ S | S ′ ∩ F ̸= ∅}

34/65

Page 256: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

INITIAL STATE AND ACCEPTING STATES OF THE DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

Initial state of DFA: The states the NFA can reach without consuming any input.

t0 = ECLOSE({s0})

Accepting states of the DFA: All subsets of S that include an accepting state.

G = {S ′ ⊆ S | S ′ ∩ F ̸= ∅}

34/65

Page 257: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

INITIAL STATE AND ACCEPTING STATES OF THE DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

Initial state of DFA: The states the NFA can reach without consuming any input.

t0 = ECLOSE({s0})

Accepting states of the DFA:

All subsets of S that include an accepting state.

G = {S ′ ⊆ S | S ′ ∩ F ̸= ∅}

34/65

Page 258: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

INITIAL STATE AND ACCEPTING STATES OF THE DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

Initial state of DFA: The states the NFA can reach without consuming any input.

t0 = ECLOSE({s0})

Accepting states of the DFA: All subsets of S that include an accepting state.

G = {S ′ ⊆ S | S ′ ∩ F ̸= ∅}

34/65

Page 259: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

INITIAL STATE AND ACCEPTING STATES OF THE DFA

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

Initial state of DFA: The states the NFA can reach without consuming any input.

t0 = ECLOSE({s0})

Accepting states of the DFA: All subsets of S that include an accepting state.

G = {S ′ ⊆ S | S ′ ∩ F ̸= ∅}

34/65

Page 260: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (1)

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

We want γ∗(t0, σ) = γ∗(ECLOSE({s0}), σ) = δ∗(s0, σ).

35/65

Page 261: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (1)

Idea: Each DFA state represents a set of NFA states the NFA can be in afterreading some string.

Trivial construction: N = (S, Σ, δ, s0, F)→ D = (2S, Σ, γ, t0,G)

We want γ∗(t0, σ) = γ∗(ECLOSE({s0}), σ) = δ∗(s0, σ).

35/65

Page 262: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (2)

A transition function for sets of states of the NFAδ∗(S ′, σ) = the set of states reachable from any state in S ′ by reading σ.

• δ∗(S ′, ϵ) =

ECLOSE(S ′)

• δ∗(S ′, xσ) =

∪s1∈ECLOSE(S ′)

∪s2∈δ(s1,x) δ

∗(s2, σ)

S ′

ϵ

δ∗(S ′, ϵ) S ′

σxϵ

δ∗(S ′, xσ)

36/65

Page 263: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (2)

A transition function for sets of states of the NFAδ∗(S ′, σ) = the set of states reachable from any state in S ′ by reading σ.

• δ∗(S ′, ϵ) =

ECLOSE(S ′)• δ∗(S ′, xσ) =

∪s1∈ECLOSE(S ′)

∪s2∈δ(s1,x) δ

∗(s2, σ)

S ′

ϵ

δ∗(S ′, ϵ) S ′

σxϵ

δ∗(S ′, xσ)

36/65

Page 264: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (2)

A transition function for sets of states of the NFAδ∗(S ′, σ) = the set of states reachable from any state in S ′ by reading σ.

• δ∗(S ′, ϵ) = ECLOSE(S ′)

• δ∗(S ′, xσ) =

∪s1∈ECLOSE(S ′)

∪s2∈δ(s1,x) δ

∗(s2, σ)

S ′

ϵ

δ∗(S ′, ϵ)

S ′

σxϵ

δ∗(S ′, xσ)

36/65

Page 265: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (2)

A transition function for sets of states of the NFAδ∗(S ′, σ) = the set of states reachable from any state in S ′ by reading σ.

• δ∗(S ′, ϵ) = ECLOSE(S ′)• δ∗(S ′, xσ) =

∪s1∈ECLOSE(S ′)

∪s2∈δ(s1,x) δ

∗(s2, σ)

S ′

ϵ

δ∗(S ′, ϵ) S ′

σxϵ

δ∗(S ′, xσ)

36/65

Page 266: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (2)

A transition function for sets of states of the NFAδ∗(S ′, σ) = the set of states reachable from any state in S ′ by reading σ.

• δ∗(S ′, ϵ) = ECLOSE(S ′)• δ∗(S ′, xσ) =

∪s1∈ECLOSE(S ′)

∪s2∈δ(s1,x) δ

∗(s2, σ)

S ′

ϵ

δ∗(S ′, ϵ) S ′

σx

ϵ

δ∗(S ′, xσ)

36/65

Page 267: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (2)

A transition function for sets of states of the NFAδ∗(S ′, σ) = the set of states reachable from any state in S ′ by reading σ.

• δ∗(S ′, ϵ) = ECLOSE(S ′)• δ∗(S ′, xσ) =

∪s1∈ECLOSE(S ′)

∪s2∈δ(s1,x)

δ∗(s2, σ)

S ′

ϵ

δ∗(S ′, ϵ) S ′

σ

δ∗(S ′, xσ)

36/65

Page 268: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (2)

A transition function for sets of states of the NFAδ∗(S ′, σ) = the set of states reachable from any state in S ′ by reading σ.

• δ∗(S ′, ϵ) = ECLOSE(S ′)• δ∗(S ′, xσ) =

∪s1∈ECLOSE(S ′)

∪s2∈δ(s1,x) δ

∗(s2, σ)

S ′

ϵ

δ∗(S ′, ϵ) S ′

σxϵ

δ∗(S ′, xσ)

36/65

Page 269: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 270: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 271: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 272: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3

ϵ ϵ ϵ

x1

x2 x3

ϵ

δ∗(S ′, x1x2x3)

γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 273: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3

ϵ

ϵ ϵ

x1 x2 x3ϵ

δ∗(S ′, x1x2x3)

γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 274: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3

ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)

γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 275: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3

ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)

γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1)

γ(E.(S2), x2) γ(E.(S3), x3)

S3S2

S1

37/65

Page 276: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3

ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)

γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2)

γ(E.(S3), x3)

S3

S2S1

37/65

Page 277: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3

ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)

γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 278: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

TRANSITION FUNCTION OF THE DFA (3)

Transition function of the DFALet• γ(S ′, x) =

∪s∈S ′ ECLOSE(δ(s, x)),

• γ∗(S ′, ϵ) = S ′, and• γ∗(S ′, xσ) = γ∗(γ(S ′, x), σ).

Then

δ∗(S ′, σ) = γ∗(ECLOSE(S ′), σ).

S ′

x2x3

ϵ ϵ ϵx1 x2 x3ϵ

δ∗(S ′, x1x2x3)

γ∗(ECLOSE(S ′), x1x2x3)

γ(E.(S1), x1) γ(E.(S2), x2) γ(E.(S3), x3)

S3S2S1

37/65

Page 279: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: CONSTRUCTING ONLY THE STATES WE NEED

N = (S, Σ, δ, s0, F)→ D = (T, Σ, γ, t0,G)

So far, T = 2S, but only a subset of states may be reachable from t0.→ We would like to choose T to be this subset.

Obvious idea: Construct D with T = 2S, then throw away the states not reachablefrom t0.Too costly!

Almost as obvious: Generate only the states we can reach from t0:

• Start with T = {t0} and a queue Q = {t0} of new states.• While Q ̸= ∅:

• Remove some t ∈ Q from Q.• For each x ∈ Σ, add γ(t, x) to T, and to Q if γ(t, x) was not in T before.

38/65

Page 280: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: CONSTRUCTING ONLY THE STATES WE NEED

N = (S, Σ, δ, s0, F)→ D = (T, Σ, γ, t0,G)

So far, T = 2S, but only a subset of states may be reachable from t0.→ We would like to choose T to be this subset.

Obvious idea: Construct D with T = 2S, then throw away the states not reachablefrom t0.Too costly!

Almost as obvious: Generate only the states we can reach from t0:

• Start with T = {t0} and a queue Q = {t0} of new states.• While Q ̸= ∅:

• Remove some t ∈ Q from Q.• For each x ∈ Σ, add γ(t, x) to T, and to Q if γ(t, x) was not in T before.

38/65

Page 281: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: CONSTRUCTING ONLY THE STATES WE NEED

N = (S, Σ, δ, s0, F)→ D = (T, Σ, γ, t0,G)

So far, T = 2S, but only a subset of states may be reachable from t0.→ We would like to choose T to be this subset.

Obvious idea: Construct D with T = 2S, then throw away the states not reachablefrom t0.

Too costly!

Almost as obvious: Generate only the states we can reach from t0:

• Start with T = {t0} and a queue Q = {t0} of new states.• While Q ̸= ∅:

• Remove some t ∈ Q from Q.• For each x ∈ Σ, add γ(t, x) to T, and to Q if γ(t, x) was not in T before.

38/65

Page 282: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: CONSTRUCTING ONLY THE STATES WE NEED

N = (S, Σ, δ, s0, F)→ D = (T, Σ, γ, t0,G)

So far, T = 2S, but only a subset of states may be reachable from t0.→ We would like to choose T to be this subset.

Obvious idea: Construct D with T = 2S, then throw away the states not reachablefrom t0.Too costly!

Almost as obvious: Generate only the states we can reach from t0:

• Start with T = {t0} and a queue Q = {t0} of new states.• While Q ̸= ∅:

• Remove some t ∈ Q from Q.• For each x ∈ Σ, add γ(t, x) to T, and to Q if γ(t, x) was not in T before.

38/65

Page 283: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: CONSTRUCTING ONLY THE STATES WE NEED

N = (S, Σ, δ, s0, F)→ D = (T, Σ, γ, t0,G)

So far, T = 2S, but only a subset of states may be reachable from t0.→ We would like to choose T to be this subset.

Obvious idea: Construct D with T = 2S, then throw away the states not reachablefrom t0.Too costly!

Almost as obvious: Generate only the states we can reach from t0:

• Start with T = {t0} and a queue Q = {t0} of new states.• While Q ̸= ∅:

• Remove some t ∈ Q from Q.• For each x ∈ Σ, add γ(t, x) to T, and to Q if γ(t, x) was not in T before.

38/65

Page 284: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→

{s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 285: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0}

{s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 286: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1}

∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 287: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅

{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 288: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1}

∅ {s2, s3, s6, s9}

∅ ∅{s2, s3, s6, s9} {s7} {s4, s10}

{s7} {s2, s3, s6, s8, s9} ∅{s4, s10} {s5} {s2, s3, s6, s9, s11}

{s2, s3, s6, s8, s9} {s7} {s4, s10}* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 289: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅

{s2, s3, s6, s9}

∅ ∅{s2, s3, s6, s9} {s7} {s4, s10}

{s7} {s2, s3, s6, s8, s9} ∅{s4, s10} {s5} {s2, s3, s6, s9, s11}

{s2, s3, s6, s8, s9} {s7} {s4, s10}* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 290: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅

∅ ∅{s2, s3, s6, s9} {s7} {s4, s10}

{s7} {s2, s3, s6, s8, s9} ∅{s4, s10} {s5} {s2, s3, s6, s9, s11}

{s2, s3, s6, s8, s9} {s7} {s4, s10}* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 291: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅

∅ ∅

{s2, s3, s6, s9}

{s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 292: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9}

{s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 293: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7}

{s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 294: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}

{s7} {s2, s3, s6, s8, s9} ∅{s4, s10} {s5} {s2, s3, s6, s9, s11}

{s2, s3, s6, s8, s9} {s7} {s4, s10}* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 295: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7}

{s2, s3, s6, s8, s9} ∅

{s4, s10}

{s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 296: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9}

{s4, s10}

{s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 297: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10}

{s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 298: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10}

{s5} {s2, s3, s6, s9, s11}

{s2, s3, s6, s8, s9}

{s7} {s4, s10}* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 299: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5}

{s2, s3, s6, s9, s11}

{s2, s3, s6, s8, s9}

{s7} {s4, s10}* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 300: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9}

{s7} {s4, s10}* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10}

39/65

Page 301: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9}

{s7} {s4, s10}*

{s5}

∅ ∅

{s2, s3, s6, s9, s11}

{s7} {s4, s10}

39/65

Page 302: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7}

{s4, s10}*

{s5}

∅ ∅

{s2, s3, s6, s9, s11}

{s7} {s4, s10}

39/65

Page 303: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

*

{s5}

∅ ∅

{s2, s3, s6, s9, s11}

{s7} {s4, s10}

39/65

Page 304: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

*

{s5} ∅ ∅{s2, s3, s6, s9, s11}

{s7} {s4, s10}

39/65

Page 305: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

*

{s5} ∅ ∅{s2, s3, s6, s9, s11} {s7}

{s4, s10}

39/65

Page 306: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

*

{s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10} 39/65

Page 307: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

s2s1s0start s3 s4 s5

s6 s7 s8

s9 s10 s11

0 1 ϵϵ

ϵ

1 0

0 0

1 1

ϵ

ϵ

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10} 39/65

Page 308: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXAMPLE

{s7}∅{s0}start {s4, s10} {s5}

{s1}

{s2, s3, s6, s8, s9}

{s2, s3, s6, s9}

{s2, s3, s6, s9, s11}

1

0

0|1

1

0 10

0|101

01

0 1

0 1

δ 0 1→ {s0} {s1} ∅{s1} ∅ {s2, s3, s6, s9}∅ ∅ ∅

{s2, s3, s6, s9} {s7} {s4, s10}{s7} {s2, s3, s6, s8, s9} ∅

{s4, s10} {s5} {s2, s3, s6, s9, s11}{s2, s3, s6, s8, s9} {s7} {s4, s10}

* {s5} ∅ ∅{s2, s3, s6, s9, s11} {s7} {s4, s10} 39/65

Page 309: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (1)

Our construction aims to avoid constructing an NFA with an exponential numberof states.

This works for most languages (e.g., the ones used in lexical analysis).

However, there are languages where a DFA needs exponentially more states thanan NFA:

L(.∗1.{n− 1})

The NFA has n+ 1 states:

q0start q1 q2 qn−1 qn1

.

. .

40/65

Page 310: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (1)

Our construction aims to avoid constructing an NFA with an exponential numberof states.

This works for most languages (e.g., the ones used in lexical analysis).

However, there are languages where a DFA needs exponentially more states thanan NFA:

L(.∗1.{n− 1})

The NFA has n+ 1 states:

q0start q1 q2 qn−1 qn1

.

. .

40/65

Page 311: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (1)

Our construction aims to avoid constructing an NFA with an exponential numberof states.

This works for most languages (e.g., the ones used in lexical analysis).

However, there are languages where a DFA needs exponentially more states thanan NFA:

L(.∗1.{n− 1})

The NFA has n+ 1 states:

q0start q1 q2 qn−1 qn1

.

. .

40/65

Page 312: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (1)

Our construction aims to avoid constructing an NFA with an exponential numberof states.

This works for most languages (e.g., the ones used in lexical analysis).

However, there are languages where a DFA needs exponentially more states thanan NFA:

L(.∗1.{n− 1})

The NFA has n+ 1 states:

q0start q1 q2 qn−1 qn1

.

. .

40/65

Page 313: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 314: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 315: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.

⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 316: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).

• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 317: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.

• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 318: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).

⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 319: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.

• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 320: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.

⇒ D does not decide L.

41/65

Page 321: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO DFA: EXPONENTIAL NUMBER OF STATES (2)

ClaimAny DFA that decides L = L(.∗1.{n− 1}) has at least 2n states. (Σ = {0, 1}.)

Proof:

• Assume there exists a DFA D = (S, Σ, δ, s0, F) with L(D) = L and |S| < 2n.⇒ There exist two strings σ1 ̸= σ2 ∈ Σn such that δ∗(s0, σ1) = δ∗(s0, σ2).• Since σ1 ̸= σ2, w.l.o.g. σ1 = .{m}0.{n−m− 1} and σ2 = .{m}1.{n−m− 1}.• Since δ∗(s0, σ1) = δ∗(s0, σ2), we also have δ∗(s0, σ10m) = δ∗(s0, σ20m).⇒ Either D accepts both σ10m and σ20m or D rejects both σ10m and σ20m.• However, σ10m /∈ L and σ20m ∈ L.⇒ D does not decide L.

41/65

Page 322: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 323: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 324: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 325: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 326: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ

s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 327: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 328: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ)

s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 329: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 330: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 331: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 332: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 333: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 334: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB

MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 335: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MB

ϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 336: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 337: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗

MAϵ ϵ

ϵ

ϵ

42/65

Page 338: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MA

ϵ ϵ

ϵ

ϵ

42/65

Page 339: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA

Base cases:

∅ s0start

ϵ s0start

x (x ∈ Σ) s0start s1x

Inductive steps:

A|B

MA

MB

ϵ

ϵ

AB MA MBϵ

ϵ

A∗ MAϵ ϵ

ϵ

ϵ

42/65

Page 340: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 341: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 342: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

0

43/65

Page 343: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

0

ϵϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 344: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

0

ϵϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

ϵϵ

ϵ

43/65

Page 345: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 346: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

0

43/65

Page 347: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

0

0 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 348: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

0

0 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

0

43/65

Page 349: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00

ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 350: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00

ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

ϵ ϵ

43/65

Page 351: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 352: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

1

43/65

Page 353: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

1

ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 354: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

1

ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

ϵ

ϵ

43/65

Page 355: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵ

ϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 356: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵ

ϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵ

43/65

Page 357: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 358: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

0

43/65

Page 359: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

0

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 360: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

0

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 361: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

0

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 362: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

0

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

ϵ

ϵ

43/65

Page 363: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵ

ϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 364: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵ

ϵ

ϵ

0

ϵ

ϵϵ

ϵ

ϵ

0

43/65

Page 365: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 366: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

ϵ

ϵϵ

43/65

Page 367: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM REGULAR EXPRESSION TO NFA: EXAMPLE

(0|ϵ)(1|000∗)∗(0|ϵ)

ϵ

ϵ

00 ϵ ϵ

ϵϵ

ϵ

ϵ

ϵ

ϵϵ

ϵ

0

ϵ

ϵϵ

43/65

Page 368: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (1)

Acceptance of a string by (D/N)FA: IntuitionConcatenating the labels of the edges of a pathin a (D/N)FA N = (S, Σ, δ, s0, F) produces astring, called the label of the path.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is σ.

0

1

1 0

1

0

1

Accepts 01101

Definition: Regular Expression NFA (RNFA)An RNFA N is a finite automaton whose edgesare labelled with regular expressions.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is R andσ ∈ L(R).

0|11 . .

Accepts01101 ∈ L((0|1)(0|1)1..)

44/65

Page 369: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (1)

Acceptance of a string by (D/N)FA: IntuitionConcatenating the labels of the edges of a pathin a (D/N)FA N = (S, Σ, δ, s0, F) produces astring, called the label of the path.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is σ.

0

1

1 0

1

0

1

Accepts 01101

Definition: Regular Expression NFA (RNFA)An RNFA N is a finite automaton whose edgesare labelled with regular expressions.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is R andσ ∈ L(R).

0|11 . .

Accepts01101 ∈ L((0|1)(0|1)1..)

44/65

Page 370: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (1)

Acceptance of a string by (D/N)FA: IntuitionConcatenating the labels of the edges of a pathin a (D/N)FA N = (S, Σ, δ, s0, F) produces astring, called the label of the path.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is σ.

0

1

1 0

1

0

1

Accepts 01101

Definition: Regular Expression NFA (RNFA)An RNFA N is a finite automaton whose edgesare labelled with regular expressions.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is R andσ ∈ L(R).

0|11 . .

Accepts01101 ∈ L((0|1)(0|1)1..)

44/65

Page 371: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (1)

Acceptance of a string by (D/N)FA: IntuitionConcatenating the labels of the edges of a pathin a (D/N)FA N = (S, Σ, δ, s0, F) produces astring, called the label of the path.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is σ.

0

1

1 0

1

0

1

Accepts 01101

Definition: Regular Expression NFA (RNFA)An RNFA N is a finite automaton whose edgesare labelled with regular expressions.

N accepts a string σ if there exists a path froms0 to an accepting state whose label is R andσ ∈ L(R).

0|11 . .

Accepts01101 ∈ L((0|1)(0|1)1..)

44/65

Page 372: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (2)

Proof idea:

NFA→ RFA1 → RFA2 → · · ·→ RFAnL(NFA) = L(RFA1) = L(RFA2) = · · · = L(RFAn)

RFAn has two states, an initial state and an accepting state:

s1start s2

R2R1

R3

R4

L(NFA) = L(RFAn) = L((R1|R2R4∗R3)∗R2R4∗)

45/65

Page 373: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (2)

Proof idea:

NFA→ RFA1 → RFA2 → · · ·→ RFAnL(NFA) = L(RFA1) = L(RFA2) = · · · = L(RFAn)

RFAn has two states, an initial state and an accepting state:

s1start s2

R2R1

R3

R4

L(NFA) = L(RFAn) = L((R1|R2R4∗R3)∗R2R4∗)

45/65

Page 374: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (2)

Proof idea:

NFA→ RFA1 → RFA2 → · · ·→ RFAnL(NFA) = L(RFA1) = L(RFA2) = · · · = L(RFAn)

RFAn has two states, an initial state and an accepting state:

s1start s2

R2R1

R3

R4

L(NFA) = L(RFAn) = L((R1|R2R4∗R3)∗R2R4∗)

45/65

Page 375: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (3)

NFA → RFA1:

• L(NFA) = L(RFA1)• RFA1 has one initial and one accepting state.

NFA

ϵ

ϵ

46/65

Page 376: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (3)

NFA → RFA1:

• L(NFA) = L(RFA1)• RFA1 has one initial and one accepting state.

NFA

ϵ

ϵ

46/65

Page 377: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (3)

NFA → RFA1:

• L(NFA) = L(RFA1)• RFA1 has one initial and one accepting state.

NFAϵ

ϵ

46/65

Page 378: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (4)

RFAk → RFAk+1:

• L(RFAk) = L(RFAk+1)• RFAk+1 has one state less than RFAk.

s

qirj

qirj

Note: This may create loops because some states may simultaneously be in- andout-neighbours of s.

47/65

Page 379: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (4)

RFAk → RFAk+1:

• L(RFAk) = L(RFAk+1)• RFAk+1 has one state less than RFAk.

s

qirj

qirj

Note: This may create loops because some states may simultaneously be in- andout-neighbours of s.

47/65

Page 380: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (4)

RFAk → RFAk+1:

• L(RFAk) = L(RFAk+1)• RFAk+1 has one state less than RFAk.

s

qirj

Qi

Tij

Rj

S

qirjTij|QiS∗Rj

Note: This may create loops because some states may simultaneously be in- andout-neighbours of s.

47/65

Page 381: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION (4)

RFAk → RFAk+1:

• L(RFAk) = L(RFAk+1)• RFAk+1 has one state less than RFAk.

s

qirj

Qi

Tij

Rj

S

qirjTij|QiS∗Rj

Note: This may create loops because some states may simultaneously be in- andout-neighbours of s.

47/65

Page 382: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION: EXAMPLE

L((0|ϵ)(1|000∗)∗(0|ϵ)): All strings that do not contain 101 as a substring

s0start s1 s2

f

s0start s1 s2

0

11∗00|0

1

1

0

0

00

ϵ11∗(ϵ|0)|ϵ ϵϵ|0 ϵ

Regular expression: (11∗00|0)∗(11∗(ϵ|0)|ϵ)

48/65

Page 383: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION: EXAMPLE

L((0|ϵ)(1|000∗)∗(0|ϵ)): All strings that do not contain 101 as a substring

s0start s1 s2

f

s0start s1 s2

0

11∗00|0

1

1

0

0

00

ϵ

11∗(ϵ|0)|ϵ

ϵ

ϵ|0

ϵ

Regular expression: (11∗00|0)∗(11∗(ϵ|0)|ϵ)

48/65

Page 384: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION: EXAMPLE

L((0|ϵ)(1|000∗)∗(0|ϵ)): All strings that do not contain 101 as a substring

s0start s1 s2

f

s0start s1 s2

0

11∗00|0

1

1

0

0

00

ϵ

11∗(ϵ|0)|ϵ

ϵ

ϵ|0

ϵ

Regular expression: (11∗00|0)∗(11∗(ϵ|0)|ϵ)

48/65

Page 385: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION: EXAMPLE

L((0|ϵ)(1|000∗)∗(0|ϵ)): All strings that do not contain 101 as a substring

s0start s1

s2

f

s0start s1 s2

0

11∗00|0

1

1

0

0

00

ϵ

11∗(ϵ|0)|ϵ ϵ

ϵ|0

ϵ

Regular expression: (11∗00|0)∗(11∗(ϵ|0)|ϵ)

48/65

Page 386: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION: EXAMPLE

L((0|ϵ)(1|000∗)∗(0|ϵ)): All strings that do not contain 101 as a substring

s0start s1

s2

f

s0start s1 s2

0

11∗00|0

1

1

0

000

ϵ

11∗(ϵ|0)|ϵ ϵ

ϵ|0

ϵ

Regular expression: (11∗00|0)∗(11∗(ϵ|0)|ϵ)

48/65

Page 387: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION: EXAMPLE

L((0|ϵ)(1|000∗)∗(0|ϵ)): All strings that do not contain 101 as a substring

s0start

s1 s2

f

s0start s1 s2

0

11∗00|0

1

1

0

000

ϵ

11∗(ϵ|0)|ϵ

ϵϵ|0 ϵ

Regular expression: (11∗00|0)∗(11∗(ϵ|0)|ϵ)

48/65

Page 388: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

FROM NFA TO REGULAR EXPRESSION: EXAMPLE

L((0|ϵ)(1|000∗)∗(0|ϵ)): All strings that do not contain 101 as a substring

s0start

s1 s2

f

s0start s1 s2

0

11∗00|0

1

1

0

000

ϵ

11∗(ϵ|0)|ϵ

ϵϵ|0 ϵ

Regular expression: (11∗00|0)∗(11∗(ϵ|0)|ϵ)

48/65

Page 389: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

49/65

Page 390: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

49/65

Page 391: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SCANNING (1)

ScannerA scanner produces a token (token type, value) stream from a character stream.

Modes of operation

• Complete pass produces token stream, which is then passed to the parser.• Parser calls scanner to request next token

In either case, the scanner greedily recognizes the longest possible token.

50/65

Page 392: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SCANNING (1)

ScannerA scanner produces a token (token type, value) stream from a character stream.

Modes of operation

• Complete pass produces token stream, which is then passed to the parser.• Parser calls scanner to request next token

In either case, the scanner greedily recognizes the longest possible token.

50/65

Page 393: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SCANNING (2)

Scanner implementation

• Hand-written, ad-hoc: Usually when speed is a concern.• From regular expression using scanner generator: More convenient.Result:

• Case statements representing transitions of the DFA.• Table representing the DFA’s transition functionplus driver code to implement the DFA.

51/65

Page 394: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

BUILDING A SCANNER

WorkflowRegular expression→ NFA→ DFA→ minimized DFA

Extensions to pure DFA:

• Not enough to accept a token; need to know which token was accepted andits value:

• One accepting state per token type• Return string read along the path to the accepting state

• Keywords are not identifiers:• Look up identifier in keyword table (e.g., hash table) to see whether it is in facta keyword

• “Look ahead” to distinguish tokens with common prefix (e.g., 100 vs 100.5):• Try to find the longest possible match by continuing to scan from an acceptingstate.

• Backtrack to last accepting state when “stuck”.

52/65

Page 395: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

BUILDING A SCANNER

WorkflowRegular expression→ NFA→ DFA→ minimized DFA

Extensions to pure DFA:

• Not enough to accept a token; need to know which token was accepted andits value:

• One accepting state per token type• Return string read along the path to the accepting state

• Keywords are not identifiers:• Look up identifier in keyword table (e.g., hash table) to see whether it is in facta keyword

• “Look ahead” to distinguish tokens with common prefix (e.g., 100 vs 100.5):• Try to find the longest possible match by continuing to scan from an acceptingstate.

• Backtrack to last accepting state when “stuck”.

52/65

Page 396: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

BUILDING A SCANNER

WorkflowRegular expression→ NFA→ DFA→ minimized DFA

Extensions to pure DFA:• Not enough to accept a token; need to know which token was accepted andits value:

• One accepting state per token type• Return string read along the path to the accepting state

• Keywords are not identifiers:• Look up identifier in keyword table (e.g., hash table) to see whether it is in facta keyword

• “Look ahead” to distinguish tokens with common prefix (e.g., 100 vs 100.5):• Try to find the longest possible match by continuing to scan from an acceptingstate.

• Backtrack to last accepting state when “stuck”.

52/65

Page 397: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

BUILDING A SCANNER

WorkflowRegular expression→ NFA→ DFA→ minimized DFA

Extensions to pure DFA:• Not enough to accept a token; need to know which token was accepted andits value:

• One accepting state per token type• Return string read along the path to the accepting state

• Keywords are not identifiers:• Look up identifier in keyword table (e.g., hash table) to see whether it is in facta keyword

• “Look ahead” to distinguish tokens with common prefix (e.g., 100 vs 100.5):• Try to find the longest possible match by continuing to scan from an acceptingstate.

• Backtrack to last accepting state when “stuck”.

52/65

Page 398: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

BUILDING A SCANNER

WorkflowRegular expression→ NFA→ DFA→ minimized DFA

Extensions to pure DFA:• Not enough to accept a token; need to know which token was accepted andits value:

• One accepting state per token type• Return string read along the path to the accepting state

• Keywords are not identifiers:• Look up identifier in keyword table (e.g., hash table) to see whether it is in facta keyword

• “Look ahead” to distinguish tokens with common prefix (e.g., 100 vs 100.5):• Try to find the longest possible match by continuing to scan from an acceptingstate.

• Backtrack to last accepting state when “stuck”.52/65

Page 399: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (1)

Regular expressions for the different tokens:

lparen: \(rparen: \)lbrac: \[rbrac: \]comma: ,dot: \.dotdot: \.\.lt: <le: <=ident: [A-Za-z][A-Za-z0-9_]*int: [+-]?[0-9]+real: [+-]?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?…

53/65

Page 400: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (2)

Construction of the DFA:

• Turn each regular expression into an NFA,label each accepting state with the token represented by this expression.

• Add an NFA that consumes spaces and comments.• Join the NFA using ϵ-edges from a new start state to their start states.• Add ϵ-transitions from the accepting states of the spaces/comments NFA tothe start state.

• Turn the NFA into a DFA:• If the tokens are unambiguous, each accepting state of the DFA, viewed as a set,includes accepting states from only one of the regular expression NFA.

• Label the DFA accepting state with this token.

• Minimize the DFA.

54/65

Page 401: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (2)

Construction of the DFA:

• Turn each regular expression into an NFA,label each accepting state with the token represented by this expression.

• Add an NFA that consumes spaces and comments.• Join the NFA using ϵ-edges from a new start state to their start states.• Add ϵ-transitions from the accepting states of the spaces/comments NFA tothe start state.

• Turn the NFA into a DFA:• If the tokens are unambiguous, each accepting state of the DFA, viewed as a set,includes accepting states from only one of the regular expression NFA.

• Label the DFA accepting state with this token.

• Minimize the DFA.

54/65

Page 402: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (2)

Construction of the DFA:

• Turn each regular expression into an NFA,label each accepting state with the token represented by this expression.

• Add an NFA that consumes spaces and comments.

• Join the NFA using ϵ-edges from a new start state to their start states.• Add ϵ-transitions from the accepting states of the spaces/comments NFA tothe start state.

• Turn the NFA into a DFA:• If the tokens are unambiguous, each accepting state of the DFA, viewed as a set,includes accepting states from only one of the regular expression NFA.

• Label the DFA accepting state with this token.

• Minimize the DFA.

54/65

Page 403: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (2)

Construction of the DFA:

• Turn each regular expression into an NFA,label each accepting state with the token represented by this expression.

• Add an NFA that consumes spaces and comments.• Join the NFA using ϵ-edges from a new start state to their start states.

• Add ϵ-transitions from the accepting states of the spaces/comments NFA tothe start state.

• Turn the NFA into a DFA:• If the tokens are unambiguous, each accepting state of the DFA, viewed as a set,includes accepting states from only one of the regular expression NFA.

• Label the DFA accepting state with this token.

• Minimize the DFA.

54/65

Page 404: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (2)

Construction of the DFA:

• Turn each regular expression into an NFA,label each accepting state with the token represented by this expression.

• Add an NFA that consumes spaces and comments.• Join the NFA using ϵ-edges from a new start state to their start states.• Add ϵ-transitions from the accepting states of the spaces/comments NFA tothe start state.

• Turn the NFA into a DFA:• If the tokens are unambiguous, each accepting state of the DFA, viewed as a set,includes accepting states from only one of the regular expression NFA.

• Label the DFA accepting state with this token.

• Minimize the DFA.

54/65

Page 405: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (2)

Construction of the DFA:

• Turn each regular expression into an NFA,label each accepting state with the token represented by this expression.

• Add an NFA that consumes spaces and comments.• Join the NFA using ϵ-edges from a new start state to their start states.• Add ϵ-transitions from the accepting states of the spaces/comments NFA tothe start state.

• Turn the NFA into a DFA:• If the tokens are unambiguous, each accepting state of the DFA, viewed as a set,includes accepting states from only one of the regular expression NFA.

• Label the DFA accepting state with this token.

• Minimize the DFA.

54/65

Page 406: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (2)

Construction of the DFA:

• Turn each regular expression into an NFA,label each accepting state with the token represented by this expression.

• Add an NFA that consumes spaces and comments.• Join the NFA using ϵ-edges from a new start state to their start states.• Add ϵ-transitions from the accepting states of the spaces/comments NFA tothe start state.

• Turn the NFA into a DFA:• If the tokens are unambiguous, each accepting state of the DFA, viewed as a set,includes accepting states from only one of the regular expression NFA.

• Label the DFA accepting state with this token.

• Minimize the DFA.

54/65

Page 407: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (3)

startspace, tab, newline, return

( ) digit [ ] , . < letter

*

*

[^*]

[^*)]

*

)

. = letter,digit,_

.

[eE]

digit [eE]digit

[+-]

digit

digitdigit

lparen rparen lbrac rbrac commadot

dotdot

lt

le

ident

int real real

55/65

Page 408: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

EXTENDED EXAMPLE: AN INCOMPLETE SCANNER FOR PASCAL (4)

Driver code:

• Whenever the scan reaches an accepting state of the spaces/comments NFA,set a start marker.

• Whenever the scan reaches an accepting state of any other NFA, set an endmarker and remember the token.

• Whenever the scan reaches state ∅,• Go back to the end marker.• Report the remembered token.• Turn the text between start and end marker into a representation of thescanned token (integer, identifier string, …).

• Set the start marker to be equal to the end marker.

56/65

Page 409: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (1)

GoalGiven a DFA D, produce a DFA D ′ withthe minimum number of states andsuch that L(D) = L(D ′).

IdeaGroup states of D into classes ofequivalent states (accepting/non-accepting, same transitions).

Procedure

• Start with two equivalence classes: accepting and non-accepting• Find an equivalence class C and a letter a such that, upon reading a, thestates in C transition to k > 1 equivalence classes C ′

1, C ′2, . . . , C ′

k.Partition C into subclasses C1, C2, . . . , Ck such that, upon reading a, the statesin Ci transition to states in C ′

i .• Repeat until no such “partitionable” equivalence class C can be found.• The final set of equivalence classes is the set of states of the minimized DFA.

57/65

Page 410: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (1)

GoalGiven a DFA D, produce a DFA D ′ withthe minimum number of states andsuch that L(D) = L(D ′).

IdeaGroup states of D into classes ofequivalent states (accepting/non-accepting, same transitions).

Procedure

• Start with two equivalence classes: accepting and non-accepting• Find an equivalence class C and a letter a such that, upon reading a, thestates in C transition to k > 1 equivalence classes C ′

1, C ′2, . . . , C ′

k.Partition C into subclasses C1, C2, . . . , Ck such that, upon reading a, the statesin Ci transition to states in C ′

i .• Repeat until no such “partitionable” equivalence class C can be found.• The final set of equivalence classes is the set of states of the minimized DFA.

57/65

Page 411: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (1)

GoalGiven a DFA D, produce a DFA D ′ withthe minimum number of states andsuch that L(D) = L(D ′).

IdeaGroup states of D into classes ofequivalent states (accepting/non-accepting, same transitions).

Procedure

• Start with two equivalence classes: accepting and non-accepting• Find an equivalence class C and a letter a such that, upon reading a, thestates in C transition to k > 1 equivalence classes C ′

1, C ′2, . . . , C ′

k.Partition C into subclasses C1, C2, . . . , Ck such that, upon reading a, the statesin Ci transition to states in C ′

i .• Repeat until no such “partitionable” equivalence class C can be found.• The final set of equivalence classes is the set of states of the minimized DFA.

57/65

Page 412: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (1)

GoalGiven a DFA D, produce a DFA D ′ withthe minimum number of states andsuch that L(D) = L(D ′).

IdeaGroup states of D into classes ofequivalent states (accepting/non-accepting, same transitions).

Procedure

• Start with two equivalence classes: accepting and non-accepting

• Find an equivalence class C and a letter a such that, upon reading a, thestates in C transition to k > 1 equivalence classes C ′

1, C ′2, . . . , C ′

k.Partition C into subclasses C1, C2, . . . , Ck such that, upon reading a, the statesin Ci transition to states in C ′

i .• Repeat until no such “partitionable” equivalence class C can be found.• The final set of equivalence classes is the set of states of the minimized DFA.

57/65

Page 413: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (1)

GoalGiven a DFA D, produce a DFA D ′ withthe minimum number of states andsuch that L(D) = L(D ′).

IdeaGroup states of D into classes ofequivalent states (accepting/non-accepting, same transitions).

Procedure

• Start with two equivalence classes: accepting and non-accepting• Find an equivalence class C and a letter a such that, upon reading a, thestates in C transition to k > 1 equivalence classes C ′

1, C ′2, . . . , C ′

k.Partition C into subclasses C1, C2, . . . , Ck such that, upon reading a, the statesin Ci transition to states in C ′

i .

• Repeat until no such “partitionable” equivalence class C can be found.• The final set of equivalence classes is the set of states of the minimized DFA.

57/65

Page 414: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (1)

GoalGiven a DFA D, produce a DFA D ′ withthe minimum number of states andsuch that L(D) = L(D ′).

IdeaGroup states of D into classes ofequivalent states (accepting/non-accepting, same transitions).

Procedure

• Start with two equivalence classes: accepting and non-accepting• Find an equivalence class C and a letter a such that, upon reading a, thestates in C transition to k > 1 equivalence classes C ′

1, C ′2, . . . , C ′

k.Partition C into subclasses C1, C2, . . . , Ck such that, upon reading a, the statesin Ci transition to states in C ′

i .• Repeat until no such “partitionable” equivalence class C can be found.

• The final set of equivalence classes is the set of states of the minimized DFA.

57/65

Page 415: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (1)

GoalGiven a DFA D, produce a DFA D ′ withthe minimum number of states andsuch that L(D) = L(D ′).

IdeaGroup states of D into classes ofequivalent states (accepting/non-accepting, same transitions).

Procedure

• Start with two equivalence classes: accepting and non-accepting• Find an equivalence class C and a letter a such that, upon reading a, thestates in C transition to k > 1 equivalence classes C ′

1, C ′2, . . . , C ′

k.Partition C into subclasses C1, C2, . . . , Ck such that, upon reading a, the statesin Ci transition to states in C ′

i .• Repeat until no such “partitionable” equivalence class C can be found.• The final set of equivalence classes is the set of states of the minimized DFA.

57/65

Page 416: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 417: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 418: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 419: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 420: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 421: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 422: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 423: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA: EXAMPLE

s0start

s1

s2

s3

s4

s5

1

0

0

1

0 1

0

1

1 0

1

0

0|1

s0s1s2s3s4s5

s0 s10 1

s2 s30 1

s0 s30 1

s4 s30 1

s5 s30 1

s5 s30 1

58/65

Page 424: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

MINIMIZING THE DFA (2)

The described procedure ensures that L(D) = L(D ′) but does not distinguishbetween different types of accepting states (corresponding to tokens).

To distinguish between different types of accepting states, start with oneequivalence class per type of accepting state.

59/65

Page 425: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

60/65

Page 426: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

ROAD MAP

• Regular languages• Regular expressions• Deterministic finite automata (DFA)• Non-deterministic finite automata (NFA)• Expressive power of DFA and NFA• Equivalence of regular expressions, DFA, and NFA

• Building a scanner• Regular expression→ NFA→ DFA• Minimizing the DFA

• Limitations of regular languages

60/65

Page 427: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 428: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 429: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 430: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 431: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 432: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 433: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 434: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 435: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 436: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 437: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

HOW GENERAL ARE REGULAR LANGUAGES?

If R and S are regular languages, then so are

• RS, R ∪ S, R∗

By definition

• R ∩ S

R ∩ S = Σ∗ \ ((Σ∗ \ R) ∪ (Σ∗ \ S))

• R \ S

R \ S = R ∩ (Σ∗ \ S)

• Σ∗ \ R (the complement of R)

Build a DFA for Σ∗ \ R from a DFAfor R by making accepting statesnon-accepting and vice versa.

• ←−R = {←−σ | σ ∈ R}, where←−σ is σwritten backwards

A regular expression for R “writtenbackwards” is a regular expressionfor←−R .

61/65

Page 438: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NOT ALL LANGUAGES ARE REGULAR

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

⇒ The language L = {0n1n | n ≥ 0}is not regular!

• Assume L is regular and let nLbe as in the Pumping Lemma.

• Let σ = 0nL1nL ∈ L.• Then σ = αβγ with |αβ| ≤ nLand |β| > 0 and αββγ ∈ L.

• Since |αβ| ≤ nL, we have α = 0k

and β = 0m, where m = |β| > 0.• Thus, αββγ = 0m+nL1nL /∈ L, acontradiction.

62/65

Page 439: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NOT ALL LANGUAGES ARE REGULAR

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

⇒ The language L = {0n1n | n ≥ 0}is not regular!

• Assume L is regular and let nLbe as in the Pumping Lemma.

• Let σ = 0nL1nL ∈ L.• Then σ = αβγ with |αβ| ≤ nLand |β| > 0 and αββγ ∈ L.

• Since |αβ| ≤ nL, we have α = 0k

and β = 0m, where m = |β| > 0.• Thus, αββγ = 0m+nL1nL /∈ L, acontradiction.

62/65

Page 440: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NOT ALL LANGUAGES ARE REGULAR

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

⇒ The language L = {0n1n | n ≥ 0}is not regular!

• Assume L is regular and let nLbe as in the Pumping Lemma.

• Let σ = 0nL1nL ∈ L.• Then σ = αβγ with |αβ| ≤ nLand |β| > 0 and αββγ ∈ L.

• Since |αβ| ≤ nL, we have α = 0k

and β = 0m, where m = |β| > 0.• Thus, αββγ = 0m+nL1nL /∈ L, acontradiction.

62/65

Page 441: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NOT ALL LANGUAGES ARE REGULAR

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

⇒ The language L = {0n1n | n ≥ 0}is not regular!

• Assume L is regular and let nLbe as in the Pumping Lemma.

• Let σ = 0nL1nL ∈ L.

• Then σ = αβγ with |αβ| ≤ nLand |β| > 0 and αββγ ∈ L.

• Since |αβ| ≤ nL, we have α = 0k

and β = 0m, where m = |β| > 0.• Thus, αββγ = 0m+nL1nL /∈ L, acontradiction.

62/65

Page 442: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NOT ALL LANGUAGES ARE REGULAR

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

⇒ The language L = {0n1n | n ≥ 0}is not regular!

• Assume L is regular and let nLbe as in the Pumping Lemma.

• Let σ = 0nL1nL ∈ L.• Then σ = αβγ with |αβ| ≤ nLand |β| > 0 and αββγ ∈ L.

• Since |αβ| ≤ nL, we have α = 0k

and β = 0m, where m = |β| > 0.• Thus, αββγ = 0m+nL1nL /∈ L, acontradiction.

62/65

Page 443: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NOT ALL LANGUAGES ARE REGULAR

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

⇒ The language L = {0n1n | n ≥ 0}is not regular!

• Assume L is regular and let nLbe as in the Pumping Lemma.

• Let σ = 0nL1nL ∈ L.• Then σ = αβγ with |αβ| ≤ nLand |β| > 0 and αββγ ∈ L.

• Since |αβ| ≤ nL, we have α = 0k

and β = 0m, where m = |β| > 0.

• Thus, αββγ = 0m+nL1nL /∈ L, acontradiction.

62/65

Page 444: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

NOT ALL LANGUAGES ARE REGULAR

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

⇒ The language L = {0n1n | n ≥ 0}is not regular!

• Assume L is regular and let nLbe as in the Pumping Lemma.

• Let σ = 0nL1nL ∈ L.• Then σ = αβγ with |αβ| ≤ nLand |β| > 0 and αββγ ∈ L.

• Since |αβ| ≤ nL, we have α = 0k

and β = 0m, where m = |β| > 0.• Thus, αββγ = 0m+nL1nL /∈ L, acontradiction.

62/65

Page 445: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROOF OF THE PUMPING LEMMA

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

Let D = (S, Σ, δ, s0, F) be a DFA such thatL = L(D).Let nL = |S|+ 1.

startstartα

β

γ

63/65

Page 446: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROOF OF THE PUMPING LEMMA

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

Let D = (S, Σ, δ, s0, F) be a DFA such thatL = L(D).

Let nL = |S|+ 1.

startstartα

β

γ

63/65

Page 447: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROOF OF THE PUMPING LEMMA

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

Let D = (S, Σ, δ, s0, F) be a DFA such thatL = L(D).Let nL = |S|+ 1.

startstartα

β

γ

63/65

Page 448: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROOF OF THE PUMPING LEMMA

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

Let D = (S, Σ, δ, s0, F) be a DFA such thatL = L(D).Let nL = |S|+ 1.

start

startα

β

γ

63/65

Page 449: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROOF OF THE PUMPING LEMMA

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

Let D = (S, Σ, δ, s0, F) be a DFA such thatL = L(D).Let nL = |S|+ 1.

start

start

α

β

γ

63/65

Page 450: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROOF OF THE PUMPING LEMMA

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

Let D = (S, Σ, δ, s0, F) be a DFA such thatL = L(D).Let nL = |S|+ 1.

start

start

α

β

γ

63/65

Page 451: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PROOF OF THE PUMPING LEMMA

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

Let D = (S, Σ, δ, s0, F) be a DFA such thatL = L(D).Let nL = |S|+ 1.

start

startα

β

γ

63/65

Page 452: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.

Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 453: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.

Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 454: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 455: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 456: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.

• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 457: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.

• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 458: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 459: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.

• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 460: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

PUMPING LEMMA: MORE APPLICATIONS

Pumping LemmaFor every regular language L, thereexists a constant nL such thatevery σ ∈ L with |σ| ≥ nL can bewritten as σ = αβγ with

• |αβ| ≤ nL,• |β| > 0, and• αβkγ ∈ L for all k ≥ 0.

• L = {(m)m | m ≥ 0} is not regular.Same structure as L ′ = {0n1n | n ≥ 0}.

• L = {ap | p is a prime number} is notregular.

• Assume L is regular.• Choose prime number p ≥ nL + 2⇒ σ = ap ∈ L.• σ = αβγ, where α = aa, β = ab,a+ b ≤ nL and b > 0.

• αβcγ ∈ L, where c = |αγ| = p− b ≥ 2.• However, |αβcγ| = (b+ 1)c, which isnot prime because b+ 1 ≥ 2 andc ≥ 2. Contradiction.

64/65

Page 461: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.

• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.• NFA are mainly a tool for translating regular expressions to DFA.• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65

Page 462: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.• NFA are mainly a tool for translating regular expressions to DFA.• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65

Page 463: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.• NFA are mainly a tool for translating regular expressions to DFA.• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65

Page 464: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.• NFA are mainly a tool for translating regular expressions to DFA.• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65

Page 465: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.• NFA are mainly a tool for translating regular expressions to DFA.• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65

Page 466: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.

• NFA are mainly a tool for translating regular expressions to DFA.• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65

Page 467: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.• NFA are mainly a tool for translating regular expressions to DFA.

• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65

Page 468: Regular Languages, Finite Automata, and Lexical Analysis ...nzeh/Teaching/3136/Slides/lexical-analysis.pdf · PROGRAMTRANSLATIONFLOWCHART Scanner(lexicalanalysis) Parser(syntacticanalysis)

SUMMARY

• Parsing is complex⇒ Apply to a token stream rather than a character stream.• Lexical analysis turns character stream into more compact token stream.

• Regular languages are general enough to capture the structure of tokens butnot general enough to capture the structure of programming languages.

• There exist languages that are not regular.

• Regular languages are described using regular expressions and recognizedusing DFA.

• DFA are very simple machines that can be implemented very efficiently.• NFA are mainly a tool for translating regular expressions to DFA.• Lexical analysis requires some simple extensions to DFA because we need toknow which token wa accepted and we need to supportgreediness/backtracking.

65/65


Recommended