next up previous contents
Next: Type-Checker Up: Compilation Strategies for HPJava Previous: Multiarray Types and HPspmd   Contents

Abstract Syntax Tree

The HPJava grammar was originally extended from a grammar freely supported by WebGain's Java Compiler Compiler (JavaCC) [25]. JavaCC is a parser generator and a tree builder with an add-on application, JJTree. Since HPJava is a superset of Java, the new constructs, multiarrays, array sections, etc, are added into the original Java grammar. With this new grammar, JavaCC generates the HPJava parser generator and the HPJava Abstract Syntax Tree (AST). In order to traverse the HPJava AST, the compiler adopts the visitor design pattern [20]. It would be confusing to have type-checking code mixed with code-generator code or flow analysis code. Moreover, adding a new operation usually requires recompiling all of these classes. It would be better if each new operation could be added separately, and the syntax node classes were independent of the operation that applies to them. We can have both by packing related operations from each class in a separate object, called Visitor, and passing it to elements of AST as it is traversed. When an element ``accepts'' the Visitor, it sends a request to the Visitor that encodes the element's class. We see how to traverse AST for an expression, $ a + b$ with some examples of Figure 4.2. AST has a visitor class, ASTVisitor, another visitor class, HPJavaTypeChecker, the binary expression syntax node, BinExprNode, and the identifier syntax node, IdentifierNode.
Figure 4.2: Example of HPJava AST and its nodes.

Figure 4.3: Hierarchy for statements.
Figure 4.4: Hierarchy for expressions.
Figure 4.5: Hierarchy for types.
ASTVisitor is the super class of actual traversal classes such as HPJavaTypeChecker, Translator, etc, that we will see in the following sections. When the HPJavaTypeChecker meets a binary expression, $ a + b$, the accept method for the left-handed node, $ a$, is called. This method calls the visit method for IdentifierNode in HPJavaTypeChecker. After actual operations for type-checking are done, the accept method for the right-handed node, $ b$, is called. Using the visitor pattern, the actual syntax tree, ASTVisitor, doesn't have to be modified and recompiled whenever some operations are added on the actual traversal classes such as HPJavaTypeChecker. This means that all visitors are well isolated from each other. Moreover, the visitor pattern naturally makes the HPJava compiler system traverse in top-down way. The HPJava AST consists of 128 syntax nodes. Figure 4.3, Figure 4.4, and Figure 4.5 partly cover the hierarchy of the HPJava AST nodes.
next up previous contents
Next: Type-Checker Up: Compilation Strategies for HPJava Previous: Multiarray Types and HPspmd   Contents
Bryan Carpenter 2004-06-09