Java Reference
In-Depth Information
5. Languages like Java andCallowconditional execution among statements
using the if construct. Those languages also allow conditional selection
of an expression value using the ternary operator , which chooses one
of two expressions based on the truth of a predicate. For example, the
expression b? 3: 5has value 3 if b is true ;otherwise,ithasvalue
5.
(a) Can the ternary operator be represented by an CondTestingnode in
the AST? If not, design an appropriate AST node for representing
the ternary operator.
(b) How does the treatment of the ternary operator di
er from the
treatment of an if statement during semantic analysis?
(c) How does code generation di
ff
ff
er for the two constructs?
6. Lisp o
ff
ers a generalization of an if statement known as cond .
(a) Investigate the syntax and semantics of the cond construct and doc-
ument your findings. Aim for a general treatment based on exam-
ining specific languages that o
er cond-like constructs.
(b) Design an AST representation for cond and define interfaces to
access the components of a cond construct.
(c) Write a code-generation visitor for cond based on your AST inter-
faces.
ff
7. Languages like C
er the switch statement as another
construct for managing conditional execution.
++
and Java o
ff
(a) Investigate the syntax and semantics of the switch and document
your findings. Be mindful of the semantics of break and its role in
the execution of a switch statement. Note any type restrictions on
the expression supplied to a switch statement.
(b) Design an AST representation for switch and define interfaces to
access the components of a cond construct.
(c) Write a code-generation visitor for cond based on your AST inter-
faces. Generate code based on one of the following strategies:
Conditional execution ismanaged by predicate tests and jumps,
in the style of an if statement.
A lookup table is generated with ( value , label ) pairs (cf. JVM
lookupswitch instruction). If the switch's expression has a
given value, then execution proceeds at its associated label. A
default label is also provided as the outcome for expressions
whose value does not appear in the table.
 
Search WWH ::




Custom Search