61a lecture 26 announcements programming languages
play

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


  1. 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 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 5

  2. 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 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 5

  3. Parsing

  4. Reading Scheme Lists 7

  5. Reading Scheme Lists A Scheme list is written as elements in parentheses: 7

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

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

  8. 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

  9. 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

  10. 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

  11. Reading Scheme Lists A Scheme list is written as elements in parentheses: A Scheme list (<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

  12. Reading Scheme Lists A Scheme list is written as elements in parentheses: A Scheme list (<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

  13. Reading Scheme Lists A Scheme list is written as elements in parentheses: A Scheme list (<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

  14. Parsing 8

  15. Parsing A Parser takes text and returns an expression 8

  16. Parsing A Parser takes text and returns an expression Text Expression 8

  17. Parsing A Parser takes text and returns an expression Lexical Text Expression analysis 8

  18. Parsing A Parser takes text and returns an expression Lexical Text Tokens Expression analysis 8

  19. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis 8

  20. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' ' (- 23)' ' (* 4 5.6))' 8

  21. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' ' (- 23)' ' (* 4 5.6))' 8

  22. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' ' (* 4 5.6))' 8

  23. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' 8

  24. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' 8

  25. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' 8

  26. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' 8

  27. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' 8

  28. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' • Iterative process 8

  29. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' • Iterative process • Checks for malformed tokens 8

  30. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' • Iterative process • Checks for malformed tokens • Determines types of tokens 8

  31. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' • Iterative process • Checks for malformed tokens • Determines types of tokens • Processes one line at a time 8

  32. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' • Iterative process • Checks for malformed tokens • Determines types of tokens • Processes one line at a time 8

  33. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 Pair('+', Pair(1, ...)) ' (- 23)' '(', '-', 23, ')' ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' • Iterative process • Checks for malformed tokens • Determines types of tokens • Processes one line at a time 8

  34. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 Pair('+', Pair(1, ...)) ' (- 23)' '(', '-', 23, ')' printed as ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' (+ 1 (- 23) (* 4 5.6)) • Iterative process • Checks for malformed tokens • Determines types of tokens • Processes one line at a time 8

  35. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 Pair('+', Pair(1, ...)) ' (- 23)' '(', '-', 23, ')' printed as ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' (+ 1 (- 23) (* 4 5.6)) • Iterative process • Tree-recursive process • Checks for malformed tokens • Determines types of tokens • Processes one line at a time 8

  36. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 Pair('+', Pair(1, ...)) ' (- 23)' '(', '-', 23, ')' printed as ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' (+ 1 (- 23) (* 4 5.6)) • Iterative process • Tree-recursive process • Checks for malformed tokens • Balances parentheses • Determines types of tokens • Processes one line at a time 8

  37. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 Pair('+', Pair(1, ...)) ' (- 23)' '(', '-', 23, ')' printed as ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' (+ 1 (- 23) (* 4 5.6)) • Iterative process • Tree-recursive process • Checks for malformed tokens • Balances parentheses • Determines types of tokens • Returns tree structure • Processes one line at a time 8

  38. Parsing A Parser takes text and returns an expression Lexical Syntactic Text Tokens Expression analysis analysis '(+ 1' '(', '+', 1 Pair('+', Pair(1, ...)) ' (- 23)' '(', '-', 23, ')' printed as ' (* 4 5.6))' '(', '*', 4, 5.6, ')', ')' (+ 1 (- 23) (* 4 5.6)) • Iterative process • Tree-recursive process • Checks for malformed tokens • Balances parentheses • Determines types of tokens • Returns tree structure • Processes one line at a time • Processes multiple lines 8

  39. Syntactic Analysis 9

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

  41. 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

  42. 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

  43. 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

  44. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  45. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  46. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  47. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  48. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  49. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  50. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  51. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them 9

  52. 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 '(', '+', 1, '(', '-', 23, ')', '(', '*', 4, 5.6, ')', ')' Base case : symbols and numbers Recursive call : scheme_read sub-expressions and combine them (Demo) 9

  53. Calculator (Demo)

  54. 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

  55. 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. 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 11

  56. 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. class Pair : >>> s = Pair( 1 , Pair( 2 , Pair( 3 , nil))) """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 11

  57. 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. class Pair : >>> s = Pair( 1 , Pair( 2 , Pair( 3 , nil))) """A Pair has two instance attributes: >>> print (s) first and second. (1 2 3) 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 11

  58. 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. class Pair : >>> s = Pair( 1 , Pair( 2 , Pair( 3 , nil))) """A Pair has two instance attributes: >>> print (s) first and second. (1 2 3) >>> len(s) For a Pair to be a well-formed list, 3 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 11

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

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

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

  62. 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. class Pair : >>> s = Pair( 1 , Pair( 2 , Pair( 3 , nil))) """A Pair has two instance attributes: >>> print (s) first and second. (1 2 3) >>> len(s) For a Pair to be a well-formed list, 3 second is either a well-formed list or nil. >>> print (Pair( 1 , 2 )) Some methods only apply to well-formed lists. (1 . 2) """ >>> print (Pair( 1 , Pair( 2 , 3 ))) def __init__ (self, first, second): (1 2 . 3) self.first = first >>> len(Pair( 1 , Pair( 2 , 3 ))) self.second = second Traceback (most recent call last): ... TypeError: length attempted on improper list Scheme expressions are represented as Scheme lists! Source code is data 11

  63. 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. class Pair : >>> s = Pair( 1 , Pair( 2 , Pair( 3 , nil))) """A Pair has two instance attributes: >>> print (s) first and second. (1 2 3) >>> len(s) For a Pair to be a well-formed list, 3 second is either a well-formed list or nil. >>> print (Pair( 1 , 2 )) Some methods only apply to well-formed lists. (1 . 2) """ >>> print (Pair( 1 , Pair( 2 , 3 ))) def __init__ (self, first, second): (1 2 . 3) self.first = first >>> len(Pair( 1 , Pair( 2 , 3 ))) self.second = second Traceback (most recent call last): ... TypeError: length attempted on improper list Scheme expressions are represented as Scheme lists! Source code is data (Demo) 11

  64. Calculator Syntax 12

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

  66. 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

  67. 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 or more expressions: (+ 1 2 3) (/ 3 (+ 4 5)) 12

  68. 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 or more expressions: (+ 1 2 3) (/ 3 (+ 4 5)) Expressions are represented as Scheme lists (Pair instances) that encode tree structures. 12

  69. 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 or more expressions: (+ 1 2 3) (/ 3 (+ 4 5)) Expressions are represented as Scheme lists (Pair instances) that encode tree structures. Expression (* 3 
 (+ 4 5) 
 (* 6 7 8)) 12

  70. 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 or more expressions: (+ 1 2 3) (/ 3 (+ 4 5)) Expressions are represented as Scheme lists (Pair instances) that encode tree structures. Expression Expression Tree (* 3 
 (+ 4 5) 
 * 3 (* 6 7 8)) + 4 5 * 6 7 8 12

  71. 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 or more expressions: (+ 1 2 3) (/ 3 (+ 4 5)) Expressions are represented as Scheme lists (Pair instances) that encode tree structures. Expression Expression Tree Representation as Pairs first second first second first second first second (* 3 
 * 3 nil (+ 4 5) 
 * 3 first second first second first second first second (* 6 7 8)) * 6 7 8 nil + 4 5 * 6 7 8 first second first second first second + 4 5 nil 12

  72. Calculator Semantics 13

  73. Calculator Semantics The value of a calculator expression is defined recursively. 13

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

  75. 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

  76. 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

  77. 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

  78. 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

  79. 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

Recommend


More recommend