Interpreters

an interpreter does 3 things:

  1. Reads input from the user in a specific programming language, translates the input to be computer readable and

  2. Evaluates the result

  3. Prints the result for the user

REPL - (Read -> Evaluate -> Print) Loop

2 languages involved:

  • Implemented language:

    • the 1 the user types in

  • Implementation language:

    • the 1 the interpreter is implemented in

Reading Input

Lexical analysis (LEXER)

  • turning the input into a collection of tokens

  • a token:

    • single input of the input string:

      • literals, names, keywords, delimiters

Syntactic analysis (PARSER)

  • turning tokens into a representation of the expression in the implementing language

Evaluating expressions

  • eval takes in 1 argument besides the expression itself - the current environment - consisting of the current frame and all its ancestors, until the Global frame

  • rules depend on the type

    • self-evaluating - booleans and numbers - use Python booleans and numbers

    • symbols - use Python strings

    • call expressions

Evaluating combinations

(<operator> <operand 1> <operand 2>)

  • if the operator is a symbol and is found in the dictionary of special forms, the combination is a special form

  • each special form has special rules for evaluation

  • otherwise the combination is a call expression

  1. Evaluate the operator to get a procedure

  2. Evaluate all of the operands from left to right

  3. Apply the procedure to the values of the operands

Types of procedures

  1. BUILT-IN: eg.: +, modulo, list, ... - predefined in the Scheme interpreter

  2. User defined: defined with a lambda or a define expression

    1. a list of formal parameters

    2. a body (which is a Scheme list)

    3. a parent frame

The evaluator

  • consists of 2 mutually recursive components

Last updated