61A Lecture 26 Announcements Programming Languages Programming - - PowerPoint PPT Presentation

61a lecture 26 announcements programming languages
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 26 Announcements Programming Languages Programming - - PowerPoint PPT Presentation

61A Lecture 26 Announcements Programming Languages Programming Languages 4 Programming Languages A computer typically executes programs written in many different programming languages 4 Programming Languages A computer typically executes


slide-1
SLIDE 1

61A Lecture 26

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Programming Languages

slide-4
SLIDE 4

Programming Languages

4

slide-5
SLIDE 5

Programming Languages

A computer typically executes programs written in many different programming languages

4

slide-6
SLIDE 6

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

slide-7
SLIDE 7

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

slide-8
SLIDE 8

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

  • Operations refer to specific hardware memory addresses; no abstraction mechanisms
slide-9
SLIDE 9

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

  • Operations refer to specific hardware memory addresses; no abstraction mechanisms

High-level languages: statements & expressions are interpreted by another program or compiled (translated) into another language

slide-10
SLIDE 10

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

  • Operations refer to specific hardware memory addresses; no abstraction mechanisms

High-level languages: statements & expressions are interpreted by another program or compiled (translated) into another language

  • Provide means of abstraction such as naming, function definition, and objects
slide-11
SLIDE 11

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

  • Operations refer to specific hardware memory addresses; no abstraction mechanisms

High-level languages: statements & expressions are interpreted by another program or compiled (translated) into another language

  • Provide means of abstraction such as naming, function definition, and objects
  • Abstract away system details to be independent of hardware and operating system
slide-12
SLIDE 12

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

  • Operations refer to specific hardware memory addresses; no abstraction mechanisms

High-level languages: statements & expressions are interpreted by another program or compiled (translated) into another language

  • Provide means of abstraction such as naming, function definition, and objects
  • Abstract away system details to be independent of hardware and operating system

def square(x): return x * x Python 3

slide-13
SLIDE 13

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

  • Operations refer to specific hardware memory addresses; no abstraction mechanisms

High-level languages: statements & expressions are interpreted by another program or compiled (translated) into another language

  • Provide means of abstraction such as naming, function definition, and objects
  • Abstract away system details to be independent of hardware and operating system

def square(x): return x * x Python 3 LOAD_FAST 0 (x) LOAD_FAST 0 (x) BINARY_MULTIPLY RETURN_VALUE Python 3 Byte Code

slide-14
SLIDE 14

Programming Languages

A computer typically executes programs written in many different programming languages

4

Machine languages: statements are interpreted by the hardware itself

  • A fixed set of instructions invoke operations implemented by the circuitry of the

central processing unit (CPU)

  • Operations refer to specific hardware memory addresses; no abstraction mechanisms

High-level languages: statements & expressions are interpreted by another program or compiled (translated) into another language

  • Provide means of abstraction such as naming, function definition, and objects
  • Abstract away system details to be independent of hardware and operating system

from dis import dis dis(square) def square(x): return x * x Python 3 LOAD_FAST 0 (x) LOAD_FAST 0 (x) BINARY_MULTIPLY RETURN_VALUE Python 3 Byte Code

slide-15
SLIDE 15

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

slide-16
SLIDE 16

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections

slide-17
SLIDE 17

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections Problem domain: The MediaWiki mark-up language was designed for generating static web

  • pages. It has built-in elements for text formatting and cross-page linking. It is used, for

example, to create Wikipedia pages

slide-18
SLIDE 18

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections Problem domain: The MediaWiki mark-up language was designed for generating static web

  • pages. It has built-in elements for text formatting and cross-page linking. It is used, for

example, to create Wikipedia pages A programming language has:

slide-19
SLIDE 19

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections Problem domain: The MediaWiki mark-up language was designed for generating static web

  • pages. It has built-in elements for text formatting and cross-page linking. It is used, for

example, to create Wikipedia pages A programming language has:

  • Syntax: The legal statements and expressions in the language
slide-20
SLIDE 20

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections Problem domain: The MediaWiki mark-up language was designed for generating static web

  • pages. It has built-in elements for text formatting and cross-page linking. It is used, for

example, to create Wikipedia pages A programming language has:

  • Syntax: The legal statements and expressions in the language
  • Semantics: The execution/evaluation rule for those statements and expressions
slide-21
SLIDE 21

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections Problem domain: The MediaWiki mark-up language was designed for generating static web

  • pages. It has built-in elements for text formatting and cross-page linking. It is used, for

example, to create Wikipedia pages A programming language has:

  • Syntax: The legal statements and expressions in the language
  • Semantics: The execution/evaluation rule for those statements and expressions

To create a new programming language, you either need a:

slide-22
SLIDE 22

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections Problem domain: The MediaWiki mark-up language was designed for generating static web

  • pages. It has built-in elements for text formatting and cross-page linking. It is used, for

example, to create Wikipedia pages A programming language has:

  • Syntax: The legal statements and expressions in the language
  • Semantics: The execution/evaluation rule for those statements and expressions

To create a new programming language, you either need a:

  • Specification: A document describe the precise syntax and semantics of the language
slide-23
SLIDE 23

Metalinguistic Abstraction

A powerful form of abstraction is to define a new language that is tailored to a particular type of application or problem domain

5

Type of application: Erlang was designed for concurrent programs. It has built-in elements for expressing concurrent communication. It is used, for example, to implement chat servers with many simultaneous connections Problem domain: The MediaWiki mark-up language was designed for generating static web

  • pages. It has built-in elements for text formatting and cross-page linking. It is used, for

example, to create Wikipedia pages A programming language has:

  • Syntax: The legal statements and expressions in the language
  • Semantics: The execution/evaluation rule for those statements and expressions

To create a new programming language, you either need a:

  • Specification: A document describe the precise syntax and semantics of the language
  • Canonical Implementation: An interpreter or compiler for the language
slide-24
SLIDE 24

Parsing

slide-25
SLIDE 25

Reading Scheme Lists

7

slide-26
SLIDE 26

Reading Scheme Lists

A Scheme list is written as elements in parentheses:

7

slide-27
SLIDE 27

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>)

7

slide-28
SLIDE 28

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive

7

slide-29
SLIDE 29

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6))

7

slide-30
SLIDE 30

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself

7

slide-31
SLIDE 31

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself (Demo) http://composingprograms.com/examples/scalc/scheme_reader.py.html

7

slide-32
SLIDE 32

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself (Demo) http://composingprograms.com/examples/scalc/scheme_reader.py.html

7

A Scheme list

slide-33
SLIDE 33

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself (Demo) http://composingprograms.com/examples/scalc/scheme_reader.py.html

7

A Scheme list

slide-34
SLIDE 34

Reading Scheme Lists

A Scheme list is written as elements in parentheses: (<element_0> <element_1> ... <element_n>) Each <element> can be a combination or primitive (+ (* 3 (+ (* 2 4) (+ 3 5))) (+ (- 10 7) 6)) The task of parsing a language involves coercing a string representation of an expression to the expression itself (Demo) http://composingprograms.com/examples/scalc/scheme_reader.py.html

7

A Scheme list

slide-35
SLIDE 35

Parsing

8

slide-36
SLIDE 36

Parsing

A Parser takes text and returns an expression

8

slide-37
SLIDE 37

Parsing

A Parser takes text and returns an expression

8

Text Expression

slide-38
SLIDE 38

Parsing

A Parser takes text and returns an expression

8

Text Expression Lexical analysis

slide-39
SLIDE 39

Parsing

A Parser takes text and returns an expression

8

Text Expression Lexical analysis Tokens

slide-40
SLIDE 40

Parsing

A Parser takes text and returns an expression

8

Text Expression Lexical analysis Tokens Syntactic analysis

slide-41
SLIDE 41

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

slide-42
SLIDE 42

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

slide-43
SLIDE 43

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1

slide-44
SLIDE 44

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')'

slide-45
SLIDE 45

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')'

slide-46
SLIDE 46

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')'

slide-47
SLIDE 47

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')'

slide-48
SLIDE 48

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')'

slide-49
SLIDE 49

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')'

  • Iterative process
slide-50
SLIDE 50

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')'

  • Iterative process
  • Checks for malformed tokens
slide-51
SLIDE 51

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')'

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
slide-52
SLIDE 52

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')'

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
slide-53
SLIDE 53

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')'

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
slide-54
SLIDE 54

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')' Pair('+', Pair(1, ...))

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
slide-55
SLIDE 55

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')' Pair('+', Pair(1, ...)) (+ 1 (- 23) (* 4 5.6))

printed as

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
slide-56
SLIDE 56

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')' Pair('+', Pair(1, ...)) (+ 1 (- 23) (* 4 5.6))

printed as

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
  • Tree-recursive process
slide-57
SLIDE 57

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')' Pair('+', Pair(1, ...)) (+ 1 (- 23) (* 4 5.6))

printed as

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
  • Tree-recursive process
  • Balances parentheses
slide-58
SLIDE 58

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')' Pair('+', Pair(1, ...)) (+ 1 (- 23) (* 4 5.6))

printed as

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
  • Tree-recursive process
  • Balances parentheses
  • Returns tree structure
slide-59
SLIDE 59

Parsing

A Parser takes text and returns an expression

8

'(+ 1' ' (- 23)' ' (* 4 5.6))'

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1 '(', '-', 23, ')' '(', '*', 4, 5.6, ')', ')' Pair('+', Pair(1, ...)) (+ 1 (- 23) (* 4 5.6))

printed as

  • Iterative process
  • Checks for malformed tokens
  • Determines types of tokens
  • Processes one line at a time
  • Tree-recursive process
  • Balances parentheses
  • Returns tree structure
  • Processes multiple lines
slide-60
SLIDE 60

Syntactic Analysis

9

slide-61
SLIDE 61

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested

9

slide-62
SLIDE 62

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression

9

slide-63
SLIDE 63

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers

9

slide-64
SLIDE 64

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

slide-65
SLIDE 65

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-66
SLIDE 66

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-67
SLIDE 67

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-68
SLIDE 68

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-69
SLIDE 69

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-70
SLIDE 70

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-71
SLIDE 71

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-72
SLIDE 72

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')'

slide-73
SLIDE 73

Syntactic Analysis

Syntactic analysis identifies the hierarchical structure of an expression, which may be nested Each call to scheme_read consumes the input tokens for exactly one expression Base case: symbols and numbers Recursive call: scheme_read sub-expressions and combine them

9

'(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' (Demo)

slide-74
SLIDE 74

Calculator

(Demo)

slide-75
SLIDE 75

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

slide-76
SLIDE 76

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

slide-77
SLIDE 77

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

slide-78
SLIDE 78

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) >>> print(s) (1 2 3) class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

slide-79
SLIDE 79

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) >>> print(s) (1 2 3) >>> len(s) 3 class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

slide-80
SLIDE 80

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) >>> print(s) (1 2 3) >>> len(s) 3 >>> print(Pair(1, 2)) (1 . 2) class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

slide-81
SLIDE 81

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) >>> print(s) (1 2 3) >>> len(s) 3 >>> print(Pair(1, 2)) (1 . 2) >>> print(Pair(1, Pair(2, 3))) (1 2 . 3) class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

slide-82
SLIDE 82

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) >>> print(s) (1 2 3) >>> len(s) 3 >>> print(Pair(1, 2)) (1 . 2) >>> print(Pair(1, Pair(2, 3))) (1 2 . 3) >>> len(Pair(1, Pair(2, 3))) Traceback (most recent call last): ... TypeError: length attempted on improper list class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

slide-83
SLIDE 83

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) >>> print(s) (1 2 3) >>> len(s) 3 >>> print(Pair(1, 2)) (1 . 2) >>> print(Pair(1, Pair(2, 3))) (1 2 . 3) >>> len(Pair(1, Pair(2, 3))) Traceback (most recent call last): ... TypeError: length attempted on improper list class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

Scheme expressions are represented as Scheme lists! Source code is data

slide-84
SLIDE 84

The Pair Class

The Pair class represents Scheme pairs and lists. A list is a pair whose second element is either a list or nil.

11

>>> s = Pair(1, Pair(2, Pair(3, nil))) >>> print(s) (1 2 3) >>> len(s) 3 >>> print(Pair(1, 2)) (1 . 2) >>> print(Pair(1, Pair(2, 3))) (1 2 . 3) >>> len(Pair(1, Pair(2, 3))) Traceback (most recent call last): ... TypeError: length attempted on improper list class Pair: """A Pair has two instance attributes: first and second. For a Pair to be a well-formed list, second is either a well-formed list or nil. Some methods only apply to well-formed lists. """ def __init__(self, first, second): self.first = first self.second = second

Scheme expressions are represented as Scheme lists! Source code is data (Demo)

slide-85
SLIDE 85

Calculator Syntax

12

slide-86
SLIDE 86

Calculator Syntax

The Calculator language has primitive expressions and call expressions. (That's it!)

12

slide-87
SLIDE 87

Calculator Syntax

The Calculator language has primitive expressions and call expressions. (That's it!) A primitive expression is a number: 2 -4 5.6

12

slide-88
SLIDE 88

Calculator Syntax

The Calculator language has primitive expressions and call expressions. (That's it!) A primitive expression is a number: 2 -4 5.6 A call expression is a combination that begins with an operator (+, -, *, /) followed by 0

  • r more expressions: (+ 1 2 3) (/ 3 (+ 4 5))

12

slide-89
SLIDE 89

Calculator Syntax

The Calculator language has primitive expressions and call expressions. (That's it!) A primitive expression is a number: 2 -4 5.6 A call expression is a combination that begins with an operator (+, -, *, /) followed by 0

  • r more expressions: (+ 1 2 3) (/ 3 (+ 4 5))

12

Expressions are represented as Scheme lists (Pair instances) that encode tree structures.

slide-90
SLIDE 90

Calculator Syntax

The Calculator language has primitive expressions and call expressions. (That's it!) A primitive expression is a number: 2 -4 5.6 A call expression is a combination that begins with an operator (+, -, *, /) followed by 0

  • r more expressions: (+ 1 2 3) (/ 3 (+ 4 5))

12

Expressions are represented as Scheme lists (Pair instances) that encode tree structures. (* 3 
 (+ 4 5)
 (* 6 7 8)) Expression

slide-91
SLIDE 91

Calculator Syntax

The Calculator language has primitive expressions and call expressions. (That's it!) A primitive expression is a number: 2 -4 5.6 A call expression is a combination that begins with an operator (+, -, *, /) followed by 0

  • r more expressions: (+ 1 2 3) (/ 3 (+ 4 5))

12

Expressions are represented as Scheme lists (Pair instances) that encode tree structures. (* 3 
 (+ 4 5)
 (* 6 7 8)) Expression Expression Tree * 3 + 4 5 * 6 8 7

slide-92
SLIDE 92

Calculator Syntax

The Calculator language has primitive expressions and call expressions. (That's it!) A primitive expression is a number: 2 -4 5.6 A call expression is a combination that begins with an operator (+, -, *, /) followed by 0

  • r more expressions: (+ 1 2 3) (/ 3 (+ 4 5))

12

Expressions are represented as Scheme lists (Pair instances) that encode tree structures. (* 3 
 (+ 4 5)
 (* 6 7 8)) Expression

second first

*

second first

3

second first second first

nil

second first

+

second first

4

second first

5 nil

second first

*

second first

6

second first

7

second first

8 nil

Representation as Pairs Expression Tree * 3 + 4 5 * 6 8 7

slide-93
SLIDE 93

Calculator Semantics

13

slide-94
SLIDE 94

Calculator Semantics

The value of a calculator expression is defined recursively.

13

slide-95
SLIDE 95

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself.

13

slide-96
SLIDE 96

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator.

13

slide-97
SLIDE 97

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments

13

slide-98
SLIDE 98

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

13

slide-99
SLIDE 99

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

  • : If one argument, negate it. If more than one, subtract the rest from the first.

13

slide-100
SLIDE 100

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

  • : If one argument, negate it. If more than one, subtract the rest from the first.

/: If one argument, invert it. If more than one, divide the rest from the first.

13

slide-101
SLIDE 101

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

  • : If one argument, negate it. If more than one, subtract the rest from the first.

/: If one argument, invert it. If more than one, divide the rest from the first.

13

(+ 5 
 (* 2 3)
 (* 2 5 5)) Expression

slide-102
SLIDE 102

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

  • : If one argument, negate it. If more than one, subtract the rest from the first.

/: If one argument, invert it. If more than one, divide the rest from the first.

13

(+ 5 
 (* 2 3)
 (* 2 5 5)) Expression Expression Tree + 5 * 2 3 * 2 5 5

slide-103
SLIDE 103

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

  • : If one argument, negate it. If more than one, subtract the rest from the first.

/: If one argument, invert it. If more than one, divide the rest from the first.

13

(+ 5 
 (* 2 3)
 (* 2 5 5)) Expression Expression Tree + 5 * 2 3 * 2 5 5 6

slide-104
SLIDE 104

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

  • : If one argument, negate it. If more than one, subtract the rest from the first.

/: If one argument, invert it. If more than one, divide the rest from the first.

13

(+ 5 
 (* 2 3)
 (* 2 5 5)) Expression Expression Tree + 5 * 2 3 * 2 5 5 50 6

slide-105
SLIDE 105

Calculator Semantics

The value of a calculator expression is defined recursively. Primitive: A number evaluates to itself. Call: A call expression evaluates to its argument values combined by an operator. +: Sum of the arguments *: Product of the arguments

  • : If one argument, negate it. If more than one, subtract the rest from the first.

/: If one argument, invert it. If more than one, divide the rest from the first.

13

(+ 5 
 (* 2 3)
 (* 2 5 5)) Expression Expression Tree + 5 * 2 3 * 2 5 5 50 6 61

slide-106
SLIDE 106

Evaluation

slide-107
SLIDE 107

The Eval Function

15

slide-108
SLIDE 108

The Eval Function

The eval function computes the value of an expression, which is always a number

15

slide-109
SLIDE 109

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

slide-110
SLIDE 110

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

Implementation Language Semantics

slide-111
SLIDE 111

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError Implementation Language Semantics

slide-112
SLIDE 112

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... Implementation Language Semantics

slide-113
SLIDE 113

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... to itself Implementation Language Semantics

slide-114
SLIDE 114

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... A call expression evaluates... to itself Implementation Language Semantics

slide-115
SLIDE 115

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... A call expression evaluates... to its argument values to itself Implementation Language Semantics

slide-116
SLIDE 116

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... A call expression evaluates... to its argument values to itself combined by an operator Implementation Language Semantics

slide-117
SLIDE 117

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... A call expression evaluates... to its argument values to itself Recursive call returns a number for each operand combined by an operator Implementation Language Semantics

slide-118
SLIDE 118

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... A call expression evaluates... to its argument values to itself Recursive call returns a number for each operand combined by an operator Implementation Language Semantics

slide-119
SLIDE 119

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... A call expression evaluates... to its argument values to itself '+', '-', '*', '/' Recursive call returns a number for each operand combined by an operator Implementation Language Semantics

slide-120
SLIDE 120

The Eval Function

The eval function computes the value of an expression, which is always a number It is a generic function that dispatches on the type of the expression (primitive or call)

15

def calc_eval(exp): if type(exp) in (int, float): return exp elif isinstance(exp, Pair): arguments = exp.second.map(calc_eval) return calc_apply(exp.first, arguments) else: raise TypeError A number evaluates... A call expression evaluates... to its argument values to itself '+', '-', '*', '/' A Scheme list

  • f numbers

Recursive call returns a number for each operand combined by an operator Implementation Language Semantics

slide-121
SLIDE 121

Applying Built-in Operators

16

slide-122
SLIDE 122

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values

16

slide-123
SLIDE 123

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values In calculator, all operations are named by built-in operators: +, -, *, /

16

slide-124
SLIDE 124

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values In calculator, all operations are named by built-in operators: +, -, *, /

16

Implementation Language Semantics

slide-125
SLIDE 125

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values In calculator, all operations are named by built-in operators: +, -, *, /

16

def calc_apply(operator, args): if operator == '+': return reduce(add, args, 0) elif operator == '-': ... elif operator == '*': ... elif operator == '/': ... else: raise TypeError Implementation Language Semantics

slide-126
SLIDE 126

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values In calculator, all operations are named by built-in operators: +, -, *, /

16

def calc_apply(operator, args): if operator == '+': return reduce(add, args, 0) elif operator == '-': ... elif operator == '*': ... elif operator == '/': ... else: raise TypeError +: Implementation Language Semantics

slide-127
SLIDE 127

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values In calculator, all operations are named by built-in operators: +, -, *, /

16

def calc_apply(operator, args): if operator == '+': return reduce(add, args, 0) elif operator == '-': ... elif operator == '*': ... elif operator == '/': ... else: raise TypeError Sum of the arguments +: Implementation Language Semantics

slide-128
SLIDE 128

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values In calculator, all operations are named by built-in operators: +, -, *, /

16

def calc_apply(operator, args): if operator == '+': return reduce(add, args, 0) elif operator == '-': ... elif operator == '*': ... elif operator == '/': ... else: raise TypeError Sum of the arguments +: Implementation Language Semantics ...

  • :

...

slide-129
SLIDE 129

Applying Built-in Operators

The apply function applies some operation to a (Scheme) list of argument values In calculator, all operations are named by built-in operators: +, -, *, /

16

def calc_apply(operator, args): if operator == '+': return reduce(add, args, 0) elif operator == '-': ... elif operator == '*': ... elif operator == '/': ... else: raise TypeError Sum of the arguments +: Implementation Language Semantics ...

  • :

... (Demo)

slide-130
SLIDE 130

Interactive Interpreters

slide-131
SLIDE 131

Read-Eval-Print Loop

18

slide-132
SLIDE 132

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter

18

slide-133
SLIDE 133

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter 1. Print a prompt

18

slide-134
SLIDE 134

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter 1. Print a prompt 2. Read text input from the user

18

slide-135
SLIDE 135

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter 1. Print a prompt 2. Read text input from the user 3. Parse the text input into an expression

18

slide-136
SLIDE 136

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter 1. Print a prompt 2. Read text input from the user 3. Parse the text input into an expression 4. Evaluate the expression

18

slide-137
SLIDE 137

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter 1. Print a prompt 2. Read text input from the user 3. Parse the text input into an expression 4. Evaluate the expression 5. If any errors occur, report those errors, otherwise

18

slide-138
SLIDE 138

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter 1. Print a prompt 2. Read text input from the user 3. Parse the text input into an expression 4. Evaluate the expression 5. If any errors occur, report those errors, otherwise 6. Print the value of the expression and repeat

18

slide-139
SLIDE 139

Read-Eval-Print Loop

The user interface for many programming languages is an interactive interpreter 1. Print a prompt 2. Read text input from the user 3. Parse the text input into an expression 4. Evaluate the expression 5. If any errors occur, report those errors, otherwise 6. Print the value of the expression and repeat

18

(Demo)

slide-140
SLIDE 140

Raising Exceptions

19

slide-141
SLIDE 141

Raising Exceptions

Exceptions are raised within lexical analysis, syntactic analysis, eval, and apply

19

slide-142
SLIDE 142

Raising Exceptions

Exceptions are raised within lexical analysis, syntactic analysis, eval, and apply Example exceptions

19

slide-143
SLIDE 143

Raising Exceptions

Exceptions are raised within lexical analysis, syntactic analysis, eval, and apply Example exceptions

  • Lexical analysis: The token 2.3.4 raises ValueError("invalid numeral")

19

slide-144
SLIDE 144

Raising Exceptions

Exceptions are raised within lexical analysis, syntactic analysis, eval, and apply Example exceptions

  • Lexical analysis: The token 2.3.4 raises ValueError("invalid numeral")
  • Syntactic analysis: An extra ) raises SyntaxError("unexpected token")

19

slide-145
SLIDE 145

Raising Exceptions

Exceptions are raised within lexical analysis, syntactic analysis, eval, and apply Example exceptions

  • Lexical analysis: The token 2.3.4 raises ValueError("invalid numeral")
  • Syntactic analysis: An extra ) raises SyntaxError("unexpected token")
  • Eval: An empty combination raises TypeError("() is not a number or call expression")

19

slide-146
SLIDE 146

Raising Exceptions

Exceptions are raised within lexical analysis, syntactic analysis, eval, and apply Example exceptions

  • Lexical analysis: The token 2.3.4 raises ValueError("invalid numeral")
  • Syntactic analysis: An extra ) raises SyntaxError("unexpected token")
  • Eval: An empty combination raises TypeError("() is not a number or call expression")
  • Apply: No arguments to - raises TypeError("- requires at least 1 argument")

19

slide-147
SLIDE 147

Raising Exceptions

Exceptions are raised within lexical analysis, syntactic analysis, eval, and apply Example exceptions

  • Lexical analysis: The token 2.3.4 raises ValueError("invalid numeral")
  • Syntactic analysis: An extra ) raises SyntaxError("unexpected token")
  • Eval: An empty combination raises TypeError("() is not a number or call expression")
  • Apply: No arguments to - raises TypeError("- requires at least 1 argument")

19

(Demo)

slide-148
SLIDE 148

Handling Exceptions

20

slide-149
SLIDE 149

Handling Exceptions

An interactive interpreter prints information about each error

20

slide-150
SLIDE 150

Handling Exceptions

An interactive interpreter prints information about each error A well-designed interactive interpreter should not halt completely on an error, so that the user has an opportunity to try again in the current environment

20

slide-151
SLIDE 151

Handling Exceptions

An interactive interpreter prints information about each error A well-designed interactive interpreter should not halt completely on an error, so that the user has an opportunity to try again in the current environment

20

(Demo)