You are on page 1of 14

Tutorial 3

Semantic Analysis Part 1

Question 1
List the differences between the parse tree and the
abstract syntax tree (AST).
The role and purpose of the AST and parse trees
are different:
A parse tree represents a derivation a conceptual
tool for analysing the parsing process.
An AST is a real data structure and stores information
for semantic analysis and code generation.

The tree structure:


A parse tree contains a node for every token in the
program.
Semantically useless symbols and punctuation tokens
are omitted in AST.

Question 2
Understand the abstract syntax of PL/3007
defined in Appendix A and draw the trees for
the following node types: Module, Import,
FunctionDeclaration, FieldDeclaration,
TypeDeclaration, LocalVarDecl, Parameter,
JavaTypeName, VarDeclStmt, Call, NegExpr,
Program.

Question 2
Module
Package
Name

Import

Import
Package

declarations

imports

Name

Declaration

Question 2
FunctionDeclaration
ReturnType
Accessibility

Name

TypeName

Parameter

FieldDecalaration

Accessibility

parameters

VarDecl

body

Block

Question 2
TypeDeclaration
Name

JavaType

Accessibility

LocalVarDecl

Parameter

Name
TypeName

Name
TypeName

Question 2
VarDeclStmt

JavaTypeName

Name

VarDecl
Name
TypeName

Call
Callee
FunctionName
Name

Argument
Expr

NegExpr

Operand
Expr

Program
Module
Package
Name

Module
imports

declarations

Import
Package

FieldDeclaration

TypeDeclaration

Name

Name JavaType
Accessibility

VarDecl

Accessibility

Name
FunctionDeclaration

TypeName

body
ReturnType
Accessibility

TypeName

Name

parameters
Parameter

Block

Name
TypeName

VarDeclStmt

ExprStmt

Question 3
Study the abstract syntax of PL/3007 in
Appendix A and the typeCheck aspect in
Appendix B. Explain why abstract AST nodes
(classes) Expr, BinaryExpr, CompExpr and
ArithCompExpr are defined.

Question 3
Abstract classes do not model real AST nodes.
The real AST nodes that may appear in the AST of a
PL/3007 program with respect to expressions are
1. AddExpr, SubExpr, MulExpr, DivExpr, ModExpr;
2. EqExpr, NeqExpr, LtExpr, LeqExpr, GtExpr,
GeqExpr;
3. VarName, ArrayIndex;
4. Assignment, Call;
5.

Question 3
Abstract classes are useful to structure inheritance
hierarchies and to encapsulate common structure
of their child types.
The sole purpose of an abstract class is to provide
an appropriate superclass from which other classes
may inherit interface and/or implementation
(structure).
A class hierarchy does not need to contain any
abstract classes, but many good object-oriented
systems have class hierarchies headed by abstract
classes. In some cases, abstract classes constitue
the top few levels of the hierarchy.

Question 3
Class Expr is the top level class in the inheritance
hierarchy and the common interface for all kinds of
expressions. For example, the abstract method
Expr.TypeCheck() is part of this common interface.
Class BinaryExpr is a subclass of Expr because it is a
subcategory of expressions. It defines the common
structure and the common interface for all binary
expressions involving binary operators. The
abstract method BinaryExpr.TypeCheck() is part of
this common interface and overrides
Expr.TypeCheck().

Question 3
CompExpr is a subclass of BinaryExpr because it
represents binary expressions that involve comparison
operators. This abstract class defines the common
interface for all binary expressions involving binary
operators. For example, type checking such an
expression will need to check whether both operands are
of the same type and this type is not void, in addition to
the type checking of the operand expressions. This is
different from the type checking of binary expressions
where both operands should be numeric.
CompExpr.TypeCheck() overrides BinaryExpr.TypeCheck().

Question 3
ArithCompExpr is a subclass of CompExpr but a
super class for concrete classes LtExpr, LeqExpr,
GtExpr, GeqExpr, because these concrete classes
need to have a common interface, for example,
comparison is between numeric typed
expressions.

You might also like