Scopes

Top  Previous  Next

Examples > C-typedef > Scopes

 

The third parameter in the call:

 

{{ AddToken(xState.str(), "TYPE", ScopeStr()); }}

 

wasn't explained yet. The area within which the additional token is recognized can be limited with this parameter. It is an optional parameter. If this optional parameter is left out, the definition applies to all following code. If the third parameter is passed, however, the definition is then valid only for time time as an area is defined with the corresponding scope name. Such a scope is defined at the very beginning of the start rule translation_unit and it is removed at the end:

 

{{

PushScope("external"); 

}}

external_declaration*

{{

PopScope(); 

}}

 

Likewise happens in the compound_statement: These scopes are numbered, to be able to generate unique names for them. Scope names are managed with a stack. The command PushScope puts an additional scope on the already available stack of scopes and PopScope removes the topmost scope. A dynamic tokens is recognized as long, as the scope, for which it has been defined is still in the stack.

The type pchar is defined in the following example within the first compound_statement.

 

  if(xi > 0)

  {

    typedef char* pchar;

    pchar p;

  }

  else

  { 

    pchar p;  // error

  }

 

This definition isn't, however, valid in the second compound_statement any more so that its use leads to a fault. This is exactly the mechanism of type definitions in C.

 

 



This page belongs to the TextTransformer Documentation

Home  Content  German