Lecture 21 Interpreters
- Questions
- Programming Languages
- High level → Assembly → Machine
- Machine languages
- statements interpreted by the hardware itself
- High-level languages
- statements and expressions are interpreted by another program or compiled into another language
- Compilers
- translate source code into machine code so that the machine code can be distributed and run repeatedly
- Interpreters
- run source code directly without first compiling it into machine code
- Understanding source code
- parser must be written to understand that source code
- Parsing
- Reading Scheme Lists
- All call expressions in Scheme are represented by a Scheme list
- Scheme list is written as elements in parentheses
- Each element can be a combination or primitive
- Parser takes in text and returns an expression that represents the text in a tree-like structure
- Lexical Analysis
- converts input text into a list of tokens
- each token represents the smallest unit of information
- Syntatic Analysis
- identifies the hierachical structure of an expression
- symbols can be nested
- Pair Abstraction
- A pair is similar to a linked list
- Generating Pairs
- we define a function called scheme_read that will consume the input tokens for exactly one expression
- The Calculator Language
- Just primitive expressions and call expressions
- Call expression is a combination that begins with operator followed by 0 or more expressions
- Expression Trees
- When we want to use control, we follow the same evaluation procedure
- Evaluation
- Computes the value of an expression, which is always a number
- In calculator, an expression is either a number or a Pair
- Language semantics
- A number evaluates to itself
- A call expression evaluates to its arguments values combined by an operator
- Interactive Interpreters
- Read-Eval-Print Loop
- Interactive interpreters
- Read text input from the user
- Parse the text input into an expression
- Evaluate the expression
- If any errors occur, report those errors, otherwise
- Print the value of the expression and repeat
- Interpreting Scheme
- The Structure of an Interpreter
- Eval
- Base case: primitive values
- Recursive calls:
- eval(operator, operands)
- Apply(procedure, arguments)
- Eval(sub-expressions) of special forms
- Apply
- Base cases
- Built-in primitive procedures
- Recursive Calls
- Eval(body) of user-defined procedures
- Mutual recursion among both functions
- Special Forms
- The scheme_eval function choose behavior based on expression form
- Logical special forms
- logical forms may only evaluate some sub-expressions
- Quotation
- Another special form
- The expression is not evaluated
- Define Expressions