Compiler Construction bio photo

Compiler Construction

Twitter Github

Edit on GitHub

Homework Assignments Week 1.5: Name Resolution (Answers)

Name Resolution with Scope Graphs

The main aspects are:

  • The name binding structure of a program is represented as a graph of scopes, references, and declarations.
  • Resolving a reference is finding a path in the graph from the reference to a matching declaration.
  • The paths used for resolution must be well-formed, which means the labels must match a given well-formedness regular expression.
  • Disambiguation defines which declarations are visible if multiple matching declarations can be reached. Disambiguation is specified using an ordering on path labels.

Resolving in Scope Graphs

  1. The resolution of f8 involves an import of A7. This import has two reachable declarations:

    • A7 · R · #2 · P · #0 · D · A2
    • A7 · R · #2 · D · A4

    According to the label order Ord, the path to A4 shadows the path to A2. Therefore, reference A7 resolves to A4.

    Given that, there is one reachable declaration for F8, which is:

    • f8 · R · #4 · I · #3 · D · f6

    Because there is only a single resolution path, the reference is resolved to declaration f6.

  2. The import A5 has one reachable declaration, which is:

    • A5 · R · #0 · D · A1

    Since there is only a single path, A5 resolves to A1.

    The import B7 has one reachable declaration, which is:

    • B7 · R · #3 · P · #0 · D · B4

    Since there is only a single path, B7 resolves to B4.

    Finally, the reference has two resolution paths, which are:

    • f8 · R · #3 · P · #0 · D · f9
    • f8 · R · #3 · I · #2 · I · #1 · D · f3

    According to the label order Ord, the path via the imports takes precedence over the path via the lexical parent. Therefore, f8 resolves to f3.

Constructing Scope Graphs

  1. The corresponding scope graph is given by:

    Scope Graph

  2. The corresponding scope graph is given by:

    Scope Graph