Implementing Scopes - PowerPoint PPT Presentation

About This Presentation
Title:

Implementing Scopes

Description:

Implementing Scopes A symbol table is in essence a dictionary I.e., every name appears in it, with the info known about it Usually expressed as a name and an ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 7
Provided by: cseWustl
Category:

less

Transcript and Presenter's Notes

Title: Implementing Scopes


1
Implementing Scopes
  • A symbol table is in essence a dictionary
  • I.e., every name appears in it, with the info
    known about it
  • Usually expressed as a name and an attribute
    list/map/etc.
  • Symbol table entries are added, modified, looked
    up
  • I.e., as a name is declared, then defined, then
    used, etc.
  • A symbol table can have other operations as well
  • Scope entry and exit operations (may navigate
    within it)
  • Symbol tables often implemented in 1 of 2 main
    ways
  • An association list (of name/value pairs) is
    often simplest
  • A central reference table maintains an explicit
    mapping

2
Symbol Tables for Nested Scopes
  • Scope analysis allows declarations and bindings
    to be processed in a stack-like manner at
    run-time
  • A symbol table is used to keep track of that
    information
  • E.g., each identifier in a scope has a set of
    bindings
  • Structure/management may range from a single
    static symbol table to a dynamic hierarchy of
    per-scope tables

pi
double
3.141
main
(int, char)
argc
int
2
argv
char

function scope
global scope
3
Names, Aliases, and Overloading
  • Multiple names may refer to same object or
    variable
  • E.g., pointers or references (and
    pass-by-reference) in C
  • The ability to re-use a name is also commonplace
  • E.g., using the operator for addition or string
    concatenation
  • Since a symbol table usually stores parameter
    type attributes for a function identifier, can
    use to resolve
  • I.e., look up the declaration/definition of a
    function or operator based on its parameter types
    as well as its name
  • Many languages allow function name overloading
    and a smaller number of them allow operator
    overloading
  • Syntactically introduced constraints such as
    precedence and associativity of operators must be
    respected (unless waived)
  • Though with tweaks like C prefix operator call
    syntax

4
Coercion and Polymorphism
  • Types often are allowed to be converted to other
    types
  • E.g., while (ifs gtgt token) // ifstream to bool
  • When the complier forces this to happen its
    coercion
  • I.e., the type conversion happens implicitly
  • Polymorphism lets multiple unconverted types be
    used
  • Inheritance polymorphism (E.g., C classes)
  • Interface polymorphism (E.g., C templates)
  • Both support subtype polymorphism (Liskov
    substitution)
  • Explicit parametric polymorphism is called
    generic
  • E.g., C templates with specialization

5
Binding of Reference Environments
  • Referencing environment depends on the order in
    which declarations are encountered
  • When is the context in which a function is
    executed fixed?
  • This matters because of nesting of scopes, hiding
    of names, and the ability to pass a function
    (pointer) as a parameter
  • Shallow binding is done just before function is
    called
  • Deep binding is done when the parameter is first
    passed
  • Function/subroutine closures
  • Bundle referencing environment with reference to
    subroutine
  • E.g., local variable capture list in C11 lambda
    expression
  • Object closures
  • Similar idea, bundle member variables and
    operator()

6
Todays Studio Exercises
  • Well code up ideas from Scott Chapter 3.4-3.8
  • Again looking at the semantics of different C
    constructs
  • Extending the symbol table from the last studio
  • Todays exercises are again in C
  • Please take advantage of the on-line reference
    manual pages that are linked on the course web
    site
  • As always, please ask us for help as needed
  • When done, email your answers to the course
    e-mail account with Semantics Studio II in the
    subject
Write a Comment
User Comments (0)
About PowerShow.com