Computation - different kinds of information that is acquired, processed, stored, represented and communicated Programming: - computational problem solving - program creation -> application creation Translator translate computer program into machine instructions -> computer executes to perform computation 2 types: 1. compiler translates entire program into machine instructions by compiling 2. interpreter translates one line of program into machine instructions -> executes line by line Expression Interpretation - process for single expression: 1. Lexical analysis divides into tokens; lexical error is syntax error 2. Syntax analysis assembles tokens into expression 3. Semantic analysis evaluate to compute result object -> type error Note: None object shell prompt => ready to interpret another expression Objects in Python - created to represent data - has identity, type, and value Identity determined when object is created; immutable and unique (address in memory) Type determines operations that can be performed + format and layout in memory (immutable) -> list type objects are immutable Value determines memory contents for each particular object of type; mutable (when operations can be applied to change the value) or immutable depending on type Tokens Analogy - Definition: smallest meaningful unit of information in sequence of data - delimiters, literals, identifiers, operators, and keywords Delimiters punctuation Literal noun identifier pronoun operator verb keyword all of the above Examples Literal -> floats, strings, ints Identifier: letter or underscore -> zero or more letters, underscores, or digits -> keywords != identifiers ex. None cant be a identifier
Literal Floats, Operators and Delimiters
CMPUT 174 Oct. 17, 2016 - interpreters and compilers use the same tokens Literal float token non-negative rational number (contains dot/decimal) Operator token used to compute object from one or more operand object (refer to lexical table) Note: lexical analyser always matches the longest character sequence from left to right Delimiter token used to separate other tokens Literal Ints, Literal Strings, Identifiers and Keywords Keyword token sequence of letters (some keywords used as delimiters i.e. while, others used as operators i.e. not), case sensitive Identifier token used as a name to refer to an object in memory (starts with letter or underscore -> zero or more letters, underscores, or digits (all keywords as valid identifiers, therefore excluded as identifiers) Whitespace? -> terminates token (ignores) unless start of line or middle of string Literal int token non-negative whole number (one or more digits) Literal string token sequence of characters (ex. Hello) Indent, Dedent, and Newline Indentation token lexical analyser uses whitespace at the start of the line to compute indent Dedent token unindented from prev. line or ignores whitespace if the level does not change Newline token lexical analyser encounters end of line character (ex. enter) Syntax Analogy - structure of statements in a computer language - sentence in nat. language similar to computer language - phrase similar to expression Examples - syntax rules prescribe the way tokens can be combined to create valid expressions and statements
[binary operator] = {any operator token except for ~} | { The individual keywords: or, and, in, is, } | { The keyword pairs: not in, is not} Atomic Expressions expression single statement atomic expression simplest form of expression Semantics References CMPUT 174 Oct. 17, 2016 - to access object in memory, need to reference object -> literals, identifiers, and some keywords are used to reference - unique literal token is bound to object in memory with diff. identity - literal is bound to object -> sticky nametags -> cannot be unbound - identifier can be bound or unbound -> detachable nametag - keywords: True (bool), False (bool), and None (NoneType) are prebound - Analogy: reference => name (refers to, but not actually object) Atomic Expressions - semantics for keyword: dereference to get result object - semantics for literal token: if literal is bound, dereference to get result object, otherwise create result object and bind literal to it - semantics for identifier token: if identifier is bound, dereference to get result object, otherwise generate error Unary and Binary Expressions - semantics for unary expression: evaluate expression to get object -> apply unary operator to get result - semantics for unary minus operator: int and float are result object, however string reports type error - semantics for binary expression: evaluate left and right to get object -> apply binary operator to get result object Operator Precedence Operator Precedence table lower precedence means higher priority