Java Reference
In-Depth Information
/
Visitor code for Marker 13 on page 302
/
procedure
visit
(TypeDeclaring td )
typeVisitor
new TypeVisitor()
28
call td . typeSpec . accept( typeVisitor )
29
name td . typeName . name
30
if currentSymbolTable . declaredLocally( name )
then
call error
31
(” This identi f ier is already declared :”
, name )
td . typeName . type errorType
td . typeName . attributesRe f
null
else
attr
new Attributes( typeAttributes )
attr . thisType td . typeSpec . type
call currentSymbolTable . enterSymbol( name , attr )
td . typeName . type td . typeSpec . type
td . typeName . attributesRe f attr
32
end
Figure 8.16:
visit
method in TopDeclVisitor for TypeDeclaring.
it does not matter if a type definition is used rather than a type name in a
variable declaration. In the previous section, we assumed that the type for a
variable declaration was given by a type name. However, since we processed
that name using TypeVisitor, it is actually irrelevant to the variable declaration
method whether the type is described by a type name or by a type definition.
In either case, processing the type subtree with TypeVisitor will result in a
reference to a TypeDescriptor being produced. The only di
erence is that in
one case (a type name), the reference is obtained from the symbol table, while
in the other (a type definition), the reference is to a descriptor created by the
TypeVisitor.
ff
Handling Type Declaration and Type Reference Errors
In the pseudocode for the TopDeclVisitor
method for an identifier, we
introduced the idea of a static semantic check . That is, we define a situation
in which an error can be recognized in a program that is syntactically correct.
The error in that case was a simple one: a name was used as a type name, but
it did not actually name a type. In such a situation, we want our compiler to
generate an errormessage that explains why the program is erroneous. Ideally,
we want to generate only one error message per error (though compilers too
often fall far short of this ideal!). The simplest way to accomplish this goal is
to immediately stop the compilation, though it is also at least approachable for
a compiler that tries to detect as many errors as possible in a source program.
Whenever semantic processing finds that a semantic error has occurred,
visit
 
Search WWH ::




Custom Search