1 Lecture 2 Unit Conversion Calculator Expressions, values, types. Their representation and interpretation. Ras Bodik Shaon Barman Thibaud Hottelier Hack Your Language! CS164: Introduction to Programming Languages and Compilers, Spring 2012 UC BerkeleyAdministrativia HW1 was assigned Tuesday. Due on Sunday 11:59pm. - a programming HW, done individually - you will implement a web mashup with GreaseMonkey Did you pick up your account forms? - you can pick them after the lecture from Shaon Today is back-to-basics Thursday. No laptops. 2Exams Midterm 1: March 6 Midterm 2: April 26 (during last lecture) The final exam is Posters and Demos: May 11 3Course grading Projects (PA1-9) 45% Homeworks (HW1-3) 15% Midterms 20% Final project 15% Class participation 5% Class participation: many alternatives – ask and answer questions during lectures or recitations, – discuss topics on the newsgroup, – post comments to lectures notes (to be added soon) 4Summary of last lecture What’s a programming abstraction? – data types – operations on them and – constructs for composing abstractions into bigger ones Example of small languages that you may design – all built on abstractions tailored to a domain What’s a language? - a set of abstractions composable into bigger ones Why small languages? - see next slide and lecture notes 5MapReduce story 6What’s a “true” language Composable abstractions not composable: – networking socket: an abstraction but can’t build a “bigger” socket from a an existing socket composable: – regexes: foo|bar* composes regexes foo and bar* 7Today Programs (expressions), values and types their representation in the interpreter their evaluation Finding errors in incorrect programs where do we catch the error? Using unit calculator as our running study it’s an interpreter of expressions with fun extensions In Lec3, we’ll make the calc language user-extensible users can without extending the interpreter 8Recall Lecture 1 Your boss asks: “Could our search box answers some semantic questions?” You build a calculator: Then you remember cs164 and easily add unit conversion. How long a brain could function on 6 beers --- if alcohol energy was not converted to fat. 9Programs from our calculator language Example: 34 knots in mph # speed of S.F. ferry boat --> 39.126 mph Example: # volume * (energy / volume) / power = time half a dozen pints * (110 Calories per 12 fl oz) / 25 W in days --> 1.704 days 10Constructs of the Calculator Language 11What do we want from the language • evaluate arithmetic expressions • … including those with physical units • check if operations are legal (area + volume is not) • convert units 12What additional features may we want what features we may want to add? – think usage scenarios beyond those we saw – talk to your neighbor – we’ll add some of these in the next lecture can we view these as user extending the language? 13Additional features we will implement in Lec3 • allow users to extend the language with their units • … with new measures (eg Ampere) • bind names to values • bind names to expressions (lazy evaluation) 14We’ll grow the language a feature at a time 1. Arithmetic expressions 2. Physical units for (SI only) 3. Non-SI units 4. Explicit unit conversion 15Sublanguage of arithmetic expressions A programming language is defined as Syntax: set of valid program strings Semantics: how the program evaluates 16Syntax The set of syntactically valid programs is large. So we define it recursively: E ::= n | E op E | ( E ) op ::= + | - | * | / | ^ E is set of all expressions expressible in the language. n is a number (integer or a float constant) Examples: 1, 2, 3, …, 1+1, 1+2, 1+3, …, (1+3)*2, … 17Semantics (Meaning) Syntax defines what our programs look like: 1, 0.01, 0.12131, 2, 3, 1+2, 1+3, (1+3)*2, … But what do they mean? Let’s try to define e1 + e2 Given the values e1 and e2, the value of e1 + e2 is the sum of the two values. We need to state more. What is the range of ints? Is it 0..232-1 ? Our calculator borrows Python’s unlimited-range integers How about if e1 or e2 is a float? Then the result is a float. There are more subtleties, as we painfully learn soon. 18How to represent a program? concrete syntax abstract syntax (input program) (internal program representation) 1+2 (‘+’, 1, 2) (3+4)*2 (‘*’, (‘+’, 3, 4), 2) 19The interpreter Recursive descent over the abstract syntax tree ast = ('*', ('+', 3, 4), 5) print(eval(ast)) def eval(e): if type(e) == type(1): return e if type(e) == type(1.1): return e if type(e) == type(()): if e[0] == '+': return eval(e[1]) + eval(e[2]) if e[0] == '-': return eval(e[1]) - eval(e[2]) if e[0] == '*': return eval(e[1]) * eval(e[2]) if e[0] == '/': return eval(e[1]) / eval(e[2]) if e[0] == '^': return eval(e[1]) ** eval(e[2]) 20How we’ll grow the language 1. Arithmetic expressions 2. Physical units for (SI only) 3. Non-SI units 4. Explicit unit conversion 21Add values that are physical units (SI only) Example: (2 m) ^ 2 --> 4 m^2 Concrete syntax: E ::= n | U | E op E | (E) U ::= m | s | kg op ::= + | - | * | | / | ^ Abstract syntax: represent SI units as string constants 3 m^2 ('*', 3, ('^', 'm', 2)) 22A question: catching illegal programs Our language now allows us to write illegal programs. Examples: 1 + m, 2ft – 3kg. Question: Where should we catch such errors? a) in the parser (as we create the AST) b) during the evaluation of the AST c) parser and evaluator will cooperate to catch this bug d) these bugs cannot generally (ie, all) be caught Answer: b: parser has only a local (ie, node and its children) view of the AST, hence cannot tell if ((m))+(kg) is legal or not. 23Representing values of units How to represent the value of ('^', 'm', 2) ? A pair (numeric value, Unit) Unit a map from an SI unit to its exponent: ('^', 'm', 2) → (1, {'m':2}) ('*', 3, ('^', 'm', 2)) → (3, {'m':2}) 24The interpreter def eval(e): if type(e) == type(1): return (e,{}) if type(e) == type('m'): return (1,{e:1}) if type(e) == type(()): if e[0] == '+': return add(eval(e[1]), eval(e[2])) … def sub((n1,u1), (n2,u2)): if u1 != u2: raise Exception(“Subtracting incompatible
View Full Document