CS61A Lecture 22 Interpretation Jom Magrotker UC Berkeley EECS July 25, 2012.

Slides:



Advertisements
Similar presentations
CS61A Lecture 25 Delayed Sequences Jom Magrotker UC Berkeley EECS July 31, 2012.
Advertisements

CS61A Lecture 8 Data Abstraction Tom Magrino and Jon Kotker UC Berkeley EECS June 28, 2012.
1 CS101 Introduction to Computing Lecture 17 Algorithms II.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
CS252: Systems Programming Ninghui Li Topic 4: Regular Expressions and Lexical Analysis.
1 Lecture-2 CSIT-120 Spring 2001 Revision of Lecture-1 Introducing Computer Architecture The FOUR Main Elements Fetch-Execute Cycle A Look Under the Hood.
Environments and Evaluation
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
1 Lecture-2 CS-120 Fall 2000 Revision of Lecture-1 Introducing Computer Architecture The FOUR Main Elements Fetch-Execute Cycle A Look Under the Hood.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Introduction to Python
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CS61A Lecture 14 Object-Oriented Programming Jom Magrotker UC Berkeley EECS July 11, 2012.
Computer Science 101 Introduction to Programming.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
CIS Computer Programming Logic
Summer 2014 Chapter 1: Basic Concepts. Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Chapter Overview Welcome to Assembly Language.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Computer Science 101 Introduction to Programming.
CS61A Lecture 21 Scheme Jom Magrotker UC Berkeley EECS July 24, 2012.
Software Overview. Why review software? Software is the set of instructions that tells hardware what to do The reason for hardware is to execute a program.
CS61A Lecture 15 Object-Oriented Programming, Mutable Data Structures Jom Magrotker UC Berkeley EECS July 12, 2012.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
What does a computer program look like: a general overview.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS61A Lecture 12 Immutable Trees Jom Magrotker UC Berkeley EECS July 9, 2012.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
CS61A Lecture 23 Py Jom Magrotker UC Berkeley EECS July 26, 2012.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS61A Lecture 20 Object-Oriented Programming: Implementation Jom Magrotker UC Berkeley EECS July 23, 2012.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CS61A Lecture 9 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 2, 2012.
CS61A Lecture 10 Immutable Data Structures Jom Magrotker UC Berkeley EECS July 3, 2012.
CS61A Lecture 11 Immutable Trees Jom Magrotker UC Berkeley EECS July 5, 2012.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
CompSci 230 S Programming Techniques
Advanced Computer Systems
Topics Designing a Program Input, Processing, and Output
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
A Simple Syntax-Directed Translator
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
2.5 Another Java Application: Adding Integers
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
PROGRAMMING LANGUAGES
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 Elementary Programming
Python Primer 2: Functions and Control Flow
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Coding Concepts (Basics)
Adapted from slides by Nicholas Shahan and Dan Grossman
Topics Designing a Program Input, Processing, and Output
Nicholas Shahan Spring 2016
Topics Designing a Program Input, Processing, and Output
6.001 SICP Interpretation Parts of an interpreter
Lecture 2 - Names & Functions
Presentation transcript:

CS61A Lecture 22 Interpretation Jom Magrotker UC Berkeley EECS July 25, 2012

2 C OMPUTER S CIENCE IN THE N EWS

3 T ODAY Interpretation: Basics Calculator Language Review: Scheme Lists

4 P ROGRAMMING L ANGUAGES Computer software today is written in a variety of programming languages.

5 P ROGRAMMING L ANGUAGES Computers can only work with 0 s and 1 s. In machine language, all data are represented as sequences of bits, all statements are primitive instructions ( ADD, DIV, JUMP ) that can be interpreted directly by hardware.

6 P ROGRAMMING L ANGUAGES High-level languages like Python allow a user to specify instructions in a more human-readable language, which eventually gets translated into machine language, are evaluated in software, not hardware, and are built on top of low-level languages, like C. The details of the 0 s and 1 s are abstracted away.

S TRUCTURE A ND I NTERPRETATION OF C OMPUTER P ROGRAMS How do we structure our programs? What functions do we need? What classes should we design? How do we structure our programs? What functions do we need? What classes should we design? How can we make the computer understand our code? How does the computer currently understand our code? How can we make the computer understand our code? How does the computer currently understand our code?

8 T HE T REACHERY OF I MAGES (R ENÉ M AGRITTE )

9 I NTERPRETATION What is a kitty ?

10 I NTERPRETATION kitty is a word made from k, i, t and y. We interpret (or give meaning to) kitty, which is merely a string of characters, as the animal. Similarly, is simply a collection of characters: how do we get a computer to “understand” that is an expression that evaluates (or produces a value of) 9 ?

11 I NTERPRETATION >>> The main question for this module is: What exactly happens at the prompt for the interpreter? More importantly, what is an interpreter? What happens here?

12 I NTERPRETATION An interpreter for a programming language is a function that, when applied to an expression of the language, performs the actions required to evaluate that expression. It allows a computer to interpret (or to “understand”) strings of characters as expressions, and to work with resulting values.

13 I NTERPRETATION Many interpreters will read the input from the user, evaluate the expression, and print the result. They repeat these three steps until stopped. This is the read-eval-print loop (REPL).

14 I NTERPRETATION READ EVAL PRINT

15 A NNOUNCEMENTS Project 3 is due Thursday, July 26. Homework 11 is due Friday, July 27. Please ask for help if you need to. There is a lot of work in the weeks ahead, so if you are ever confused, consult (in order of preference) your study group and Piazza, your TAs, and Jom. Don’t be clueless!

16 A NNOUNCEMENTS : M IDTERM 2 Midterm 2 is tonight. – Where? 2050 VLSB. – When? 7PM to 9PM. Closed book and closed electronic devices. One 8.5” x 11” ‘cheat sheet’ allowed. Group portion is 15 minutes long.

17 A NNOUNCEMENTS : M IDTERM 2 C AMPANILE

18 T HE C ALCULATOR L ANGUAGE ( OR “C ALC ”) We will implement an interpreter for a very simple calculator language (or “Calc”): calc> add(3, 4) 7 calc> add(3, mul(4, 5)) 23 calc> +(3, *(4, 5), 6) 29 calc> div(3, 0) ZeroDivisionError: division by zero

19 T HE C ALCULATOR L ANGUAGE ( OR “C ALC ”) The interpreter for the calculator language will be written in Python. This is our first example of using one language to write an interpreter for another. This is not uncommon: this is how interpreters for new programming languages are written. Our implementation of the Python interpreter was written in C, but there are other implementations in Java, C++, and even Python itself!

20 S YNTAX AND S EMANTICS OF C ALCULATOR There are two types of expressions: 1.A primitive expression is a number. 2.A call expression is an operator name, followed by a comma-delimited list of operand expressions, in parentheses. What expressions mean How expressions are structured

21 S YNTAX AND S EMANTICS OF C ALCULATOR Only four operators are allowed: 1.add (or + ) 2.sub (or – ) 3.mul (or * ) 4.div (or / ) All of these operators are prefix operators: they are placed before the operands they work on.

22 R EAD -E VAL -P RINT L OOP def read_eval_print_loop(): while True: try: expression_tree = \ calc_parse(input('calc> ')) print(calc_eval(expression_tree)) except...: # Error-handling code not shown READ EVAL PRINT

23 R EAD -E VAL -P RINT L OOP def read_eval_print_loop(): while True: try: expression_tree = \ calc_parse(input('calc> ')) print(calc_eval(expression_tree)) except...: # Error-handling code not shown input prints the string provided and waits for a line of user input. It then returns the line.

24 R EAD The function calc_parse reads a line of input as a string and parses it. Parsing a string involves converting it into something more useful (and easier!) to work with. Parsing has two stages: tokenization (or lexical analysis), followed by syntactic analysis.

25 P ARSING def calc_parse(line): tokens = tokenize(line) expression_tree = analyze(tokens) return expression_tree TOKENIZATION SYNTACTIC ANALYSIS

26 P ARSING : T OKENIZATION Remember that a string is merely a sequence of characters: tokenization separates the characters in a string into tokens. As an analogy, for English, we use spaces and other characters (such as. and, ) to separate tokens (or “words”) in a string.

27 P ARSING : T OKENIZATION Tokenization, or lexical analysis, identifies the symbols and delimiters in a string. A symbol is a sequence of characters with meaning: this can either be a name (or an identifier), a literal value, or a reserved word. A delimiter is a sequence of characters that defines the syntactic structure of an expression.

28 P ARSING : T OKENIZATION >>> tokenize(‘add(2, mul(4, 6))’) [‘add’, ‘(’, ‘2’, ‘,’, ‘mul’, ‘(’, ‘4’, ‘,’, ‘6’, ‘)’, ‘)’] This is the Python interpreter, not the interpreter for Calc. Symbol: Operator name Symbol: Literal Delimiter

29 P ARSING : T OKENIZATION For Calc, we can simply insert spaces and then split at the spaces. def tokenize(line): spaced = line.replace(‘(’, ‘ ( ’) spaced = spaced.replace(‘)’, ‘ ) ’) spaced = spaced.replace(‘,’, ‘, ’) return spaced.strip().split() Removes leading and trailing white spaces. Returns a list of strings separated by white space.

30 P ARSING : S YNTACTIC A NALYSIS Now that we have the tokens, we need to understand the structure of the expression: What is the operator? What are its operands? As an analog, in English, we need to determine the grammatical structure of the sentence before we can understand it.

31 P ARSING : S YNTACTIC A NALYSIS

32 P ARSING : S YNTACTIC A NALYSIS For English (and human languages in general), this can get complicated and tricky quickly: Jom lectures to the student in the class with the cat. The horse raced past the barn fell. Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo. Yes, this is a valid sentence.

33 P ARSING : S YNTACTIC A NALYSIS For Calc, the structure is much easier to determine. Expressions in Calc can nest other expressions: add(3, mul(4, 5)) Calc expressions establish a hierarchy between operators, operands and nested expressions. What is a useful data structure to represent hierarchical data?

34 P ARSING : S YNTACTIC A NALYSIS An expression tree is a hierarchical data structure that represents a Calc expression. add(3, mul(4, 5)) add 3mul 4 5 O PERANDS O PERATOR

35 P ARSING : S YNTACTIC A NALYSIS class Exp: def __init__(self, operator, operands): self.operator = operator self.operands = operands String representing the operator of a Calc expression List of either integers, floats, or other (children) Exp trees

36 P ARSING : S YNTACTIC A NALYSIS add 3mul 4 5 Exp(‘add’, [3, Exp(‘mul’, [4, 5])])

37 P ARSING : S YNTACTIC A NALYSIS The function analyze takes in a list of tokens and constructs the expression tree as an Exp object. It also changes the tokens that represent integers or floats to the corresponding values. >>> analyze(tokenize(‘add(3, mul(4, 5))’)) Exp(‘add’, [3, Exp(‘mul’, [4, 5])]) (We will not go over the code for analyze.)

38 B REAK

39 R EAD -E VAL -P RINT L OOP def read_eval_print_loop(): while True: try: expression_tree = \ calc_parse(input('calc> ')) print(calc_eval(expression_tree)) except...: # Error-handling code not shown READ EVAL PRINT

40 E VALUATION Evaluation finds the value of an expression, using its corresponding expression tree. It discovers the form of an expression and then executes the corresponding evaluation rule.

41 E VALUATION : R ULES Primitive expressions (literals) are evaluated directly. Call expressions are evaluated recursively: – Evaluate each operand expression. – Collect their values as a list of arguments. – Apply the named operator to the argument list.

42 E VALUATION def calc_eval(exp): if type(exp) in (int, float): return exp elif type(exp) == Exp: arguments = \ list(map(calc_eval, exp.operands)) return calc_apply(exp.operator, arguments) 1. If the expression is a number, then return the number itself. Numbers are self-evaluating: they are their own values. 1. If the expression is a number, then return the number itself. Numbers are self-evaluating: they are their own values. 2. Otherwise, evaluate the arguments… 3. … collect the values in a list … 4. … and then apply the operator on the argument values.

43 E VALUATION Why do we need to evaluate the arguments? Some of them may be nested expressions! We need to know what we are operating with (operator) and the values of all that we are operating on (operands), before we can completely evaluate an expression.

44 A PPLY def calc_apply(operator, args): if operator in (‘add’, ‘+’): return sum(args) if operator in (‘sub’, ‘–’): if len(args) == 1: return –args[0] return sum(args[:1] + \ [–arg for arg in args[1:]])... If the operator is ‘ add ’ or ‘ + ’, add the values in the list of arguments. If the operator is ‘ sub ’ or ‘ - ’, either return the negative of the number, if there is only one argument, or the difference of the arguments. … and so on, for all of the operators that we want Calc to understand.

45 A PPLY It may seem odd that we are using Python’s addition operator to perform addition. Remember, however, that we are interpreting an expression in another language. As we interpret the expression, we realize that we need to add numbers together. The only way we know how to add is to use Python’s addition operator.

46 E VAL AND A PPLY Notice that calc_eval calls calc_apply, but before calc_apply can apply the operator, it needs to evaluate the operands. We do this by calling calc_eval on each of the operands. calc_eval calls calc_apply, which itself calls calc_eval. Mutual recursion!

47 E VAL AND A PPLY The eval - apply cycle is essential to the evaluation of an expression, and thus to the interpretation of many computer languages. It is not specific to calc. eval receives the expression (and in some interpreters, the environment) and returns a function and arguments; apply applies the function on its arguments and returns another expression, which can be a value. Functions, Arguments

48 R EAD -E VAL -P RINT L OOP : W E A RE D ONE ! def read_eval_print_loop(): while True: try: expression_tree = \ calc_parse(input('calc> ')) print(calc_eval(expression_tree)) except...: # Error-handling code not shown READ EVAL PRINT With a small amount of code, we have a fully functioning interpreter for a new language!

49 T HREE MORE B UILTIN F UNCTIONS str(obj) returns the string representation of an object obj. It merely calls the __str__ method on the object: str(obj) is equivalent to obj.__str__() eval(str) evaluates the string provided, as if it were an expression typed at the Python interpreter.

50 T HREE MORE B UILTIN F UNCTIONS repr(obj) returns the canonical string representation of the object obj. It merely calls the __repr__ method on the object: repr(obj) is equivalent to obj.__repr__() If the __repr__ method is correctly implemented, then it will return a string, which contains an expression that, when evaluated, will return a new object equal to the current object. In other words, for many classes and objects (including Exp ), eval(repr(obj)) == obj.

51 C ONCLUSION An interpreter is a function that, when applied to an expression, performs the actions required to evaluate that expression. We saw how to implement an interpreter for a simple calculator language, Calc. The read-eval-print loop reads user input, evaluates the expression in the input, and prints the resulting value. Reading also involves parsing user input into an expression tree. Evaluation works on the provided expression tree to obtain the value of the corresponding expression. Preview: An interpreter for Python, written in Python.

52 G OOD L UCK T ONIGHT !