5 The Semantic Level

As indicated by the investigation example in the previous section, it is often useful for tools to be able to add attributes (operations, data) to the patterns of the context-free level. A simple way to add semantic attributes is to let the tool programmer textually edit the patterns of the context-free level. In a programming environment with many grammars and tools this is not satisfactory from a maintenance point of view. If semantic attributes have to be manually inserted into the patterns this has to be done each time changes are made to the grammar. From a structuring point of view it would be an advantage if the definition of semantic attributes could be kept separate from the generated patterns.

The semantic level of the metaprogramming system is therefore defined as part of the grammar. The semantic level allows the specification of the number of semantic attributes of each syntactic category in the grammar. The metaprogramming system will then ensure that the proper memory space is allocated for these attributes, and their values will be maintained by the system and stored along with the AST.

It is important to know, that the persistent parts of the ASTs (i.e. the information stored in the files) is not in the form of BETA objects (instances of the AST patterns). The storage is instead in the form of an encoded bytestream, which also is the runtime representation of the ASTs, and the AST patterns are merely interfaces to this compact representation. This also implies that information in various types of objects as part of the instances of the AST patterns will not be stored when the AST is stored onto the disk. Such information is transient and cannot be shared with other tools. Since the semantic information is to be shared between tools, and stored onto the persistent representation of ASTs, it is specified in the grammar (in the attributes part, see chapter 6).

These persistent attributes of ASTs are accessed through the putAttribute and getAttribute of ASTs. The attributes are integer-valued and indexed, and may be used for any purpose.

To enable the specification of transient properties of AST to be used at the semantic level, the metaprogramming system offers facilities for specifying that SLOTs should be inserted at various places in the generated context-free level interface. First of all, if a nonterminal is mentioned in the attribute part of the grammar, an attribute slot is automatically inserted in the pattern generated for that nonterminal (see chapter 6 and appendix 3). Secondly, the options part of the grammar may specify an identifier in the substanceSlot option in the options part of the grammar, and the result is that a descriptor slot with that name is inserted in the treelevel subpattern for the grammar (see chapter 6 and appendix 3).

These slots are used for specifying the transient properties in separate fragments, such that changes in the grammar (and thereby regeneration of the context-free level interface, does not destroy any semantic specifications. See appendix 3 for an example of this usage of semantic level slots.

The Metaprogramming System - Reference Manual
© 1991-2004 Mjølner Informatics
[Modified: Friday April 6th 2001 at 12:43]