Java Reference
In-Depth Information
VariableListDeclaring
idList
typeName
Identifier
IdentifierList
type
. . .
ids
attributesRef
name
Figure 8.12: Abstract Syntax Tree for Variable Declarations
8.6 Variable and Type Declarations
We begin by examining the techniques necessary to handle declarations of
variables and scalar types, and then move on to consideration of structured
types.
8.6.1 Simple Variable Declarations
Our study of declaration processing begins with a simplified version of the
variable declarations found in any programming language. This simple form
of declaration includes a type name and a list of identifiers, indicating that
all of the identifiers are to be declared as variables with the named type. The
abstract syntax tree built to represent a variable declaration with the type
defined by a type name is shown in Figure 8.12. Regardless of the exact
syntax used in a particular programming language, this AST can be used to
represent such declarations. However, it only represents a restricted version of
variable declarations,whichwe are using to beginour discussionof declaration
processing. Amore general version of this visitor can be found in Section 8.6.4.
The visitor actions for simple variable declarations in Figure 8.13 illus-
trate use of a specialized visitor to process parts of the abstract syntax tree.
An Identifier canbeusedinmanycontextsinasyntaxtree. Sincewewant
this particular identifier to be interpreted as a type name, a new instance of
TypeVisitor is created at Marker 20 and invoked at Marker 21 to look up the
type name in the symbol table and verify that it does indeed refer to a type.
The loop at Marker 22 processes the list of variable names, first checking
each for a previous declaration (Marker 23 ). The block of code that begins at
Marker 24 creates an Attributesstructure for an identifier and then enters the
identifier in the symbol table with the appropriate Attributes.
 
 
 
Search WWH ::




Custom Search