61A Lecture 24 Friday, November 1 Announcements 2 Announcements - - PowerPoint PPT Presentation

61a lecture 24
SMART_READER_LITE
LIVE PREVIEW

61A Lecture 24 Friday, November 1 Announcements 2 Announcements - - PowerPoint PPT Presentation

61A Lecture 24 Friday, November 1 Announcements 2 Announcements Homework 7 due Tuesday 11/5 @ 11:59pm. 2 Announcements Homework 7 due Tuesday 11/5 @ 11:59pm. Project 1 composition revisions due Thursday 11/7 @ 11:59pm. 2 Heard on


slide-1
SLIDE 1

61A Lecture 24

Friday, November 1

slide-2
SLIDE 2

Announcements

2

slide-3
SLIDE 3

Announcements

  • Homework 7 due Tuesday 11/5 @ 11:59pm.

2

slide-4
SLIDE 4

Announcements

  • Homework 7 due Tuesday 11/5 @ 11:59pm.
  • Project 1 composition revisions due Thursday 11/7 @ 11:59pm.

2

slide-5
SLIDE 5

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-6
SLIDE 6

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-7
SLIDE 7

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

What do people fear most about the Dread Pirate Lambda? ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-8
SLIDE 8

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

What do people fear most about the Dread Pirate Lambda? His eval ways! ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-9
SLIDE 9

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

When does the Dread Pirate Lambda finally stop plundering? What do people fear most about the Dread Pirate Lambda? His eval ways! ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-10
SLIDE 10

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

When does the Dread Pirate Lambda finally stop plundering? The base case! What do people fear most about the Dread Pirate Lambda? His eval ways! ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-11
SLIDE 11

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

What did the DPL say when he dropped his fruit overboard? When does the Dread Pirate Lambda finally stop plundering? The base case! What do people fear most about the Dread Pirate Lambda? His eval ways! ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-12
SLIDE 12

Heard on the Dread Pirate Lambda's Fibbonautical Voyage

3

What did the DPL say when he dropped his fruit overboard? (Oh no, I've lost my pear in the seas!) When does the Dread Pirate Lambda finally stop plundering? The base case! What do people fear most about the Dread Pirate Lambda? His eval ways! ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( ) ( )

slide-13
SLIDE 13

Exceptions

slide-14
SLIDE 14

Today's Topic: Handling Errors

5

slide-15
SLIDE 15

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

5

slide-16
SLIDE 16

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type

5

slide-17
SLIDE 17

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type
  • Some resource (such as a file) is not available

5

slide-18
SLIDE 18

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type
  • Some resource (such as a file) is not available
  • A network connection is lost in the middle of data transmission

5

slide-19
SLIDE 19

Today's Topic: Handling Errors

Sometimes, computer programs behave in non-standard ways

  • A function receives an argument value of an improper type
  • Some resource (such as a file) is not available
  • A network connection is lost in the middle of data transmission

Grace Hopper's Notebook, 1947, Moth found in a Mark II Computer

5

slide-20
SLIDE 20

Exceptions

6

slide-21
SLIDE 21

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions

6

slide-22
SLIDE 22

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs.

6

slide-23
SLIDE 23

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs. Exceptions can be handled by the program, preventing the interpreter from halting.

6

slide-24
SLIDE 24

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs. Exceptions can be handled by the program, preventing the interpreter from halting. Unhandled exceptions will cause Python to halt execution and print a stack trace.

6

slide-25
SLIDE 25

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs. Exceptions can be handled by the program, preventing the interpreter from halting. Unhandled exceptions will cause Python to halt execution and print a stack trace. Mastering exceptions:

6

slide-26
SLIDE 26

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs. Exceptions can be handled by the program, preventing the interpreter from halting. Unhandled exceptions will cause Python to halt execution and print a stack trace. Exceptions are objects! They have classes with constructors. Mastering exceptions:

6

slide-27
SLIDE 27

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs. Exceptions can be handled by the program, preventing the interpreter from halting. Unhandled exceptions will cause Python to halt execution and print a stack trace. Exceptions are objects! They have classes with constructors. They enable non-local continuations of control: Mastering exceptions:

6

slide-28
SLIDE 28

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs. Exceptions can be handled by the program, preventing the interpreter from halting. Unhandled exceptions will cause Python to halt execution and print a stack trace. Exceptions are objects! They have classes with constructors. They enable non-local continuations of control: If f calls g and g calls h, exceptions can shift control from h to f without waiting for g to return. Mastering exceptions:

6

slide-29
SLIDE 29

Exceptions

A built-in mechanism in a programming language to declare and respond to exceptional conditions Python raises an exception whenever an error occurs. Exceptions can be handled by the program, preventing the interpreter from halting. Unhandled exceptions will cause Python to halt execution and print a stack trace. Exceptions are objects! They have classes with constructors. They enable non-local continuations of control: If f calls g and g calls h, exceptions can shift control from h to f without waiting for g to return. (Exception handling tends to be slow.) Mastering exceptions:

6

slide-30
SLIDE 30

Raising Exceptions

slide-31
SLIDE 31

Assert Statements

Assert statements raise an exception of type AssertionError

8

slide-32
SLIDE 32

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string>

8

slide-33
SLIDE 33

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag. "O" stands for optimized.

8

slide-34
SLIDE 34

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag. "O" stands for optimized. python3 -O

8

slide-35
SLIDE 35

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag. "O" stands for optimized. python3 -O Whether assertions are enabled is governed by a bool __debug__

8

slide-36
SLIDE 36

Assert Statements

Assert statements raise an exception of type AssertionError assert <expression>, <string> Assertions are designed to be used liberally. They can be ignored to increase efficiency by running Python with the "-O" flag. "O" stands for optimized. python3 -O Whether assertions are enabled is governed by a bool __debug__

8

(Demo)

slide-37
SLIDE 37

Raise Statements

9

slide-38
SLIDE 38

Raise Statements

Exceptions are raised with a raise statement.

9

slide-39
SLIDE 39

Raise Statements

Exceptions are raised with a raise statement. raise <expression>

9

slide-40
SLIDE 40

Raise Statements

Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one.

9

slide-41
SLIDE 41

Raise Statements

Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!')

9

slide-42
SLIDE 42

Raise Statements

Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument

9

slide-43
SLIDE 43

Raise Statements

Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found

9

slide-44
SLIDE 44

Raise Statements

Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary

9

slide-45
SLIDE 45

Raise Statements

Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary RuntimeError -- Catch-all for troubles during interpretation

9

slide-46
SLIDE 46

Raise Statements

Exceptions are raised with a raise statement. raise <expression> <expression> must evaluate to a subclass of BaseException or an instance of one. Exceptions are constructed like any other object. E.g., TypeError('Bad argument!') TypeError -- A function was passed the wrong number/type of argument NameError -- A name wasn't found KeyError -- A key wasn't found in a dictionary RuntimeError -- Catch-all for troubles during interpretation

9

(Demo)

slide-47
SLIDE 47

Try Statements

slide-48
SLIDE 48

Try Statements

11

slide-49
SLIDE 49

Try Statements

Try statements handle exceptions

11

slide-50
SLIDE 50

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

11

slide-51
SLIDE 51

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule:

11

slide-52
SLIDE 52

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first.

11

slide-53
SLIDE 53

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first. If, during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and

11

slide-54
SLIDE 54

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first. If, during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and If the class of the exception inherits from <exception class>, then

11

slide-55
SLIDE 55

Try Statements

Try statements handle exceptions try: <try suite> except <exception class> as <name>: <except suite> ...

Execution rule: The <try suite> is executed first. If, during the course of executing the <try suite>, an exception is raised that is not handled otherwise, and If the class of the exception inherits from <exception class>, then The <except suite> is executed, with <name> bound to the exception.

11

slide-56
SLIDE 56

Handling Exceptions

12

slide-57
SLIDE 57

Handling Exceptions

Exception handling can prevent a program from terminating

12

slide-58
SLIDE 58

Handling Exceptions

Exception handling can prevent a program from terminating >>> try:

12

slide-59
SLIDE 59

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0

12

slide-60
SLIDE 60

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e:

12

slide-61
SLIDE 61

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e))

12

slide-62
SLIDE 62

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0

12

slide-63
SLIDE 63

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'>

12

slide-64
SLIDE 64

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x

12

slide-65
SLIDE 65

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x

12

slide-66
SLIDE 66

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x Multiple try statements: Control jumps to the except suite of the most recent try statement that handles that type of exception.

12

slide-67
SLIDE 67

Handling Exceptions

Exception handling can prevent a program from terminating >>> try: x = 1/0 except ZeroDivisionError as e: print('handling a', type(e)) x = 0 handling a <class 'ZeroDivisionError'> >>> x Multiple try statements: Control jumps to the except suite of the most recent try statement that handles that type of exception.

12

(Demo)

slide-68
SLIDE 68

WWPD: What Would Python Do?

How will the Python interpreter respond?

13

slide-69
SLIDE 69

WWPD: What Would Python Do?

How will the Python interpreter respond?

13

slide-70
SLIDE 70

WWPD: What Would Python Do?

How will the Python interpreter respond? def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

13

slide-71
SLIDE 71

WWPD: What Would Python Do?

How will the Python interpreter respond? >>> invert_safe(1/0) def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

13

slide-72
SLIDE 72

WWPD: What Would Python Do?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

13

slide-73
SLIDE 73

WWPD: What Would Python Do?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

13

slide-74
SLIDE 74

WWPD: What Would Python Do?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) ... except ZeroDivisionError as e: def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

13

slide-75
SLIDE 75

WWPD: What Would Python Do?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) ... except ZeroDivisionError as e: ... print('Handled!') def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

13

slide-76
SLIDE 76

WWPD: What Would Python Do?

How will the Python interpreter respond? >>> invert_safe(1/0) >>> try: ... invert_safe(0) ... except ZeroDivisionError as e: ... print('Handled!') >>> inverrrrt_safe(1/0) def invert(x): result = 1/x # Raises a ZeroDivisionError if x is 0 print('Never printed if x is 0') return result def invert_safe(x): try: return invert(x) except ZeroDivisionError as e: return str(e)

13

slide-77
SLIDE 77

Interpreters

slide-78
SLIDE 78

Reading Scheme Lists

15

slide-79
SLIDE 79

A Scheme list is written as elements in parentheses:

Reading Scheme Lists

15

slide-80
SLIDE 80

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

Reading Scheme Lists

15

slide-81
SLIDE 81

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

Reading Scheme Lists

15

slide-82
SLIDE 82

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))

Reading Scheme Lists

15

slide-83
SLIDE 83

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.

Reading Scheme Lists

15

slide-84
SLIDE 84

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. Parsers must validate that expressions are well-formed.

Reading Scheme Lists

15

slide-85
SLIDE 85

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. Parsers must validate that expressions are well-formed.

Reading Scheme Lists

(Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html

15

slide-86
SLIDE 86

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. Parsers must validate that expressions are well-formed.

Reading Scheme Lists

(Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html

15

A recursive Scheme list

slide-87
SLIDE 87

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. Parsers must validate that expressions are well-formed.

Reading Scheme Lists

(Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html

15

A recursive Scheme list

slide-88
SLIDE 88

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. Parsers must validate that expressions are well-formed.

Reading Scheme Lists

(Demo) http://composingprograms.com/projects/scalc/scheme_reader.py.html

15

A recursive Scheme list

slide-89
SLIDE 89

Parsing

slide-90
SLIDE 90

Parsing

17

slide-91
SLIDE 91

Parsing

A Parser takes text and returns an expression.

17

slide-92
SLIDE 92

Parsing

A Parser takes text and returns an expression.

17

Text Expression

slide-93
SLIDE 93

Parsing

A Parser takes text and returns an expression.

17

Text Expression Lexical analysis

slide-94
SLIDE 94

Parsing

A Parser takes text and returns an expression.

17

Text Expression Lexical analysis Tokens

slide-95
SLIDE 95

Parsing

A Parser takes text and returns an expression.

17

Text Expression Lexical analysis Tokens Syntactic analysis

slide-96
SLIDE 96

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

slide-97
SLIDE 97

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

slide-98
SLIDE 98

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

'(', '+', 1

slide-99
SLIDE 99

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

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

slide-100
SLIDE 100

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

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

slide-101
SLIDE 101

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

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

slide-102
SLIDE 102

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

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

slide-103
SLIDE 103

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

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

slide-104
SLIDE 104

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

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

  • Iterative process
slide-105
SLIDE 105

Parsing

A Parser takes text and returns an expression.

17

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

Text Expression Lexical analysis Tokens Syntactic analysis

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

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

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-107
SLIDE 107

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-108
SLIDE 108

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-109
SLIDE 109

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-110
SLIDE 110

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-111
SLIDE 111

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-112
SLIDE 112

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-113
SLIDE 113

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-114
SLIDE 114

Parsing

A Parser takes text and returns an expression.

17

'(+ 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-115
SLIDE 115

Recursive Syntactic Analysis

18

slide-116
SLIDE 116

Recursive Syntactic Analysis

A predictive recursive descent parser inspects only k tokens to decide how to proceed, for some fixed k.

18

slide-117
SLIDE 117

Recursive Syntactic Analysis

A predictive recursive descent parser inspects only k tokens to decide how to proceed, for some fixed k. Can English be parsed via predictive recursive descent?

18

slide-118
SLIDE 118

Recursive Syntactic Analysis

A predictive recursive descent parser inspects only k tokens to decide how to proceed, for some fixed k. Can English be parsed via predictive recursive descent? The horse raced past the barn fell.

18

slide-119
SLIDE 119

Recursive Syntactic Analysis

A predictive recursive descent parser inspects only k tokens to decide how to proceed, for some fixed k. Can English be parsed via predictive recursive descent? The horse raced past the barn fell. ridden

18

slide-120
SLIDE 120

Recursive Syntactic Analysis

A predictive recursive descent parser inspects only k tokens to decide how to proceed, for some fixed k. Can English be parsed via predictive recursive descent? The horse raced past the barn fell. ridden ( t h a t w a s )

18

slide-121
SLIDE 121

Recursive Syntactic Analysis

A predictive recursive descent parser inspects only k tokens to decide how to proceed, for some fixed k. Can English be parsed via predictive recursive descent? The horse raced past the barn fell. ridden ( t h a t w a s ) sentence subject

18

slide-122
SLIDE 122

Syntactic Analysis

19

slide-123
SLIDE 123

Syntactic Analysis

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

19

slide-124
SLIDE 124

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.

19

slide-125
SLIDE 125

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

19

slide-126
SLIDE 126

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

19

slide-127
SLIDE 127

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

19

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

slide-128
SLIDE 128

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

19

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

slide-129
SLIDE 129

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

19

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

slide-130
SLIDE 130

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

19

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

slide-131
SLIDE 131

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

19

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

slide-132
SLIDE 132

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

19

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

slide-133
SLIDE 133

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

19

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

slide-134
SLIDE 134

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

19

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

slide-135
SLIDE 135

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

19

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