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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.