Download Lecture 28: Implementing Interpreters - Reasons to Learn Python - Prof. David Evans and more Study notes Computer Science in PDF only on Docsity!
David Evans http://www.cs.virginia.edu/evans
CS150: Computer Science University of Virginia Computer Science
Lecture 28:Lecture 28:
ImplementingImplementing
Interpreters Interpreters
Lecture 28: Implementing Interpreters 2
Why learn
Python?
Lecture 28: Implementing Interpreters 3
Reason 1: Vocational Skill
Python
Java
SQL
Scheme
Job listings at monster.com in Virginia
(27 March 2007, postings in last 3 months):
770 $35-200K
27 $40-200K
55 $100-999K
1138 $60-400K PS5, PS8 & 9
Lecture 28: Implementing Interpreters 4
“Scheme” Jobs
Lecture 28: Implementing Interpreters 5
Reason 2: Expanding Minds
Languages change the way we think.
The more languages you know, the
more different ways you have of
thinking about (and solving) problems.
Lecture 28: Implementing Interpreters 6
“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. Printed by Ottaviano Dei Petrucci in 1501 (first music with movable type)
Lecture 28: Implementing Interpreters 7
J S Bach, “Coffee Cantata”, BWV 211 (1732) www.npj.com/homepage/teritowe/jsbhand.html
“Jamais Jamais Jamais” from Harmonice Musices Odhecaton A. (1501) Lecture 28: Implementing Interpreters 8
Reason 3: Deepening Understanding
By 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).
Lecture 28: Implementing Interpreters 9
Reason 4: Building Confidence
By 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).
Lecture 28: Implementing Interpreters 10
Reason 5: Fun
Programming 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.
Lecture 28: Implementing Interpreters 11
Python
- 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, inheritance
Lecture 28: Implementing Interpreters 12
Learning 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 program
Lecture 28: Implementing Interpreters 19
Implementing
Interpreters
Lecture 28: Implementing Interpreters 20
Inventing 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 language
Lecture 28: Implementing Interpreters 21
Is 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 another program. 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)
Lecture 28: Implementing Interpreters 22
Environmental Model of Evaluation
- To evaluate a combination, evaluate all the subexpressions and apply the value of the first subexpression to the values of the other subexpressions.
- 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.
Lecture 28: Implementing Interpreters 23
Eval Eval
Apply Apply
Eval and Apply are defined in terms of each other.
Lecture 28: Implementing Interpreters 24
def meval(expr, env): if isPrimitive(expr): return evalPrimitive(expr) elif isConditional(expr): return evalConditional(expr, env) elif isLambda(expr): return evalLambda(expr, env) elif isDefinition(expr): evalDefinition(expr, env) elif isName(expr): return evalName(expr, env) elif isApplication(expr): return evalApplication(expr, env) else: evalError ("Unknown expression type: " + str(expr))
Implementing meval
Lecture 28: Implementing Interpreters 25
def mapply(proc, operands): if (isPrimitiveProcedure(proc)): return proc(operands) elif isinstance(proc, Procedure): params = proc.getParams() newenv = Environment(proc.getEnvironment()) if len(params) != len(operands): evalError ("Parameter length mismatch: ...") for i in range(0, len(params)): newenv.addVariable(params[i], operands[i]) return meval(proc.getBody(), newenv) else: evalError("Application of non-procedure: %s" % (proc))
Implementing mapply
Lecture 28: Implementing Interpreters 26
Charge
- Friday: Implementing the rest of the
interpreter: the evaluation rules,
environments, procedures
- Next week: changing the evaluation rules
- Don’t wait any longer to start PS
- The statement, “There will be a surprise
quiz someday this week” might still be
true