Pattern 17Symbol Table for Nested Scopes

Purpose

This pattern tracks symbols and builds a scope tree for languages with multiple, possibly nested scopes.

Programming language functions are a good example of nested scopes. Each function has its own scope that is nested within a global or class scope. Some languages even support nested function definitions or multiple local scopes. Many DSLs have nested scopes as well. The DOT[22] graphics language, for example, has subgraphs within graphs. This pattern handles them all gracefully.

Discussion

To discuss nested scopes, let’s add functions to our Cymbol C++ subset. That means we’ll need a function (method) Symbol and scopes for globals, parameters, and local variables. Let’s take a look at some sample ...

Get Language Implementation Patterns now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.