compilers and automata theory - university of...

190
Compilers and Automata Theory Joseph Thomas University of Arizona Department of Mathematics November 14, 2011

Upload: others

Post on 07-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Compilers and Automata Theory

Joseph Thomas

University of ArizonaDepartment of Mathematics

November 14, 2011

Page 2: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:

I Is correct.I Produces useful error messages.I Produces efficient code.

Page 3: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:

I Is correct.I Produces useful error messages.I Produces efficient code.

Page 4: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:

I Is correct.I Produces useful error messages.I Produces efficient code.

Page 5: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:

I Is correct.I Produces useful error messages.I Produces efficient code.

Page 6: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:

I Is correct.I Produces useful error messages.I Produces efficient code.

Page 7: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:I Is correct.

I Produces useful error messages.I Produces efficient code.

Page 8: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:I Is correct.I Produces useful error messages.

I Produces efficient code.

Page 9: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

What is a compiler?

I A compiler is a program that translates source code intosome kind of executable machine code.

I Mathematically, a mapping from programs written in onelanguage (C, Java, Python, etc.) to programs written inanother (x86, MIPS, Java Byte-Code, etc.).

I Problem: How can I write a compiler that:I Is correct.I Produces useful error messages.I Produces efficient code.

Page 10: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

The Automata Viewpoint

I A alphabet is a set of symbols. (Ex: Σ = {a, b})I Strings are ordered, finite tuples with entries in an alphabet.

(Ex: abaabba)

I Languages are sets of strings. (Ex: {anbn : n ∈ N})I Problem: Can I write down instructions that describe how to

recognize when a string s is a member of language S?

I Problem: Can one method of “writing down instructions” bemore powerful than another?

Page 11: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

The Automata Viewpoint

I A alphabet is a set of symbols. (Ex: Σ = {a, b})

I Strings are ordered, finite tuples with entries in an alphabet.(Ex: abaabba)

I Languages are sets of strings. (Ex: {anbn : n ∈ N})I Problem: Can I write down instructions that describe how to

recognize when a string s is a member of language S?

I Problem: Can one method of “writing down instructions” bemore powerful than another?

Page 12: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

The Automata Viewpoint

I A alphabet is a set of symbols. (Ex: Σ = {a, b})I Strings are ordered, finite tuples with entries in an alphabet.

(Ex: abaabba)

I Languages are sets of strings. (Ex: {anbn : n ∈ N})I Problem: Can I write down instructions that describe how to

recognize when a string s is a member of language S?

I Problem: Can one method of “writing down instructions” bemore powerful than another?

Page 13: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

The Automata Viewpoint

I A alphabet is a set of symbols. (Ex: Σ = {a, b})I Strings are ordered, finite tuples with entries in an alphabet.

(Ex: abaabba)

I Languages are sets of strings. (Ex: {anbn : n ∈ N})

I Problem: Can I write down instructions that describe how torecognize when a string s is a member of language S?

I Problem: Can one method of “writing down instructions” bemore powerful than another?

Page 14: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

The Automata Viewpoint

I A alphabet is a set of symbols. (Ex: Σ = {a, b})I Strings are ordered, finite tuples with entries in an alphabet.

(Ex: abaabba)

I Languages are sets of strings. (Ex: {anbn : n ∈ N})I Problem: Can I write down instructions that describe how to

recognize when a string s is a member of language S?

I Problem: Can one method of “writing down instructions” bemore powerful than another?

Page 15: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

The Automata Viewpoint

I A alphabet is a set of symbols. (Ex: Σ = {a, b})I Strings are ordered, finite tuples with entries in an alphabet.

(Ex: abaabba)

I Languages are sets of strings. (Ex: {anbn : n ∈ N})I Problem: Can I write down instructions that describe how to

recognize when a string s is a member of language S?

I Problem: Can one method of “writing down instructions” bemore powerful than another?

Page 16: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 17: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 18: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 19: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 20: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 21: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 22: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 23: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 24: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Who needs this technology?

Anyone who needs to translate to or from a structured language.

I Programming

I LaTeX, Web Browsers

I Computer Algebra Systems (GAP, Matlab, Mathematica)

This talk will help you understand what these tools are doing...

I ...when they succeed in producing output.

I ...when they give you error messages.

Page 25: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Phases of Compilation

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 26: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 27: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:

I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 28: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:

I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 29: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:

I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 30: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:I for 7→ FOR BEGIN

I <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 31: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 32: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).

I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 33: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 34: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 35: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.

I 314!!foo is not a valid variable name in C.

Page 36: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Lexical Analysis

Main idea: Decompose a file (a string of characters) into “atomic”pieces.

I Recognize reserved words and operators:I for 7→ FOR BEGINI <= 7→ LESS THAN EQUAL

I Recognize numerical literals: 453 7→ INT LIT(453).I Recognize “spelling” errors:

I 3.14.14 doesn’t define a valid floating point number.I 314!!foo is not a valid variable name in C.

Page 37: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

A line of C code (30 characters with whitespace):

int foo = bar + 453;

becomes a sequence of 7 tokens:

IDENT(int), IDENT(foo), EQUALS, IDENT(bar), PLUS,INTLIT(453), SEMICOLON

Page 38: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

A line of C code (30 characters with whitespace):

int foo = bar + 453;

becomes a sequence of 7 tokens:

IDENT(int), IDENT(foo), EQUALS, IDENT(bar), PLUS,INTLIT(453), SEMICOLON

Page 39: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Deterministic Finite Automatons (DFAs)

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 40: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Deterministic Finite Automatons (DFAs)

a

a

a

b

a

b

b

a

a

b

b

a

DFAs are decorated directed graphs.

I Each edge identified with exactly one letter in the alphabet.

I One “start node,” at least one “end node.”

A string is recognized (or accepted) by a given DFA if itdescribes a path from the start node to an end node.

Page 41: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Deterministic Finite Automatons (DFAs)

a

a

a

b

a

b

b

a

a

b

b

a

DFAs are decorated directed graphs.

I Each edge identified with exactly one letter in the alphabet.

I One “start node,” at least one “end node.”

A string is recognized (or accepted) by a given DFA if itdescribes a path from the start node to an end node.

Page 42: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Deterministic Finite Automatons (DFAs)

a

a

a

b

a

b

b

a

a

b

b

a

DFAs are decorated directed graphs.

I Each edge identified with exactly one letter in the alphabet.

I One “start node,” at least one “end node.”

A string is recognized (or accepted) by a given DFA if itdescribes a path from the start node to an end node.

Page 43: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Deterministic Finite Automatons (DFAs)

a

a

a

b

a

b

b

a

a

b

b

a

DFAs are decorated directed graphs.

I Each edge identified with exactly one letter in the alphabet.

I One “start node,” at least one “end node.”

A string is recognized (or accepted) by a given DFA if itdescribes a path from the start node to an end node.

Page 44: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Deterministic Finite Automatons (DFAs)

a

a

a

b

a

b

b

a

a

b

b

a

DFAs are decorated directed graphs.

I Each edge identified with exactly one letter in the alphabet.

I One “start node,” at least one “end node.”

A string is recognized (or accepted) by a given DFA if itdescribes a path from the start node to an end node.

Page 45: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

A DFA that recognizes identifiers (variable names) and the if

token.

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Page 46: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output:

Page 47: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output:

Page 48: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output:

Page 49: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output: IF,

Page 50: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output: IF,

Page 51: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output: IF,

Page 52: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output: IF,

Page 53: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output: IF,

Page 54: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

DFA Example

Input: if iota

Σ - {i}

i

Σ

IDENT

Σ

IF

Σ - {f}

f

Output: IF, IDENT(iota),

Page 55: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Syntax Analysis

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 56: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Syntax Analysis

Problem: Convert a sequence of tokens to a syntax tree.

I The tree encodes the grammatical structure of the program.

I We’ll store further information about the program on the tree.

I Syntax trees are very convenient for operating on programs.

Page 57: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Syntax Analysis

Problem: Convert a sequence of tokens to a syntax tree.

I The tree encodes the grammatical structure of the program.

I We’ll store further information about the program on the tree.

I Syntax trees are very convenient for operating on programs.

Page 58: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Syntax Analysis

Problem: Convert a sequence of tokens to a syntax tree.

I The tree encodes the grammatical structure of the program.

I We’ll store further information about the program on the tree.

I Syntax trees are very convenient for operating on programs.

Page 59: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Syntax Analysis

Problem: Convert a sequence of tokens to a syntax tree.

I The tree encodes the grammatical structure of the program.

I We’ll store further information about the program on the tree.

I Syntax trees are very convenient for operating on programs.

Page 60: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example:

PROGRAM P ;

VAR i : INTEGER ;BEGIN

i := 1 ;WHILE i < 10 DO

i := i + 1 ;ENDDO;WRITE i − 1 0 ; WRITELN ;

END.

Page 61: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 62: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 63: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 64: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 65: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 66: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 67: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 68: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 69: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

PROGRAM

decls stats

STATS

left right

DECLS

left right

STATS

left right

ASSIGN

left right

STATS

left right

WHILE

expr stats

STATS

left right

WRITE

expr

STATNULLWRITELNBINARY

left right

INTLITVARREF

next

DESNULL

STATS

left right

BINARY

left right

STATNULLASSIGN

left right

BINARY

left right

VARREF

next

INTLITVARREF

next

DESNULL

DESNULL

INTLITVARREF

next

DESNULL

INTLITVARREF

next

DESNULL

DECLNULLVARDECL

Page 70: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 71: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”

I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 72: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”

I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 73: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”

I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 74: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”

I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 75: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”

I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 76: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”I Context A: “Fruit” is the subject, “flies” is the verb.

I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 77: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 78: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 79: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines / Context Free Grammars

I We specify syntax with a Context Free Grammar (CFG).

I Programs have a single grammatical structure, regardless ofcontext.

I English is not context free: “Fruit flies like a banana.”I Context A: “Fruit” is the subject, “flies” is the verb.I Context B: “Fruit flies” is the subject, “like” is the verb.

Page 80: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

Tokens:

Σ = {while, do, done, true, or, not, :=, ;, (, ), x, y, z, 0, 1}

Grammar:

Stats→ Stat ; Stats | Stat

Stat→ Ident := Exp | while Exp do Stats done

Exp→ (Exp or Exp) | (not Exp) | Ident | trueIdent→ x | y | z | 0 | 1

Example Program:

while (not x) do x := (not x); done; y := x;

Page 81: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 82: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 83: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 84: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 85: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 86: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 87: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 88: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 89: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 90: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 91: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 92: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 93: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Stack Machines

A recipe for creating automatons:

I Take a finite directed graph G .

I Add a data structure.

I Decorate edges of G with symbols of the input alphabet andoperations on the data structure.

For Stack Machines the added data structure is a stack. Wemay:

I Push symbols onto the stack.

I Pop symbols off the stack.

I Peek at the top stack symbol.

Notation:

I $: The end of the input string.

I Edges decorated with “x : [PUSH/POP/PEEK] y”, where xand y are tokens.

Page 94: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

A stack machine to recognize {anbn : n ∈ N}:

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Here, the stack lets us:

I Count up the number of a’s.

I Count off the number of b’s.

Page 95: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

A stack machine to recognize {anbn : n ∈ N}:

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Here, the stack lets us:

I Count up the number of a’s.

I Count off the number of b’s.

Page 96: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

A stack machine to recognize {anbn : n ∈ N}:

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Here, the stack lets us:

I Count up the number of a’s.

I Count off the number of b’s.

Page 97: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

A stack machine to recognize {anbn : n ∈ N}:

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Here, the stack lets us:

I Count up the number of a’s.

I Count off the number of b’s.

Page 98: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

Input: aabb$

Stack:

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Page 99: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

Input: aabb$

Stack: a

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Page 100: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

Input: aabb$

Stack: aa

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Page 101: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

Input: aabb$

Stack: a

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Page 102: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

Input: aabb$

Stack:

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Page 103: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

Input: aabb$

Stack:

a : PUSH a

a : PUSH a

b : POP a

b : POP a

$ : EMPTY

Page 104: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 105: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 106: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 107: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 108: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 109: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 110: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 111: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 112: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

We want compilers that...

I ...help us avoid making silly mistakes.

I ...produce the program we expect.

During the semantic analysis step we gather information about:

I Variables

I Scope

I Type — what a variable is supposed to represent.

Page 113: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

This is one of the more “language dependent” parts of a compiler.Assuming a strongly typed language (like C), we will:

I Build data structures that categorize variables and procedurecalls.

I Traverse the syntax tree:

I Accumulate data from declarations.I Examine statements, making sure they’re well formed.

Page 114: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

This is one of the more “language dependent” parts of a compiler.Assuming a strongly typed language (like C), we will:

I Build data structures that categorize variables and procedurecalls.

I Traverse the syntax tree:

I Accumulate data from declarations.I Examine statements, making sure they’re well formed.

Page 115: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

This is one of the more “language dependent” parts of a compiler.Assuming a strongly typed language (like C), we will:

I Build data structures that categorize variables and procedurecalls.

I Traverse the syntax tree:

I Accumulate data from declarations.I Examine statements, making sure they’re well formed.

Page 116: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

This is one of the more “language dependent” parts of a compiler.Assuming a strongly typed language (like C), we will:

I Build data structures that categorize variables and procedurecalls.

I Traverse the syntax tree:

I Accumulate data from declarations.I Examine statements, making sure they’re well formed.

Page 117: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

This is one of the more “language dependent” parts of a compiler.Assuming a strongly typed language (like C), we will:

I Build data structures that categorize variables and procedurecalls.

I Traverse the syntax tree:I Accumulate data from declarations.

I Examine statements, making sure they’re well formed.

Page 118: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

This is one of the more “language dependent” parts of a compiler.Assuming a strongly typed language (like C), we will:

I Build data structures that categorize variables and procedurecalls.

I Traverse the syntax tree:I Accumulate data from declarations.I Examine statements, making sure they’re well formed.

Page 119: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

Examples of errors:

I Using a variable/procedure without declaring it.

I Using a variable in a way that doesn’t agree with it’s type(Ex. adding a string to an int).

I Declaring a variable several times in the same scope, withdifferent types.

Important: The more you know at compile-time, the moreefficient you can make your machine/byte code.

Page 120: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

Examples of errors:

I Using a variable/procedure without declaring it.

I Using a variable in a way that doesn’t agree with it’s type(Ex. adding a string to an int).

I Declaring a variable several times in the same scope, withdifferent types.

Important: The more you know at compile-time, the moreefficient you can make your machine/byte code.

Page 121: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

Examples of errors:

I Using a variable/procedure without declaring it.

I Using a variable in a way that doesn’t agree with it’s type(Ex. adding a string to an int).

I Declaring a variable several times in the same scope, withdifferent types.

Important: The more you know at compile-time, the moreefficient you can make your machine/byte code.

Page 122: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

Examples of errors:

I Using a variable/procedure without declaring it.

I Using a variable in a way that doesn’t agree with it’s type(Ex. adding a string to an int).

I Declaring a variable several times in the same scope, withdifferent types.

Important: The more you know at compile-time, the moreefficient you can make your machine/byte code.

Page 123: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

Examples of errors:

I Using a variable/procedure without declaring it.

I Using a variable in a way that doesn’t agree with it’s type(Ex. adding a string to an int).

I Declaring a variable several times in the same scope, withdifferent types.

Important: The more you know at compile-time, the moreefficient you can make your machine/byte code.

Page 124: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

Examples of errors:

I Using a variable/procedure without declaring it.

I Using a variable in a way that doesn’t agree with it’s type(Ex. adding a string to an int).

I Declaring a variable several times in the same scope, withdifferent types.

Important: The more you know at compile-time, the moreefficient you can make your machine/byte code.

Page 125: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Semantic Analysis

Examples of errors:

I Using a variable/procedure without declaring it.

I Using a variable in a way that doesn’t agree with it’s type(Ex. adding a string to an int).

I Declaring a variable several times in the same scope, withdifferent types.

Important: The more you know at compile-time, the moreefficient you can make your machine/byte code.

Page 126: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

PROGRAM

name="P"

ident="null"

isGlobal="false"

position="1"

decls stats

STATS

start="UNDEF"

loopExit="null"

inLoop="false"

position="5"

left right

DECLS

isGlobal="true"

position="3"

left right

STATS

start="UNDEF"

loopExit="null"

inLoop="false"

position="6"

left right

ASSIGN

inLoop="false"

next="null"

loopExit="null"

position="5"

left right

STATS

start="UNDEF"

loopExit="null"

inLoop="false"

position="9"

left right

WHILE

inLoop="false"

next="null"

loopExit="null"

position="6"

expr stats

STATS

start="UNDEF"

loopExit="null"

inLoop="false"

position="9"

left right

WRITE

inLoop="false"

next="null"

loopExit="null"

position="9"

expr

STATNULL

left="null"

right="null"

start="UNDEF"

loopExit="null"

inLoop="false"

position="10"

WRITELN

inLoop="false"

next="null"

loopExit="null"

position="9"

BINARY

op="2"

type="INTEGER"

isConst="false"

isLVar="false"

value="Error"

position="9"

left right

INTLIT

literal="10"

type="INTEGER"

isConst="true"

isLVar="false"

value="10"

position="9"

VARREF

ident="i"

ss="sem.Variable@503429"

vv="sem.Variable@503429"

type="INTEGER"

isConst="false"

isLVar="true"

value="Error"

position="9"

next

DESNULL

type="null"

isConst="false"

isLVar="false"

value="Error"

position="9"

STATS

start="UNDEF"

loopExit="null"

inLoop="false"

position="7"

left right

BINARY

op="22"

type="BOOLEAN"

isConst="false"

isLVar="false"

value="Error"

position="6"

left right

STATNULL

left="null"

right="null"

start="UNDEF"

loopExit="null"

inLoop="false"

position="8"

ASSIGN

inLoop="false"

next="null"

loopExit="null"

position="7"

left right

BINARY

op="1"

type="INTEGER"

isConst="false"

isLVar="false"

value="Error"

position="7"

left right

VARREF

ident="i"

ss="sem.Variable@503429"

vv="sem.Variable@503429"

type="INTEGER"

isConst="false"

isLVar="true"

value="Error"

position="7"

next

INTLIT

literal="1"

type="INTEGER"

isConst="true"

isLVar="false"

value="1"

position="7"

VARREF

ident="i"

ss="sem.Variable@503429"

vv="sem.Variable@503429"

type="INTEGER"

isConst="false"

isLVar="true"

value="Error"

position="7"

next

DESNULL

type="null"

isConst="false"

isLVar="false"

value="Error"

position="7"

DESNULL

type="null"

isConst="false"

isLVar="false"

value="Error"

position="7"

INTLIT

literal="10"

type="INTEGER"

isConst="true"

isLVar="false"

value="10"

position="6"

VARREF

ident="i"

ss="sem.Variable@503429"

vv="sem.Variable@503429"

type="INTEGER"

isConst="false"

isLVar="true"

value="Error"

position="6"

next

DESNULL

type="null"

isConst="false"

isLVar="false"

value="Error"

position="6"

INTLIT

literal="1"

type="INTEGER"

isConst="true"

isLVar="false"

value="1"

position="5"

VARREF

ident="i"

ss="sem.Variable@503429"

vv="sem.Variable@503429"

type="INTEGER"

isConst="false"

isLVar="true"

value="Error"

position="5"

next

DESNULL

type="null"

isConst="false"

isLVar="false"

value="Error"

position="5"

DECLNULL

left="null"

right="null"

isGlobal="true"

position="4"

VARDECL

typeName="INTEGER"

ident="i"

isGlobal="true"

position="3"

Page 127: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 128: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 129: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .

I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 130: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 131: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 132: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.

I We can only inspect the array element the read/write headcovers.

I Operations: read cell, write cell, and move read/write headleft/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 133: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.

I Operations: read cell, write cell, and move read/write headleft/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 134: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 135: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 136: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:

I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 137: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:I The symbol observed on the tape.

I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 138: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:I The symbol observed on the tape.I Instructions for what to write to the tape.

I Instructions for which way to move the read/write head.

Page 139: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

Definition:

I Start with a finite directed graph G .I Add a data structure — an infinite tape.

I An array that can be “grown” in either direction.I We can only inspect the array element the read/write head

covers.I Operations: read cell, write cell, and move read/write head

left/right one cell.

I Decorate edges of G with instructions for:I The symbol observed on the tape.I Instructions for what to write to the tape.I Instructions for which way to move the read/write head.

Page 140: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

DFA’s are strictly weaker than CFG’s, which are strictly weakerthan Turing Machines.

Compilers Viewpoint:

I A compiler is a “proof” that the input language is no strongerthan the output language.

I Translating from a high-level language directly to a low-levellanguage is hard.

I Instead, we can pass through a number of intermediatelanguages.

Page 141: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

DFA’s are strictly weaker than CFG’s, which are strictly weakerthan Turing Machines.

Compilers Viewpoint:

I A compiler is a “proof” that the input language is no strongerthan the output language.

I Translating from a high-level language directly to a low-levellanguage is hard.

I Instead, we can pass through a number of intermediatelanguages.

Page 142: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

DFA’s are strictly weaker than CFG’s, which are strictly weakerthan Turing Machines.

Compilers Viewpoint:

I A compiler is a “proof” that the input language is no strongerthan the output language.

I Translating from a high-level language directly to a low-levellanguage is hard.

I Instead, we can pass through a number of intermediatelanguages.

Page 143: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

DFA’s are strictly weaker than CFG’s, which are strictly weakerthan Turing Machines.

Compilers Viewpoint:

I A compiler is a “proof” that the input language is no strongerthan the output language.

I Translating from a high-level language directly to a low-levellanguage is hard.

I Instead, we can pass through a number of intermediatelanguages.

Page 144: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Turing Machines

DFA’s are strictly weaker than CFG’s, which are strictly weakerthan Turing Machines.

Compilers Viewpoint:

I A compiler is a “proof” that the input language is no strongerthan the output language.

I Translating from a high-level language directly to a low-levellanguage is hard.

I Instead, we can pass through a number of intermediatelanguages.

Page 145: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

DFA

CFG orStack Machine

Turing Machine

Source Code

Lexical Analyzer

Executable Code

Lexing Errors

Syntax Analyzer

Tokens

Syntax Errors

Semantic Analyzer

Syntax Tree

Semantic Errors

Code Generator

Decorated Syntax Tree

Page 146: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:

I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 147: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:

I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 148: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:

I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 149: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:

I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 150: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:

I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 151: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:I Correctness — no data gets clobbered.

I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 152: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 153: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.

I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 154: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 155: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 156: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.

I Your compiler is tied to a particular architecture.

Page 157: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Generating Machine Code

This step is closely tied to the architecture or virtual machineyou choose to target.For Microprocessors:

I Compiler Output: Instructions for the CPU

I To perform any calculation, you need to store data in one offinitely many registers.

I Problem: How can we ensure:I Correctness — no data gets clobbered.I Speed

I Pros: Programs run faster.I Cons:

I An intermediate representation is necessary.I Your compiler is tied to a particular architecture.

Page 158: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Example

PROGRAM FACTORIAL ;

VAR i : INTEGER ;VAR prod : INTEGER ;BEGIN

i := 1 ;prod := 1 ;WHILE i <= 10 DO

prod := prod ∗ i ;i := i + 1 ;

ENDDO;WRITE prod ; WRITELN ;

END.

Page 159: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Intermediate Representation Example

1 assign v0 1

2 assign v1 1

3 le v2 v0 10

4 joz v2 10

5 mul v3 v1 v0

6 assign v1 v3

7 add v4 v0 1

8 assign v0 v4

9 jump 3

10 assign v5 v1

11 write v5

12 writeln

13 end

Page 160: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Assembly Language Example (MIPS)

Triple Code MIPS Assembly

1 assign v0 1 li $t0, 1

sw $t0, 24($sp)

2 assign v1 1 li $t0, 1

sw $t0, 28($sp)

3 le v2 v0 10 lw $t1, 24($sp)

li $t2, 10

sle $t0, $t1, $t2

sw $t0, 40($sp)

4 joz v2 10 label 3:

lw $t0, 40($sp)

beq $zero, $t0, label 10

Page 161: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:

I Easier to produce virtual machine code.I Easier to implement “high level features” correctly.I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 162: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:

I Easier to produce virtual machine code.I Easier to implement “high level features” correctly.I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 163: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:

I Easier to produce virtual machine code.I Easier to implement “high level features” correctly.I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 164: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:

I Easier to produce virtual machine code.I Easier to implement “high level features” correctly.I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 165: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:I Easier to produce virtual machine code.

I Easier to implement “high level features” correctly.I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 166: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:I Easier to produce virtual machine code.I Easier to implement “high level features” correctly.

I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 167: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:I Easier to produce virtual machine code.I Easier to implement “high level features” correctly.I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 168: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

I Compiler Output: A sequence of bytes to be interpreted aslow-level instructions by another program.

I Pros:I Easier to produce virtual machine code.I Easier to implement “high level features” correctly.I Not tied to a particular architecture.

I Cons: Poor performance (sometimes).

Page 169: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAPI Many interpreters are stack-based. Bytecode running on a

virtual machine looks like:

I A list of instructions.I An array of memory (for storing variables).I A stack for performing calculations (in lieu of registers).

Page 170: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAPI Many interpreters are stack-based. Bytecode running on a

virtual machine looks like:

I A list of instructions.I An array of memory (for storing variables).I A stack for performing calculations (in lieu of registers).

Page 171: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAP

I Many interpreters are stack-based. Bytecode running on avirtual machine looks like:

I A list of instructions.I An array of memory (for storing variables).I A stack for performing calculations (in lieu of registers).

Page 172: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAPI Many interpreters are stack-based. Bytecode running on a

virtual machine looks like:

I A list of instructions.I An array of memory (for storing variables).I A stack for performing calculations (in lieu of registers).

Page 173: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAPI Many interpreters are stack-based. Bytecode running on a

virtual machine looks like:

I A list of instructions.I An array of memory (for storing variables).I A stack for performing calculations (in lieu of registers).

Page 174: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAPI Many interpreters are stack-based. Bytecode running on a

virtual machine looks like:I A list of instructions.

I An array of memory (for storing variables).I A stack for performing calculations (in lieu of registers).

Page 175: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAPI Many interpreters are stack-based. Bytecode running on a

virtual machine looks like:I A list of instructions.I An array of memory (for storing variables).

I A stack for performing calculations (in lieu of registers).

Page 176: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many languages use a virtual machine or interpreter rather thancompiling to assembly:

I Examples: Java, Python, Lisp

I Examples: Sage, GAPI Many interpreters are stack-based. Bytecode running on a

virtual machine looks like:I A list of instructions.I An array of memory (for storing variables).I A stack for performing calculations (in lieu of registers).

Page 177: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.

I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 178: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 179: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 180: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.

I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 181: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.

I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 182: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 183: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 184: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 185: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Virtual Machines / Interpreters

Many virtual machines use an operand stack to calculate.I Instead of operating on registers:

I Push values onto the stack.I Pop values off, operate on them.I Push the results onto the stack.

I Similar to Reverse Polish Notation.

Example: 5 * (3 + 2) becomes

Instructions Stack State

push 5 5push 3 5, 3push 2 5, 3, 2add 5, (3+2)mul 5(3+2)

Page 186: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Bytecode Example

0 alloc 2 16 get

1 push 0 % Addr. i 17 mul i

2 push 1 % Value := 1 18 store

3 store 19 push 0 % Addr. i

4 push 1 % Addr. prod 20 push 0 % Addr. i

5 push 1 % Value := 1 21 get

6 store 22 push 1 % Value := 1

7 push 0 % WHILE start 23 add i

8 get 24 store

9 push 10 % Value := 10 25 jump 7 % WHILE end

10 le 26 push 1 % Addr. prod

11 joz 26 27 get

12 push 1 % Addr. prod 28 write I

13 push 1 % Addr. prod 29 writeln

14 get 30 end

15 push 0 % Addr. i

Page 187: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Open Problems

1. Obfuscation : Given a program P, how can I modify P tomake it difficult for others to steal my trade secrets,algorithms, etc.?

2. Deobfuscation : Given an obfuscated program P, what can Ilearn about the operations of P?

3. Decompilation : Given an executable E , can I generate sourcecode S that compiles to E ?

Page 188: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Open Problems

1. Obfuscation : Given a program P, how can I modify P tomake it difficult for others to steal my trade secrets,algorithms, etc.?

2. Deobfuscation : Given an obfuscated program P, what can Ilearn about the operations of P?

3. Decompilation : Given an executable E , can I generate sourcecode S that compiles to E ?

Page 189: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Open Problems

1. Obfuscation : Given a program P, how can I modify P tomake it difficult for others to steal my trade secrets,algorithms, etc.?

2. Deobfuscation : Given an obfuscated program P, what can Ilearn about the operations of P?

3. Decompilation : Given an executable E , can I generate sourcecode S that compiles to E ?

Page 190: Compilers and Automata Theory - University of Arizonamath.arizona.edu/~swig/documentation/compiler_theory/slides.pdf · The Automata Viewpoint I A alphabet is a set of symbols. (Ex:

Open Problems

1. Obfuscation : Given a program P, how can I modify P tomake it difficult for others to steal my trade secrets,algorithms, etc.?

2. Deobfuscation : Given an obfuscated program P, what can Ilearn about the operations of P?

3. Decompilation : Given an executable E , can I generate sourcecode S that compiles to E ?