SymbolTable - TypeCobolTeam/TypeCobol GitHub Wiki

What is SymbolTable ?

TypeCobol SymbolTable is the way for TypeCobol parser, to store different elements like Data Symbols (Variables), Section, Paragraphs, Types, Functions / Procedures and Programs.

A SymbolTable always has an enclosing symboltable except for intrinsic scope. There are 5 depths level. It’s starting from intrinsic scope to function scope. Let’s see them:

  • Intrinsic Scope : This scope is specific to TypeCobol and allows to store the different public types given by the CLI option –y. This Intrinsic Scope is the highest SymbolTable level. We could also find the types BOOLEAN and DATE hardly defined in C# Code in CobolNodeBuilder.

  • Namespace Scope : This scope is the first one enclosed into Intrinsic Scope. The namespace scope contains all the program already parsed by the TypeCobol Parser. It’s really usefull to find back some public function or procedure in another program but also a public type which is not referenced into intrinsic scope. The use of Namespace scope to find a function or a type into a program is to use a Namespace represented by a QualifiedName like MYPGM::PublicFunction.

  • Global-Storage Scope : The Global-Storage scope is used to store variables that will be available for any program in a Cobol Module. (Like MainPGM, Nested program, Stacked Program). The Global-Storage scope will store all the variables declared inside a Global-Storage Section. Global-Storage variables will also be available inside Procedure/Function core.

  • Global Scope : Global Scope is enclosed by Global-Storage symbol table. The Global scope contains all the variables declared in a DATA DIVISION as GLOBAL. This will expose all the GLOBAL variables to the entire program in which they are declared and for all nested programs contained in the main program.

  • Declaration Scope : Declaration scope is enclosed by Global Scope symbol table. This scope will store all the available procedure/function and types declared inside the program.

  • Program Scope : Program Scope is enclosed by Declaration Scope. The program scope will contain the current source program being parsed, all the variables declared in DATA DIVISION whithout GLOBAL are going to be visible to the entire program in which they are declared.

  • Function Scope : Function Scope is enclosed by Program Scope. It will contain all the function and procedure of the current parsed program.

Declared in program others procedures nested program procedure in nested stacked program procedure in stacked

Variable

program

working or local without global

Y

Variable

program

global

Y

Y

Variable

program

global-storage

Y

Y

Y

Y

Type

program

(private by default)

Y

Y

Type

program

global

Y

Y

Y

Y

Type

program

private global

Y

Y

Y

Y

Type

program

public

Y

Y

Y with program prefix

Y with program prefix

Y with program prefix

Y with program prefix

Type

program

public global

Y

Y

Y

Y

Y with program prefix

Y with program prefix

Type

procedure

(private by default)

Y

Y

Type

procedure

global (forbiden)

Type

procedure

public

Y

Y

Y with program prefix

Y with program prefix

Y with program prefix

Y with program prefix

Where is it use ?

The SymbolTable is mostly used in CobolNodeBuilder but also in the different parsing checker. The SymbolTable allow to do the SemanticCheck of all the elements detected in the parsed program.

  • CobolNodeBuilder : CobolNodeBuilder is responsible of creating the nodes. After creating a new node, for instance a FunctionDeclaration node, it will store it in the SymbolTable of the current program. The CobolNodeBuilder is in charge of creating and storing the node in the correct SymbolTable scope and node type.

  • Checkers : The SymbolTable is also largely used into TypeCobolChecker and Cobol85Checker. It also enables the checker to retrieve the different variables, functions or types that were parsed. To do so, SymbolTable exposes multiple methods depending on the type of Node we want to retrieve. There are two principal kinds of method, the ones to Get the node from the different levels of SymbolTable and the ones to Add the node into a given SymbolTable scope.

SymbolTable Methods

SymbolTable is composed of different Methods that allows CobolNodeBuilder or the checkers to Add or Get a node. SymbolTable implements a generic Add<T>() and GetFromTableAndEnclosing<T>() methods. Those methods are called by specific methods for each Node’s type. We have divided the different type of Node in C# REGIONS.

REGIONS : DATA SYMBOLS, SECTION, PARAGRAPHS, TYPES, FUNCTIONS, PROGRAMS.

For instance the GetFunction() will call the method GetFromTableAndEnclosing() by passing the function name and the GetFunctionTable() method as a Func<> parameter. This will allow GetFromTableAndEnclosing() to invoke the GetFunctionTable() to seek to the function in all the different levels of Symbol Table. After passing all the levels, so until intrinsic scope, GetFromTableAndEnclosing() will return the founded function(s).

The GetFunction() and GetType() methods are specific in this case because they also use Namespace identifier. Namespace scope provided the ability to TypeCobol to find public procedures and types of a certain program, given by his name.

⚠️ **GitHub.com Fallback** ⚠️