UVA CS 150 - Lecture 28: Implementing Interpreters

Unformatted text preview:

Slide 1Why learn Python?Reason 1: Vocational Skill“Scheme” JobsReason 2: Expanding Minds“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. Printed by Ottaviano Dei Petrucci in 1501 (first music with movable type)Slide 7Reason 3: Deepening UnderstandingReason 4: Building ConfidenceReason 5: FunPythonLearning New LanguagesPython IfComputability in Theory and Practice (Intellectual Computability Discussion on TV Video)Ali G ProblemSlide 16Slide 17Ali G was Right!Implementing InterpretersInventing a LanguageIs this an exaggeration?Environmental Model of EvaluationSlide 23Slide 24Slide 25ChargeDavid Evanshttp://www.cs.virginia.edu/evansCS150: Computer ScienceUniversity of VirginiaComputer ScienceLecture 28: Lecture 28: Implementing Implementing InterpretersInterpreters2Lecture 28: Implementing InterpretersWhy learn Python?3Lecture 28: Implementing InterpretersReason 1: Vocational SkillPythonJavaSQLSchemeJob listings at monster.com in Virginia (27 March 2007, postings in last 3 months): 770 $35-200K 27 $40-200K 55 $100-999K1138 $60-400KPS5, PS8 & 94Lecture 28: Implementing Interpreters“Scheme” Jobs5Lecture 28: Implementing InterpretersReason 2: Expanding MindsLanguages change the way we think. The more languages you know, the more different ways you have of thinking about (and solving) problems.6Lecture 28: Implementing Interpreters“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. Printed by Ottaviano Dei Petrucci in 1501 (first music with movable type)7Lecture 28: Implementing InterpretersJ S Bach, “Coffee Cantata”, BWV 211 (1732) www.npj.com/homepage/teritowe/jsbhand.html “Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. (1501)8Lecture 28: Implementing InterpretersReason 3: Deepening UnderstandingBy seeing how the same concepts we encountered in Scheme are implemented by a different language, you will understand those concepts better (especially classes/objects, assignment, data abstraction).9Lecture 28: Implementing InterpretersReason 4: Building ConfidenceBy learning Python (mostly) on your own, the next time you encounter a problem that is best solved using a language you don’t know, you will be confident you can learn it (rather than trying to use the wrong tool to solve the problem).10Lecture 28: Implementing InterpretersReason 5: FunProgramming in Python is fun. Especially because:• It is an elegant and simple language• Most programs mean what you think they mean• It is dynamic and interactive• It can be used to build web applications (PS8, PS9)• It is named after Monty Python’s Flying Circus• It was designed by someone named Guido.11Lecture 28: Implementing InterpretersPython•A universal programming language–Everything you can compute in Scheme you can compute in Python, and vice versa•Imperative Language–Designed to support a programming where most of the work is done using assignment statements: x = e•Object-Oriented Language–Every data thing is an object–Built in support for classes, inheritance12Lecture 28: Implementing InterpretersLearning New Languages•Syntax: Where the {, ;, $, etc. all go–If you can understand a BNF grammar, this is easy•Semantics: What does it mean–Learning the evaluation rules–Harder, but most programming languages have very similar evaluation rules•Style–What are the idioms and customs of experienced programmers in that language?•Takes many years to learn•Need it to be a “professional” Python programmer, but not to make a useful program13Lecture 28: Implementing InterpretersPython IfInstruction ::= if (Expression) : BlockEvaluate Expression. If it evaluates to true, evaluate the Block.It is similar to (if Expression (begin Statements))Differences: Indenting and new lines matter! Changing the indentation changes meaning of code What “true” means: Scheme: anything that is not #f. Python: anything that is not False, None, 0, and empty string or container14Lecture 28: Implementing InterpretersComputability in Theory and Practice(Intellectual Computability Discussion on TV Video)15Lecture 28: Implementing InterpretersAli G Problem•Input: a list of 2 numbers with up to d digits each•Output: the product of the 2 numbersIs it computable?Yes – a straightforward algorithmsolves it. Using elementary multiplication techniques it is O(d2)Can real computers solve it?18Lecture 28: Implementing InterpretersAli G was Right!•Theory assumes ideal computers:–Unlimited, perfect memory–Unlimited (finite) time•Real computers have:–Limited memory, time, power outages, flaky programming languages, etc.–There are many computable problems we cannot solve with real computer: the actual inputs do matter (in practice, but not in theory!)19Lecture 28: Implementing InterpretersImplementingInterpreters20Lecture 28: Implementing InterpretersInventing a Language•Design the grammar–What strings are in the language?–Use BNF to describe all the strings in the language•Make up the evaluation rules–Describe what everything the grammar can produce means•Build an evaluator–A procedure that evaluates expressions in the language21Lecture 28: Implementing InterpretersIs this an exaggeration?It is no exaggeration to regard this as the most fundamental idea in programming:The evaluator, which determines the meaning of expressions in the programming language, is just anotherprogram.To appreciate this point is to change our images of ourselves as programmers. We come to see ourselves as designers of languages, rather than only users of languages designed by others.(SICP, p. 360)22Lecture 28: Implementing InterpretersEnvironmental Model of Evaluation1. To evaluate a combination, evaluate all the subexpressions and apply the value of the first subexpression to the values of the other subexpressions.2. To apply a compound procedure to a set of arguments, evaluate the body of the procedure in a new environment. To construct this environment, make a new frame with an environment pointer that is the environment of the procedure that contains places with the formal parameters bound to the arguments.23Lecture 28: Implementing InterpretersEvalEvalApplyApplyEval and Apply are defined in terms of each other.24Lecture 28: Implementing Interpretersdef meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isConditional(expr): return evalConditional(expr, env) elif isLambda(expr):


View Full Document

UVA CS 150 - Lecture 28: Implementing Interpreters

Documents in this Course
Objects

Objects

6 pages

Load more
Download Lecture 28: Implementing Interpreters
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 28: Implementing Interpreters and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 28: Implementing Interpreters 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?