Bindings & The Symbol Table
Data Types
Type Equivalence & Type Checking
XXXtras
XXXtras
100

What is binding and what are the different types of it?

The process of associating an attribute with a name

Static binding: occurs before execution

Dynamic binding: occurs during execution

100

What is a type constructor?

- mechanisms used with a group of basic types to construct more complex types (combine basic types to create complex data structures like arrays or records)


100

Define type equivalence and the difference between structural equivalence and name equivalence

Type equivalence: rules for determining if 2 types are the same

  • Structural equivalence: 2 data types are the same if they have the same structure

  • Name equivalence: 2 data types are the same if they have the same name

100

What is the difference between static and dynamic scoping?

Static scoping: all declarations processed prior to execution

  • AKA lexical scoping

  • Symbol table is managed by a compiler

  • Bindings of declarations are all static


Dynamic scoping: declarations are processed as they are encountered along an execution path

  • an interpreter must initialize & maintain a symbol table at run time

100

What is binding time?

The moment when an attribute is assigned or bound to a name

200

What is the purpose of a symbol table?

It stores bindings between names and their attributes, managing variable and function references

200

What is a simple type in programming?

What is the difference between simple types and complex types?

Simple types have no other structure than their inherent arithmetic or sequential structure... complex types are formed by combining simple types or other complex types. allows for more elaborate data structures

200

What is the difference between a statically typed language and a dynamically typed language

Statically typed language: types are checked at compile time 

Dynamically typed language: types are determined at runtime


200

What is a lexical address and how is it used in static scoping?

Lexical address: consists of a level number and an offset

This addressing system allows for static scoping, because the compiler can use its lexical address to locate it.

200

What are the basic operations of a symbol table?

Insert a new binding, look up attributes bound to a name, and delete a binding.

300

what is a scope hole?

A gap in a variable’s visibility within a certain scope, often caused by a local variable shadowing a global one (local variable has the same name as a global variable)

300

Discuss the different simple types discussed in lecture plz :p

B - O - E - S


1. Built-in Types

  • Definition: Types that come with the language (no need to define them yourself).
  • Example: In Java, char is built-in (since it's directly available in the language), but String is not technically built-in, as it's a reference type provided through a class.
  • Memory Trick: Think of "built-in" as "pre-installed" — you don’t have to create or import it.

2. Ordinal Types

  • Definition: Types with a countable and ordered range.
  • Examples: Integers, booleans, characters.
  • Feature: They usually have a successor (next) and predecessor (previous) operation.
  • Memory Trick: "Ordinal" sounds like "order" — these types have an order you can count through.

3. Enumerated Types (Enums)

  • Definition: A set of named values that are ordered.
  • Examples: Days of the week (Monday, Tuesday, etc.), traffic light colors (Red, Yellow, Green).
  • Feature: Enums are usually ordered, so you can move to the next or previous value (successor and predecessor).
  • Memory Trick: "Enumerated" means "listed" — think of enums as a fixed list of names.

4. Subrange Types

  • Definition: A contiguous portion of a simple type’s values, defined by a minimum and maximum.
  • Examples: A subrange of integers from 1 to 10, or characters from 'A' to 'Z'.
  • Memory Trick: "Subrange" sounds like "subset" — it’s just a smaller part of a larger type.



300

Does casting have a purpose in a dynamically typed language?

No, casting does not have a purpose in a dynamically typed language. In dynamically typed languages, types are determined at runtime, so there’s no need to explicitly cast or convert types ahead of time. The language automatically adjusts and interprets the types as needed during execution.

300

Be able to determine a programs output using either static or dynamic scoping

Static Scoping: Looks at where variables are defined in the code (lexical structure).

- looks for the closest x in the code.

Dynamic Scoping: Looks at the order in which functions are called at runtime (call stack).

- checks the most recent x used when calling function

300

Define a discriminated union and why it is useful.

A discriminated union includes a tag that indicates the current type stored, making it safer by preventing type mismatches.

400

What is overload resolution? How does it work?

process of choosing a unique function among many with the same name

expands lookup operation in the symbol table, to perform lookups based not only on the name of the function, but also the number of parameters and their data types

400

What are subrange types, and what benefits do they provide?

Subrange types are contiguous subsets of a type, enhancing documentation, allowing semantic checks, and potentially using less memory

400

What is the difference between casting and conversion?

Casting: reinterpreting the memory as a different type (without changing the underlying bit representation)

Conversion: changing the data type (converting from one type to another)

400

What is name equivalence in type systems?

Name equivalence considers two types the same only if they share the exact name, unlike structural equivalence, which compares internal structure.

400

How does a symbol table assist with static scoping?

The symbol table stores variable bindings, allowing the compiler to reference variables based on their lexical position in the code.

500

In what way does dynamic scoping affect the output of a program compared to static scoping? Give output with static vs dynamic

#include <stdio.h>


int x = 1;          // Global variable x

char y = 'a';       // Global variable y


void p() {

    double x = 2.5; // Local x in function p, shadows global x

    printf("%c\n", y); // Prints y

    {

        int y[10];   // Local y array, shadows global y within this block

    }

}


void q() {

    int y = 42;       // Local y in function q, shadows global y

    printf("%d\n", x); // Prints x

    p();

}


int main() {

    char x = 'b';     // Local x in main, shadows global x within main

    q();

    return 0;

}


Dynamic scoping allows nonlocal variables to be bound at runtime based on the execution path, potentially altering the program output depending on this path

Output with static:

1

a

Output with dynamic:

98

*



500

What are the different type constructors discussed in lecture 

C - U - A - F - PR

  • Cartesian Product 

    • CROSS PRODUCT - Combines two or more types to create a structured type with multiple fields.
  • Union

    • A type that can store different types of values in the same memory space, but only one type at a time.
    • Discriminated Union: A union with a tag that identifies the current type of the stored value, making it safer.
    • Undiscriminated Union: A union without a tag, so the type is assumed, making it less safe and potentially error-prone.
  • Array

    • "a mapping from an index type to a component type" meaning that each label (index) points to a specific item (component) in the array.
  • Function Type Constructor

    • Maps input types to output types
  • Pointer and Recursive Types

    • constructs the set of all addresses that refer to a specified type
500

Define type conversion and the different types 

IE - WN

Converting from 1 type to another (can be built into the system to happen automatically)

Implicit conversion (coercion): inserted by the translator 

Explicit conversion (cast): conversion directives are written into code

Widening conversion: target data type can hold all of the converted data without loss of value

Narrowing conversion: conversion may involve a loss of data

500

How does structural equivalence determine type equivalence for two recursive types, and what challenge does recursion introduce?

Structural equivalence requires that two recursive types have identical structures and type constructors for equivalence. Recursion complicates this by requiring a deep comparison through recursive references, which can be difficult to implement without infinite looping or complex checks.

500

Explain the difference between a function set and a Cartesian product as type constructors and provide an example of each.

A function set represents all possible mappings from one type to another (e.g., int -> bool), while a Cartesian product is an ordered combination of types (e.g., struct {int, float}). A function set is like all possible functions from int to bool, while a Cartesian product would be like a struct with int and float fields.